Skip to content

Commit

Permalink
Merge 5f4115b into bbbe479
Browse files Browse the repository at this point in the history
  • Loading branch information
xubo245 committed Oct 27, 2018
2 parents bbbe479 + 5f4115b commit ca06371
Show file tree
Hide file tree
Showing 7 changed files with 635 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static void main(String[] args) throws Exception {
CarbonProperties.getInstance()
.addProperty(CarbonLoadOptionConstants.ENABLE_CARBON_LOAD_DIRECT_WRITE_TO_STORE_PATH, "true");

String path = "s3a://sdk/WriterOutput";
String path = "s3a://sdk/WriterOutput4";
if (args.length > 3) {
path=args[3];
}
Expand All @@ -62,16 +62,21 @@ public static void main(String[] args) throws Exception {
num = Integer.parseInt(args[4]);
}

Configuration conf = new Configuration(false);
Configuration conf = new Configuration(true);
conf.set(Constants.ACCESS_KEY, args[0]);
conf.set(Constants.SECRET_KEY, args[1]);
conf.set(Constants.ENDPOINT, args[2]);

Field[] fields = new Field[2];
fields[0] = new Field("name", DataTypes.STRING);
fields[1] = new Field("age", DataTypes.INT);
CarbonWriterBuilder builder = CarbonWriter.builder().outputPath(path).withHadoopConf(conf);
CarbonWriter writer = builder.withCsvInput(new Schema(fields)).build();
CarbonWriter writer = CarbonWriter
.builder()
.outputPath(path)
.withHadoopConf(conf)
.withCsvInput(new Schema(fields))
.writtenBy("SDKS3Example")
.build();

for (int i = 0; i < num; i++) {
writer.write(new String[]{"robot" + (i % 10), String.valueOf(i)});
Expand Down
2 changes: 1 addition & 1 deletion store/CSDK/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ find_package(JNI REQUIRED)
include_directories(${JNI_INCLUDE_DIRS})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES src/CarbonReader.cpp src/CarbonReader.h test/main.cpp src/CarbonRow.h src/CarbonRow.cpp)
set(SOURCE_FILES src/CarbonReader.cpp src/CarbonReader.h test/main.cpp src/CarbonRow.h src/CarbonRow.cpp src/CarbonWriter.h src/CarbonWriter.cpp)

add_executable(CJDK ${SOURCE_FILES})
get_filename_component(JAVA_JVM_LIBRARY_DIR ${JAVA_JVM_LIBRARY} DIRECTORY)
Expand Down
161 changes: 161 additions & 0 deletions store/CSDK/src/CarbonWriter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <stdexcept>
#include "CarbonWriter.h"

void CarbonWriter::builder(JNIEnv *env) {
if (env == NULL) {
throw std::runtime_error("JNIEnv parameter can't be NULL.");
}
jniEnv = env;
carbonWriter = env->FindClass("org/apache/carbondata/sdk/file/CarbonWriter");
if (carbonWriter == NULL) {
throw std::runtime_error("Can't find the class in java: org/apache/carbondata/sdk/file/CarbonWriter");
}
jmethodID carbonWriterBuilderID = env->GetStaticMethodID(carbonWriter, "builder",
"()Lorg/apache/carbondata/sdk/file/CarbonWriterBuilder;");
if (carbonWriterBuilderID == NULL) {
throw std::runtime_error("Can't find the method in java: carbonWriterBuilder");
}
carbonWriterBuilderObject = env->CallStaticObjectMethod(carbonWriter, carbonWriterBuilderID);
}

bool CarbonWriter::checkBuilder() {
if (carbonWriterBuilderObject == NULL) {
throw std::runtime_error("carbonWriterBuilder Object can't be NULL. Please call builder method first.");
}
}

void CarbonWriter::outputPath(char *path) {
if (path == NULL) {
throw std::runtime_error("path parameter can't be NULL.");
}
checkBuilder();
jclass carbonWriterBuilderClass = jniEnv->GetObjectClass(carbonWriterBuilderObject);
jmethodID methodID = jniEnv->GetMethodID(carbonWriterBuilderClass, "outputPath",
"(Ljava/lang/String;)Lorg/apache/carbondata/sdk/file/CarbonWriterBuilder;");
if (methodID == NULL) {
throw std::runtime_error("Can't find the method in java: outputPath");
}
jstring jPath = jniEnv->NewStringUTF(path);
jvalue args[1];
args[0].l = jPath;
carbonWriterBuilderObject = jniEnv->CallObjectMethodA(carbonWriterBuilderObject, methodID, args);
}

void CarbonWriter::withCsvInput(char *jsonSchema) {
if (jsonSchema == NULL) {
throw std::runtime_error("jsonSchema parameter can't be NULL.");
}
checkBuilder();
jclass carbonWriterBuilderClass = jniEnv->GetObjectClass(carbonWriterBuilderObject);
jmethodID methodID = jniEnv->GetMethodID(carbonWriterBuilderClass, "withCsvInput",
"(Ljava/lang/String;)Lorg/apache/carbondata/sdk/file/CarbonWriterBuilder;");
if (methodID == NULL) {
throw std::runtime_error("Can't find the method in java: withCsvInput");
}
jstring jPath = jniEnv->NewStringUTF(jsonSchema);
jvalue args[1];
args[0].l = jPath;
carbonWriterBuilderObject = jniEnv->CallObjectMethodA(carbonWriterBuilderObject, methodID, args);
if (jniEnv->ExceptionCheck()) {
throw jniEnv->ExceptionOccurred();
}
};

void CarbonWriter::withHadoopConf(char *key, char *value) {
if (key == NULL) {
throw std::runtime_error("key parameter can't be NULL.");
}
if (value == NULL) {
throw std::runtime_error("value parameter can't be NULL.");
}
checkBuilder();
jclass carbonWriterBuilderClass = jniEnv->GetObjectClass(carbonWriterBuilderObject);
jmethodID methodID = jniEnv->GetMethodID(carbonWriterBuilderClass, "withHadoopConf",
"(Ljava/lang/String;Ljava/lang/String;)Lorg/apache/carbondata/sdk/file/CarbonWriterBuilder;");
if (methodID == NULL) {
throw std::runtime_error("Can't find the method in java: withHadoopConf");
}
jvalue args[2];
args[0].l = jniEnv->NewStringUTF(key);
args[1].l = jniEnv->NewStringUTF(value);
carbonWriterBuilderObject = jniEnv->CallObjectMethodA(carbonWriterBuilderObject, methodID, args);
}

void CarbonWriter::writtenBy(char *appName) {
checkBuilder();
jclass carbonWriterBuilderClass = jniEnv->GetObjectClass(carbonWriterBuilderObject);
jmethodID methodID = jniEnv->GetMethodID(carbonWriterBuilderClass, "writtenBy",
"(Ljava/lang/String;)Lorg/apache/carbondata/sdk/file/CarbonWriterBuilder;");
if (methodID == NULL) {
throw std::runtime_error("Can't find the method in java: writtenBy");
}
jvalue args[1];
args[0].l = jniEnv->NewStringUTF(appName);
carbonWriterBuilderObject = jniEnv->CallObjectMethodA(carbonWriterBuilderObject, methodID, args);
}

void CarbonWriter::build() {
checkBuilder();
jclass carbonWriterBuilderClass = jniEnv->GetObjectClass(carbonWriterBuilderObject);
jmethodID methodID = jniEnv->GetMethodID(carbonWriterBuilderClass, "build",
"()Lorg/apache/carbondata/sdk/file/CarbonWriter;");
if (methodID == NULL) {
throw std::runtime_error("Can't find the method in java: build");
}
carbonWriterObject = jniEnv->CallObjectMethod(carbonWriterBuilderObject, methodID);

if (jniEnv->ExceptionCheck()) {
throw jniEnv->ExceptionOccurred();
}
}

void CarbonWriter::write(jobject obj) {
if (carbonWriterObject == NULL) {
throw std::runtime_error("Please call build first.");
}
if (writeID == NULL) {
carbonWriter = jniEnv->GetObjectClass(carbonWriterObject);
writeID = jniEnv->GetMethodID(carbonWriter, "write", "(Ljava/lang/Object;)V");
if (writeID == NULL) {
throw std::runtime_error("Can't find the method in java: write");
}
}
jvalue args[1];
args[0].l = obj;
jniEnv->CallBooleanMethodA(carbonWriterObject, writeID, args);
if (jniEnv->ExceptionCheck()) {
throw jniEnv->ExceptionOccurred();
}
};

jboolean CarbonWriter::close() {
if (carbonWriterObject == NULL) {
throw std::runtime_error("Please call build first.");
}
jclass carbonWriter = jniEnv->GetObjectClass(carbonWriterObject);
jmethodID methodID = jniEnv->GetMethodID(carbonWriter, "close", "()V");
if (methodID == NULL) {
throw std::runtime_error("Can't find the method in java: close");
}
jniEnv->CallBooleanMethod(carbonWriterObject, methodID);
if (jniEnv->ExceptionCheck()) {
throw jniEnv->ExceptionOccurred();
}
}
118 changes: 118 additions & 0 deletions store/CSDK/src/CarbonWriter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <jni.h>

class CarbonWriter {
private:
/**
* jni env
*/
JNIEnv *jniEnv;

/**
* carbonWriterBuilder object for building carbonWriter
* it can configure some operation
*/
jobject carbonWriterBuilderObject = NULL;

/**
* carbonWriter object for writing data
*/
jobject carbonWriterObject;

/**
* carbon writer class
*/
jclass carbonWriter;

/**
* write method id
*/
jmethodID writeID = NULL;

/**
* check whether has called builder
*
* @return true or throw exception
*/
bool checkBuilder();
public:
/**
* create a CarbonWriterBuilder object for building carbonWriter,
* CarbonWriterBuilder object can configure different parameter
*
* @param env JNIEnv
* @return CarbonWriterBuilder object
*/
void builder(JNIEnv *env);

/**
* Sets the output path of the writer builder
*
* @param path is the absolute path where output files are written
* This method must be called when building CarbonWriterBuilder
* @return updated CarbonWriterBuilder
*/
void outputPath(char *path);

/**
* configure the schema with json style schema
*
* @param jsonSchema json style schema
* @return updated CarbonWriterBuilder
*/
void withCsvInput(char *jsonSchema);

/**
* configure parameter, including ak,sk and endpoint
*
* @param key key word
* @param value value
* @return CarbonWriterBuilder object
*/
void withHadoopConf(char *key, char *value);

/**
* @param appName appName which is writing the carbondata files
*/
void writtenBy(char *appName);

/**
* build carbonWriter object for writing data
* it support write data from load disk
*
* @return carbonWriter object
*/
void build();

/**
* Write an object to the file, the format of the object depends on the
* implementation.
* Note: This API is not thread safe
*/
void write(jobject obj);

/**
* close the carbon Writer
*
* @return boolean value
*/
jboolean close();
};


0 comments on commit ca06371

Please sign in to comment.