Skip to content

Commit 46bba74

Browse files
committed
fix: address reviews v1
1 parent c35eefd commit 46bba74

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

java/source/c_data.rst

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.. _c-data-java:
2+
3+
==================
4+
C Data Integration
5+
==================
6+
7+
C Data interface is an important aspect of supporting multiple languages in Apache Arrow.
8+
A Java programme can seamlessly work with C++ and Python programmes. The following examples
9+
demonstrates how it can be done.
10+
11+
:ref:`arrow-python-java`
12+
------------------------

java/source/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ This cookbook is tested with Apache Arrow |version|.
4343
data
4444
avro
4545
jdbc
46-
python_java
46+
c_data
4747

4848
Indices and tables
4949
==================

java/source/python_java.rst

+54-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Java Component:
148148
In the Java component, the MapValuesConsumer class receives data from the Python component through C Data.
149149
It then updates the data and sends it back to the Python component.
150150

151-
.. code-block:: java
151+
.. testcode:: java
152152

153153
import org.apache.arrow.c.ArrowArray;
154154
import org.apache.arrow.c.ArrowSchema;
@@ -164,6 +164,8 @@ Java Component:
164164
private final static BufferAllocator allocator = new RootAllocator();
165165
private final CDataDictionaryProvider provider;
166166
private FieldVector vector;
167+
private final static BigIntVector intVector = new BigIntVector("internal_test_vector", allocator);
168+
167169

168170
public MapValuesConsumer(CDataDictionaryProvider provider) {
169171
this.provider = provider;
@@ -184,13 +186,64 @@ Java Component:
184186
this.doWorkInJava(vector);
185187
}
186188

189+
public FieldVector updateFromJava(long c_array_ptr, long c_schema_ptr) {
190+
ArrowArray arrow_array = ArrowArray.wrap(c_array_ptr);
191+
ArrowSchema arrow_schema = ArrowSchema.wrap(c_schema_ptr);
192+
vector = Data.importVector(allocator, arrow_array, arrow_schema, null);
193+
this.doWorkInJava(vector);
194+
return vector;
195+
}
196+
187197
private void doWorkInJava(FieldVector vector) {
188198
System.out.println("Doing work in Java");
189199
BigIntVector bigIntVector = (BigIntVector)vector;
190200
bigIntVector.setSafe(0, 2);
191201
}
202+
203+
private static BigIntVector getIntVectorForJavaConsumers() {
204+
intVector.allocateNew(3);
205+
intVector.set(0, 1);
206+
intVector.set(1, 7);
207+
intVector.set(2, 93);
208+
intVector.setValueCount(3);
209+
return intVector;
210+
}
211+
212+
public static void simulateAsAJavaConsumers() {
213+
CDataDictionaryProvider provider = new CDataDictionaryProvider();
214+
MapValueConsumerV2 mvc = new MapValueConsumerV2(provider);//FIXME! Use constructor with dictionary provider
215+
try (
216+
ArrowArray arrowArray = ArrowArray.allocateNew(allocator);
217+
ArrowSchema arrowSchema = ArrowSchema.allocateNew(allocator)
218+
) {
219+
Data.exportVector(allocator, getIntVectorForJavaConsumers(), provider, arrowArray, arrowSchema);
220+
FieldVector updatedVector = mvc.updateFromJava(arrowArray.memoryAddress(), arrowSchema.memoryAddress());
221+
try (ArrowArray usedArray = ArrowArray.allocateNew(allocator);
222+
ArrowSchema usedSchema = ArrowSchema.allocateNew(allocator)) {
223+
Data.exportVector(allocator, updatedVector, provider, usedArray, usedSchema);
224+
try(FieldVector valueVectors = Data.importVector(allocator, usedArray, usedSchema, provider)) {
225+
System.out.println(valueVectors);
226+
}
227+
}
228+
}
229+
}
230+
231+
public static void close() {
232+
intVector.close();
233+
}
234+
235+
public static void main(String[] args) {
236+
simulateAsAJavaConsumers();
237+
close();
238+
}
192239
}
193240

241+
.. testoutput::
242+
243+
Doing work in Java
244+
[2, 7, 93]
245+
246+
194247
The Java component performs the following actions:
195248

196249
1. Receives data from the Python component.

0 commit comments

Comments
 (0)