Skip to content

Commit

Permalink
[CARBONDATA-2997] Support read schema from index file and data file i…
Browse files Browse the repository at this point in the history
…n CSDK

1.support readSchemaInIndexFile
2.support readSchemaInDataFile
3.support get field name and data type name
4.suppport get array child element data type name
5.can read schema when carbonreader has set ak,sk,endpoint
6.TODO: need support read scehma from S3 in the future

This closes #2807
  • Loading branch information
xubo245 authored and QiangCai committed Nov 1, 2018
1 parent 26d1cdd commit ac94dba
Show file tree
Hide file tree
Showing 11 changed files with 687 additions and 8 deletions.
82 changes: 81 additions & 1 deletion docs/csdk-guide.md
Expand Up @@ -39,6 +39,7 @@ and read data from S3 at main.cpp of CSDK module. Finally, users need to
release the memory and destroy JVM.

## API List
### CarbonReader
```
/**
* create a CarbonReaderBuilder object for building carbonReader,
Expand Down Expand Up @@ -119,7 +120,7 @@ and write data to S3 at main.cpp of CSDK module. Finally, users need to
release the memory and destroy JVM.

## API List

### CarbonWriter
```
/**
* create a CarbonWriterBuilder object for building carbonWriter,
Expand Down Expand Up @@ -187,4 +188,83 @@ release the memory and destroy JVM.
* close the carbon Writer
*/
void close();
```

### CarbonSchemaReader

```
/**
* constructor with jni env
*
* @param env jni env
*/
CarbonSchemaReader(JNIEnv *env);
```
```
/**
* read Schema from Data File
*
* @param path Data File path
* @return carbon schema object
*/
jobject readSchemaInDataFile(char *path);
```
```
/**
* read Schema from index File
*
* @param path index File path
* @return carbon schema object
*/
jobject readSchemaInIndexFile(char *path);
```
###Schema
```
/**
* constructor with jni env and carbon schema data
*
* @param env jni env
* @param schema carbon schema data
*/
Schema(JNIEnv *env, jobject schema);
```
```
/**
* get fields length of schema
*
* @return fields length
*/
int getFieldsLength();
```
```
/**
* get field name by ordinal
*
* @param ordinal the data index of carbon schema
* @return ordinal field name
*/
char *getFieldName(int ordinal);
```
```
/**
* get field data type name by ordinal
*
* @param ordinal the data index of carbon schema
* @return ordinal field data type name
*/
char *getFieldDataTypeName(int ordinal);
```
```
/**
* get array child element data type name by ordinal
*
* @param ordinal the data index of carbon schema
* @return ordinal array child element data type name
*/
char *getArrayElementTypeName(int ordinal);
```
2 changes: 1 addition & 1 deletion store/CSDK/CMakeLists.txt
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 src/CarbonWriter.h src/CarbonWriter.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 src/CarbonSchemaReader.h src/CarbonSchemaReader.cpp src/Schema.h src/Schema.cpp)

add_executable(CJDK ${SOURCE_FILES})
get_filename_component(JAVA_JVM_LIBRARY_DIR ${JAVA_JVM_LIBRARY} DIRECTORY)
Expand Down
12 changes: 6 additions & 6 deletions store/CSDK/src/CarbonReader.cpp
Expand Up @@ -86,9 +86,9 @@ void CarbonReader::projection(int argc, char *argv[]) {
}
checkBuilder();
jclass carbonReaderBuilderClass = jniEnv->GetObjectClass(carbonReaderBuilderObject);
jmethodID buildID = jniEnv->GetMethodID(carbonReaderBuilderClass, "projection",
jmethodID methodID = jniEnv->GetMethodID(carbonReaderBuilderClass, "projection",
"([Ljava/lang/String;)Lorg/apache/carbondata/sdk/file/CarbonReaderBuilder;");
if (buildID == NULL) {
if (methodID == NULL) {
throw std::runtime_error("Can't find the method in java: projection");
}
jclass objectArrayClass = jniEnv->FindClass("Ljava/lang/String;");
Expand All @@ -103,7 +103,7 @@ void CarbonReader::projection(int argc, char *argv[]) {

jvalue args[1];
args[0].l = array;
carbonReaderBuilderObject = jniEnv->CallObjectMethodA(carbonReaderBuilderObject, buildID, args);
carbonReaderBuilderObject = jniEnv->CallObjectMethodA(carbonReaderBuilderObject, methodID, args);
}

void CarbonReader::withHadoopConf(char *key, char *value) {
Expand All @@ -129,12 +129,12 @@ void CarbonReader::withHadoopConf(char *key, char *value) {
jobject CarbonReader::build() {
checkBuilder();
jclass carbonReaderBuilderClass = jniEnv->GetObjectClass(carbonReaderBuilderObject);
jmethodID buildID = jniEnv->GetMethodID(carbonReaderBuilderClass, "build",
jmethodID methodID = jniEnv->GetMethodID(carbonReaderBuilderClass, "build",
"()Lorg/apache/carbondata/sdk/file/CarbonReader;");
if (buildID == NULL) {
if (methodID == NULL) {
throw std::runtime_error("Can't find the method in java: build");
}
carbonReaderObject = jniEnv->CallObjectMethod(carbonReaderBuilderObject, buildID);
carbonReaderObject = jniEnv->CallObjectMethod(carbonReaderBuilderObject, methodID);
if (jniEnv->ExceptionCheck()) {
throw jniEnv->ExceptionOccurred();
}
Expand Down
68 changes: 68 additions & 0 deletions store/CSDK/src/CarbonSchemaReader.cpp
@@ -0,0 +1,68 @@
/*
* 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 "CarbonSchemaReader.h"

CarbonSchemaReader::CarbonSchemaReader(JNIEnv *env) {
if (env == NULL) {
throw std::runtime_error("JNIEnv parameter can't be NULL.");
}
this->carbonSchemaReaderClass = env->FindClass("org/apache/carbondata/sdk/file/CarbonSchemaReader");
if (carbonSchemaReaderClass == NULL) {
throw std::runtime_error("Can't find the class in java: org/apache/carbondata/sdk/file/CarbonSchemaReader");
}
this->jniEnv = env;
}

jobject CarbonSchemaReader::readSchemaInDataFile(char *path) {
if (path == NULL) {
throw std::runtime_error("path parameter can't be NULL.");
}
jmethodID methodID = jniEnv->GetStaticMethodID(carbonSchemaReaderClass, "readSchemaInDataFile",
"(Ljava/lang/String;)Lorg/apache/carbondata/sdk/file/Schema;");
if (methodID == NULL) {
throw std::runtime_error("Can't find the method in java: readSchemaInDataFile");
}
jstring jPath = jniEnv->NewStringUTF(path);
jvalue args[1];
args[0].l = jPath;
jobject result = jniEnv->CallStaticObjectMethodA(carbonSchemaReaderClass, methodID, args);
if (jniEnv->ExceptionCheck()) {
throw jniEnv->ExceptionOccurred();
}
return result;
}

jobject CarbonSchemaReader::readSchemaInIndexFile(char *path) {
if (path == NULL) {
throw std::runtime_error("path parameter can't be NULL.");
}
jmethodID methodID = jniEnv->GetStaticMethodID(carbonSchemaReaderClass, "readSchemaInIndexFile",
"(Ljava/lang/String;)Lorg/apache/carbondata/sdk/file/Schema;");
if (methodID == NULL) {
throw std::runtime_error("Can't find the method in java: readSchemaInDataFile");
}
jstring jPath = jniEnv->NewStringUTF(path);
jvalue args[1];
args[0].l = jPath;
jobject result = jniEnv->CallStaticObjectMethodA(carbonSchemaReaderClass, methodID, args);
if (jniEnv->ExceptionCheck()) {
throw jniEnv->ExceptionOccurred();
}
return result;
}
58 changes: 58 additions & 0 deletions store/CSDK/src/CarbonSchemaReader.h
@@ -0,0 +1,58 @@
/*
* 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 CarbonSchemaReader {
private:

/**
* jni env
*/
JNIEnv *jniEnv;

/**
* carbonSchemaReader Class for get method id and call method
*/
jclass carbonSchemaReaderClass;

public:

/**
* constructor with jni env
*
* @param env jni env
*/
CarbonSchemaReader(JNIEnv *env);

/**
* read Schema from Data File
*
* @param path Data File path
* @return carbon schema object
*/
jobject readSchemaInDataFile(char *path);

/**
* read Schema from index File
*
* @param path index File path
* @return carbon schema object
*/
jobject readSchemaInIndexFile(char *path);

};
92 changes: 92 additions & 0 deletions store/CSDK/src/Schema.cpp
@@ -0,0 +1,92 @@
/*
* 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>
#include <stdexcept>
#include "Schema.h"

Schema::Schema(JNIEnv *env, jobject schema) {
if (env == NULL) {
throw std::runtime_error("JNIEnv parameter can't be NULL.");
}
if (schema == NULL) {
throw std::runtime_error("schema parameter can't be NULL.");
}
this->schemaClass = env->FindClass("org/apache/carbondata/sdk/file/Schema");
if (schemaClass == NULL) {
throw std::runtime_error("Can't find the class in java: org/apache/carbondata/sdk/file/Schema");
}
this->jniEnv = env;
this->schema = schema;
}

int Schema::getFieldsLength() {
jmethodID methodID = jniEnv->GetMethodID(schemaClass, "getFieldsLength",
"()I");
if (methodID == NULL) {
throw std::runtime_error("Can't find the method in java: getFieldsLength");
}
return jniEnv->CallIntMethod(schema, methodID);
};

void Schema::checkOrdinal(int ordinal) {
if (ordinal < 0) {
throw std::runtime_error("ordinal parameter can't be negative.");
}
}

char *Schema::getFieldName(int ordinal) {
checkOrdinal(ordinal);
jmethodID methodID = jniEnv->GetMethodID(schemaClass, "getFieldName",
"(I)Ljava/lang/String;");
if (methodID == NULL) {
throw std::runtime_error("Can't find the method in java: getFieldName");
}
jvalue args[1];
args[0].i = ordinal;
jobject fieldName = jniEnv->CallObjectMethodA(schema, methodID, args);
return (char *) jniEnv->GetStringUTFChars((jstring) fieldName, JNI_FALSE);
};

char *Schema::getFieldDataTypeName(int ordinal) {
checkOrdinal(ordinal);
jmethodID methodID = jniEnv->GetMethodID(schemaClass, "getFieldDataTypeName",
"(I)Ljava/lang/String;");
if (methodID == NULL) {
throw std::runtime_error("Can't find the method in java: getFieldDataTypeName");
}
jvalue args[1];
args[0].i = ordinal;
jobject fieldName = jniEnv->CallObjectMethodA(schema, methodID, args);
return (char *) jniEnv->GetStringUTFChars((jstring) fieldName, JNI_FALSE);
};

char *Schema::getArrayElementTypeName(int ordinal) {
checkOrdinal(ordinal);
jmethodID methodID = jniEnv->GetMethodID(schemaClass, "getArrayElementTypeName",
"(I)Ljava/lang/String;");
if (methodID == NULL) {
throw std::runtime_error("Can't find the method in java: getArrayElementTypeName");
}
jvalue args[1];
args[0].i = ordinal;
jobject fieldName = jniEnv->CallObjectMethodA(schema, methodID, args);
if (jniEnv->ExceptionCheck()) {
throw jniEnv->ExceptionOccurred();
}
return (char *) jniEnv->GetStringUTFChars((jstring) fieldName, JNI_FALSE);
};

0 comments on commit ac94dba

Please sign in to comment.