You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by li...@apache.org on 2023/09/04 20:36:40 UTC

[arrow-cookbook] branch main updated: GH-317: [Java] add recipe for vector schema root appender cases (#318)

This is an automated email from the ASF dual-hosted git repository.

lidavidm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-cookbook.git


The following commit(s) were added to refs/heads/main by this push:
     new c20eea0  GH-317: [Java] add recipe for vector schema root appender cases (#318)
c20eea0 is described below

commit c20eea06ac52e1c36df74de73b2a6d536f3ad56a
Author: david dali susanibar arce <da...@gmail.com>
AuthorDate: Mon Sep 4 15:36:35 2023 -0500

    GH-317: [Java] add recipe for vector schema root appender cases (#318)
    
    Fixes #317.
    
    ---------
    
    Co-authored-by: David Li <li...@gmail.com>
---
 java/source/data.rst | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

diff --git a/java/source/data.rst b/java/source/data.rst
index 929f9cb..04c7ce0 100644
--- a/java/source/data.rst
+++ b/java/source/data.rst
@@ -23,6 +23,58 @@ Recipes related to compare, filtering or transforming data.
 
 .. contents::
 
+Concatenate VectorSchemaRoots
+=============================
+
+In some cases, VectorSchemaRoot needs to be modeled as a container. To accomplish
+this, you can use ``VectorSchemaRootAppender.append``. The following code 
+creates two roots, then concatenates them together:
+
+.. testcode::
+
+    import org.apache.arrow.memory.BufferAllocator;
+    import org.apache.arrow.memory.RootAllocator;
+    import org.apache.arrow.vector.IntVector;
+    import org.apache.arrow.vector.VectorSchemaRoot;
+    import org.apache.arrow.vector.types.pojo.ArrowType;
+    import org.apache.arrow.vector.types.pojo.Field;
+    import org.apache.arrow.vector.types.pojo.FieldType;
+    import org.apache.arrow.vector.types.pojo.Schema;
+    import org.apache.arrow.vector.util.VectorSchemaRootAppender;
+
+    import static java.util.Arrays.asList;
+
+    Field column_one = new Field("column-one", FieldType.nullable(new ArrowType.Int(32, true)), null);
+    Schema schema = new Schema(asList(column_one));
+    try (
+        BufferAllocator allocator = new RootAllocator();
+        VectorSchemaRoot rootOne = VectorSchemaRoot.create(schema, allocator);
+        VectorSchemaRoot rootTwo = VectorSchemaRoot.create(schema, allocator);
+        VectorSchemaRoot result = VectorSchemaRoot.create(schema, allocator);
+    ) {
+        IntVector appenderOne = (IntVector) rootOne.getVector(0);
+        rootOne.allocateNew();
+        appenderOne.set(0, 100);
+        appenderOne.set(1, 20);
+        rootOne.setRowCount(2);
+        IntVector appenderTwo = (IntVector) rootTwo.getVector(0);
+        rootTwo.allocateNew();
+        appenderTwo.set(0, 34);
+        appenderTwo.set(1, 75);
+        rootTwo.setRowCount(2);
+        result.allocateNew();
+        VectorSchemaRootAppender.append(result, rootOne, rootTwo);
+        System.out.print(result.contentToTSVString());
+    }
+
+.. testoutput::
+
+    column-one
+    100
+    20
+    34
+    75
+
 Compare Vectors for Field Equality
 ==================================