Skip to content

Commit c20eea0

Browse files
GH-317: [Java] add recipe for vector schema root appender cases (#318)
Fixes #317. --------- Co-authored-by: David Li <[email protected]>
1 parent d4e3b34 commit c20eea0

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

java/source/data.rst

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,58 @@ Recipes related to compare, filtering or transforming data.
2323

2424
.. contents::
2525

26+
Concatenate VectorSchemaRoots
27+
=============================
28+
29+
In some cases, VectorSchemaRoot needs to be modeled as a container. To accomplish
30+
this, you can use ``VectorSchemaRootAppender.append``. The following code
31+
creates two roots, then concatenates them together:
32+
33+
.. testcode::
34+
35+
import org.apache.arrow.memory.BufferAllocator;
36+
import org.apache.arrow.memory.RootAllocator;
37+
import org.apache.arrow.vector.IntVector;
38+
import org.apache.arrow.vector.VectorSchemaRoot;
39+
import org.apache.arrow.vector.types.pojo.ArrowType;
40+
import org.apache.arrow.vector.types.pojo.Field;
41+
import org.apache.arrow.vector.types.pojo.FieldType;
42+
import org.apache.arrow.vector.types.pojo.Schema;
43+
import org.apache.arrow.vector.util.VectorSchemaRootAppender;
44+
45+
import static java.util.Arrays.asList;
46+
47+
Field column_one = new Field("column-one", FieldType.nullable(new ArrowType.Int(32, true)), null);
48+
Schema schema = new Schema(asList(column_one));
49+
try (
50+
BufferAllocator allocator = new RootAllocator();
51+
VectorSchemaRoot rootOne = VectorSchemaRoot.create(schema, allocator);
52+
VectorSchemaRoot rootTwo = VectorSchemaRoot.create(schema, allocator);
53+
VectorSchemaRoot result = VectorSchemaRoot.create(schema, allocator);
54+
) {
55+
IntVector appenderOne = (IntVector) rootOne.getVector(0);
56+
rootOne.allocateNew();
57+
appenderOne.set(0, 100);
58+
appenderOne.set(1, 20);
59+
rootOne.setRowCount(2);
60+
IntVector appenderTwo = (IntVector) rootTwo.getVector(0);
61+
rootTwo.allocateNew();
62+
appenderTwo.set(0, 34);
63+
appenderTwo.set(1, 75);
64+
rootTwo.setRowCount(2);
65+
result.allocateNew();
66+
VectorSchemaRootAppender.append(result, rootOne, rootTwo);
67+
System.out.print(result.contentToTSVString());
68+
}
69+
70+
.. testoutput::
71+
72+
column-one
73+
100
74+
20
75+
34
76+
75
77+
2678
Compare Vectors for Field Equality
2779
==================================
2880

0 commit comments

Comments
 (0)