davisusanibar commented on code in PR #13788:
URL: https://github.com/apache/arrow/pull/13788#discussion_r937771213


##########
docs/source/java/cdata.rst:
##########
@@ -220,4 +221,252 @@ Let's create a Java class to test our bridge:
 
     C++-allocated array: [1, 2, 3, null, 5, 6, 7, 8, 9, 10]
 
+Share an Int32 array from Java to C++
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+**Java Side**
+
+For this example, we will build a JAR with all dependencies bundled.
+
+.. code-block:: xml
+
+    <?xml version="1.0" encoding="UTF-8"?>
+    <project xmlns="http://maven.apache.org/POM/4.0.0";
+             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+        <modelVersion>4.0.0</modelVersion>
+        <groupId>org.example</groupId>
+        <artifactId>cpptojava</artifactId>
+        <version>1.0-SNAPSHOT</version>
+        <properties>
+            <maven.compiler.source>11</maven.compiler.source>
+            <maven.compiler.target>11</maven.compiler.target>
+            <arrow.version>8.0.0</arrow.version>
+        </properties>
+        <repositories>
+            <repository>
+                <id>arrow-nightly</id>
+                <url>https://nightlies.apache.org/arrow/java</url>
+            </repository>
+        </repositories>
+        <dependencies>
+            <dependency>
+                <groupId>org.apache.arrow</groupId>
+                <artifactId>arrow-c-data</artifactId>
+                <version>${arrow.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.apache.arrow</groupId>
+                <artifactId>arrow-memory-netty</artifactId>
+                <version>${arrow.version}</version>
+            </dependency>
+        </dependencies>
+        <build>
+            <plugins>
+                <plugin>
+                    <groupId>org.apache.maven.plugins</groupId>
+                    <artifactId>maven-assembly-plugin</artifactId>
+                    <executions>
+                        <execution>
+                            <phase>package</phase>
+                            <goals>
+                                <goal>single</goal>
+                            </goals>
+                            <configuration>
+                                <archive>
+                                    <manifest>
+                                        <mainClass>
+                                            ToBeCalledByCpp
+                                        </mainClass>
+                                    </manifest>
+                                </archive>
+                                <descriptorRefs>
+                                    
<descriptorRef>jar-with-dependencies</descriptorRef>
+                                </descriptorRefs>
+                            </configuration>
+                        </execution>
+                    </executions>
+                </plugin>
+            </plugins>
+        </build>
+    </project>
+
+.. code-block:: java
+
+    import org.apache.arrow.c.ArrowArray;
+    import org.apache.arrow.c.ArrowSchema;
+    import org.apache.arrow.c.Data;
+    import org.apache.arrow.memory.BufferAllocator;
+    import org.apache.arrow.memory.RootAllocator;
+    import org.apache.arrow.vector.FieldVector;
+    import org.apache.arrow.vector.IntVector;
+    import org.apache.arrow.vector.VectorSchemaRoot;
+
+    import java.util.Arrays;
+
+    public class ToBeCalledByCpp {
+        final static BufferAllocator allocator = new RootAllocator();
+
+        public static void fillVector(long schemaAddress, long arrayAddress){
+            try (ArrowArray arrow_array = ArrowArray.wrap(arrayAddress);
+                 ArrowSchema arrow_schema = ArrowSchema.wrap(schemaAddress) ) {
+                Data.exportVector(allocator, populateFieldVectorToExport(), 
null, arrow_array, arrow_schema);
+            }
+        }
+
+        public static FieldVector populateFieldVectorToExport(){
+            IntVector intVector = new IntVector("int-to-export", allocator);
+            intVector.allocateNew(3);
+            intVector.setSafe(0, 1);
+            intVector.setSafe(1, 2);
+            intVector.setSafe(2, 3);
+            intVector.setValueCount(3);
+            System.out.println("[Java] FieldVector: \n" + intVector);
+            return intVector;
+        }
+
+        public static void fillVectorSchemaRoot(long schemaAddress, long 
arrayAddress){
+            try (ArrowArray arrow_array = ArrowArray.wrap(arrayAddress);
+                 ArrowSchema arrow_schema = ArrowSchema.wrap(schemaAddress) ) {
+                Data.exportVectorSchemaRoot(allocator, 
populateVectorSchemaRootToExport(), null, arrow_array, arrow_schema);
+            }
+        }
+
+        public static VectorSchemaRoot populateVectorSchemaRootToExport(){
+            IntVector intVector = new IntVector("age-to-export", allocator);
+            intVector.setSafe(0, 10);
+            intVector.setSafe(1, 20);
+            intVector.setSafe(2, 30);
+            VectorSchemaRoot root = new 
VectorSchemaRoot(Arrays.asList(intVector));
+            root.setRowCount(3);
+            System.out.println("[Java] VectorSchemaRoot: \n" + 
root.contentToTSVString());
+            return root;
+        }
+    }
+
+
+Build the JAR and copy it to the C++ project.
+
+.. code-block:: shell
+
+    $ mvn clean install
+    $ cp target/cpptojava-1.0-SNAPSHOT-jar-with-dependencies.jar <c++ project 
path>/cpptojava.jar
+
+**C++ Side**
+
+This application uses JNI to call Java code, but transfers data (zero-copy) 
via the C Data Interface instead.
+
+.. code-block:: cpp
+
+    #include <iostream>
+    #include <arrow/api.h>
+    #include <arrow/c/bridge.h>
+    #include <jni.h>
+
+    JNIEnv *CreateVM(JavaVM **jvm) {
+        JNIEnv *env;
+        JavaVMInitArgs vm_args;
+        JavaVMOption options[2];
+        options[0].optionString = "-Djava.class.path=cpptojava.jar"; // java 
jar name
+        options[1].optionString = "-DXcheck:jni:pedantic";
+        vm_args.version = JNI_VERSION_1_8;
+        vm_args.nOptions = 2;
+        vm_args.options = options;
+        int status = JNI_CreateJavaVM(jvm, (void **) &env, &vm_args);
+        if (status < 0) {
+            std::cout << "\n<<<<< Unable to Launch JVM >>>>>\n" << std::endl;
+            std::abort();
+        }
+        return env;
+    }
+
+    int main() {
+        JNIEnv *env;
+        JavaVM *jvm;
+        env = CreateVM(&jvm);
+        if (env == nullptr) return 1;
+        jclass javaClassToBeCalledByCpp = NULL;
+        javaClassToBeCalledByCpp = env->FindClass("ToBeCalledByCpp");
+        if (javaClassToBeCalledByCpp != NULL) {
+            jmethodID fillVector = NULL;
+            fillVector = env->GetStaticMethodID(javaClassToBeCalledByCpp, 
"fillVector", "(JJ)V");
+            if (fillVector != NULL) {
+                struct ArrowSchema arrowSchema;
+                struct ArrowArray arrowArray;
+                std::cout << "\n<<<<< C++ to Java for Arrays >>>>>\n" << 
std::endl;
+                env->CallStaticVoidMethod(javaClassToBeCalledByCpp, 
fillVector, reinterpret_cast<uintptr_t>(&arrowSchema),

Review Comment:
   Updated



##########
docs/source/java/cdata.rst:
##########
@@ -220,4 +221,252 @@ Let's create a Java class to test our bridge:
 
     C++-allocated array: [1, 2, 3, null, 5, 6, 7, 8, 9, 10]
 
+Share an Int32 array from Java to C++
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+**Java Side**
+
+For this example, we will build a JAR with all dependencies bundled.
+
+.. code-block:: xml
+
+    <?xml version="1.0" encoding="UTF-8"?>
+    <project xmlns="http://maven.apache.org/POM/4.0.0";
+             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+        <modelVersion>4.0.0</modelVersion>
+        <groupId>org.example</groupId>
+        <artifactId>cpptojava</artifactId>
+        <version>1.0-SNAPSHOT</version>
+        <properties>
+            <maven.compiler.source>11</maven.compiler.source>
+            <maven.compiler.target>11</maven.compiler.target>
+            <arrow.version>8.0.0</arrow.version>
+        </properties>
+        <repositories>
+            <repository>
+                <id>arrow-nightly</id>
+                <url>https://nightlies.apache.org/arrow/java</url>
+            </repository>
+        </repositories>
+        <dependencies>
+            <dependency>
+                <groupId>org.apache.arrow</groupId>
+                <artifactId>arrow-c-data</artifactId>
+                <version>${arrow.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.apache.arrow</groupId>
+                <artifactId>arrow-memory-netty</artifactId>
+                <version>${arrow.version}</version>
+            </dependency>
+        </dependencies>
+        <build>
+            <plugins>
+                <plugin>
+                    <groupId>org.apache.maven.plugins</groupId>
+                    <artifactId>maven-assembly-plugin</artifactId>
+                    <executions>
+                        <execution>
+                            <phase>package</phase>
+                            <goals>
+                                <goal>single</goal>
+                            </goals>
+                            <configuration>
+                                <archive>
+                                    <manifest>
+                                        <mainClass>
+                                            ToBeCalledByCpp
+                                        </mainClass>
+                                    </manifest>
+                                </archive>
+                                <descriptorRefs>
+                                    
<descriptorRef>jar-with-dependencies</descriptorRef>
+                                </descriptorRefs>
+                            </configuration>
+                        </execution>
+                    </executions>
+                </plugin>
+            </plugins>
+        </build>
+    </project>
+
+.. code-block:: java
+
+    import org.apache.arrow.c.ArrowArray;
+    import org.apache.arrow.c.ArrowSchema;
+    import org.apache.arrow.c.Data;
+    import org.apache.arrow.memory.BufferAllocator;
+    import org.apache.arrow.memory.RootAllocator;
+    import org.apache.arrow.vector.FieldVector;
+    import org.apache.arrow.vector.IntVector;
+    import org.apache.arrow.vector.VectorSchemaRoot;
+
+    import java.util.Arrays;
+
+    public class ToBeCalledByCpp {
+        final static BufferAllocator allocator = new RootAllocator();
+
+        public static void fillVector(long schemaAddress, long arrayAddress){
+            try (ArrowArray arrow_array = ArrowArray.wrap(arrayAddress);
+                 ArrowSchema arrow_schema = ArrowSchema.wrap(schemaAddress) ) {
+                Data.exportVector(allocator, populateFieldVectorToExport(), 
null, arrow_array, arrow_schema);
+            }
+        }
+
+        public static FieldVector populateFieldVectorToExport(){
+            IntVector intVector = new IntVector("int-to-export", allocator);
+            intVector.allocateNew(3);
+            intVector.setSafe(0, 1);
+            intVector.setSafe(1, 2);
+            intVector.setSafe(2, 3);
+            intVector.setValueCount(3);
+            System.out.println("[Java] FieldVector: \n" + intVector);
+            return intVector;
+        }
+
+        public static void fillVectorSchemaRoot(long schemaAddress, long 
arrayAddress){
+            try (ArrowArray arrow_array = ArrowArray.wrap(arrayAddress);
+                 ArrowSchema arrow_schema = ArrowSchema.wrap(schemaAddress) ) {
+                Data.exportVectorSchemaRoot(allocator, 
populateVectorSchemaRootToExport(), null, arrow_array, arrow_schema);
+            }
+        }
+
+        public static VectorSchemaRoot populateVectorSchemaRootToExport(){
+            IntVector intVector = new IntVector("age-to-export", allocator);
+            intVector.setSafe(0, 10);
+            intVector.setSafe(1, 20);
+            intVector.setSafe(2, 30);
+            VectorSchemaRoot root = new 
VectorSchemaRoot(Arrays.asList(intVector));
+            root.setRowCount(3);
+            System.out.println("[Java] VectorSchemaRoot: \n" + 
root.contentToTSVString());
+            return root;
+        }
+    }
+
+
+Build the JAR and copy it to the C++ project.
+
+.. code-block:: shell
+
+    $ mvn clean install
+    $ cp target/cpptojava-1.0-SNAPSHOT-jar-with-dependencies.jar <c++ project 
path>/cpptojava.jar
+
+**C++ Side**
+
+This application uses JNI to call Java code, but transfers data (zero-copy) 
via the C Data Interface instead.
+
+.. code-block:: cpp
+
+    #include <iostream>
+    #include <arrow/api.h>
+    #include <arrow/c/bridge.h>
+    #include <jni.h>
+
+    JNIEnv *CreateVM(JavaVM **jvm) {
+        JNIEnv *env;
+        JavaVMInitArgs vm_args;
+        JavaVMOption options[2];
+        options[0].optionString = "-Djava.class.path=cpptojava.jar"; // java 
jar name
+        options[1].optionString = "-DXcheck:jni:pedantic";
+        vm_args.version = JNI_VERSION_1_8;
+        vm_args.nOptions = 2;
+        vm_args.options = options;
+        int status = JNI_CreateJavaVM(jvm, (void **) &env, &vm_args);
+        if (status < 0) {
+            std::cout << "\n<<<<< Unable to Launch JVM >>>>>\n" << std::endl;
+            std::abort();
+        }
+        return env;
+    }
+
+    int main() {
+        JNIEnv *env;
+        JavaVM *jvm;
+        env = CreateVM(&jvm);
+        if (env == nullptr) return 1;
+        jclass javaClassToBeCalledByCpp = NULL;
+        javaClassToBeCalledByCpp = env->FindClass("ToBeCalledByCpp");
+        if (javaClassToBeCalledByCpp != NULL) {
+            jmethodID fillVector = NULL;
+            fillVector = env->GetStaticMethodID(javaClassToBeCalledByCpp, 
"fillVector", "(JJ)V");
+            if (fillVector != NULL) {
+                struct ArrowSchema arrowSchema;
+                struct ArrowArray arrowArray;
+                std::cout << "\n<<<<< C++ to Java for Arrays >>>>>\n" << 
std::endl;
+                env->CallStaticVoidMethod(javaClassToBeCalledByCpp, 
fillVector, reinterpret_cast<uintptr_t>(&arrowSchema),
+                                          
reinterpret_cast<uintptr_t>(&arrowArray));
+                auto resultImportArray = arrow::ImportArray(&arrowArray, 
&arrowSchema);
+                std::shared_ptr<arrow::Array> array = 
resultImportArray.ValueOrDie();
+                std::cout << "[C++] Array: " << array->ToString() << std::endl;
+            } else {
+                std::cout << "Problem to read method fillVector\n" << 
std::endl;
+                return EXIT_FAILURE;
+            }
+            jmethodID fillVectorSchemaRoot = NULL;
+            fillVectorSchemaRoot = 
env->GetStaticMethodID(javaClassToBeCalledByCpp, "fillVectorSchemaRoot", 
"(JJ)V");
+            if (fillVectorSchemaRoot != NULL) {
+                struct ArrowSchema arrowSchema;
+                struct ArrowArray arrowArray;
+                std::cout << "\n<<<<< C++ to Java for RecordBatch >>>>>\n" << 
std::endl;
+                env->CallStaticVoidMethod(javaClassToBeCalledByCpp, 
fillVectorSchemaRoot,
+                                          
static_cast<long>(reinterpret_cast<uintptr_t>(&arrowSchema)),
+                                          
static_cast<long>(reinterpret_cast<uintptr_t>(&arrowArray)));
+                auto resultImportVectorSchemaRoot = 
arrow::ImportRecordBatch(&arrowArray, &arrowSchema);
+                std::shared_ptr<arrow::RecordBatch> recordBatch = 
resultImportVectorSchemaRoot.ValueOrDie();
+                std::cout << "[C++] RecordBatch: " << recordBatch->ToString() 
<< std::endl;
+            } else {
+                std::cout << "Problem to read method fillVectorSchemaRoot\n" 
<< std::endl;
+                return EXIT_FAILURE;
+            }
+        } else {
+            std::cout << "Problem to read class ToBeCalledByCpp\n" << 
std::endl;
+            return EXIT_FAILURE;
+        }
+        jvm->DestroyJavaVM();
+        return 0;
+    }
+
+CMakeLists.txt definition file:
+
+.. code-block:: xml
+
+    cmake_minimum_required(VERSION 3.19)
+    project(cdatacpptojava)
+    find_package(JNI REQUIRED)
+    find_package(Arrow REQUIRED)
+    message(STATUS "Arrow version: ${ARROW_VERSION}")
+    message(${ARROW_FULL_SO_VERSION})

Review Comment:
   Message not needed, deleted



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to