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

dongjoon pushed a commit to branch branch-1.7
in repository https://gitbox.apache.org/repos/asf/orc.git


The following commit(s) were added to refs/heads/branch-1.7 by this push:
     new 42b0632  ORC-933: Add `AdvancedReader.java` example (#847)
42b0632 is described below

commit 42b0632be056b35dffaf33d4b34eb6c930e53ffb
Author: 林 <[email protected]>
AuthorDate: Wed Aug 11 13:20:00 2021 +0800

    ORC-933: Add `AdvancedReader.java` example (#847)
    
    ### What changes were proposed in this pull request?
    
    the main branch's example module has the AdvancedWriter,but doesn't contain 
the match reader,so I complete it.
    
    (cherry picked from commit f3159014fe5ff5b4c272630a88adc679850b957c)
    Signed-off-by: Dongjoon Hyun <[email protected]>
---
 .../org/apache/orc/examples/AdvancedReader.java    | 90 ++++++++++++++++++++++
 .../src/java/org/apache/orc/examples/Driver.java   |  3 +
 2 files changed, 93 insertions(+)

diff --git a/java/examples/src/java/org/apache/orc/examples/AdvancedReader.java 
b/java/examples/src/java/org/apache/orc/examples/AdvancedReader.java
new file mode 100644
index 0000000..61b951e
--- /dev/null
+++ b/java/examples/src/java/org/apache/orc/examples/AdvancedReader.java
@@ -0,0 +1,90 @@
+/*
+ * 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.
+ */
+package org.apache.orc.examples;
+
+import java.io.IOException;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.ql.exec.vector.BytesColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.LongColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.MapColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch;
+import org.apache.orc.OrcFile;
+import org.apache.orc.Reader;
+import org.apache.orc.RecordReader;
+import org.apache.orc.TypeDescription;
+
+/**
+ * This example shows how to read compound data types in ORC.
+ */
+public class AdvancedReader {
+
+  public static void main(Configuration conf, String[] args) throws 
IOException {
+    // Get the information from the file footer
+    Reader reader = OrcFile.createReader(new Path("advanced-example.orc"),
+        OrcFile.readerOptions(conf));
+    System.out.println("File schema: " + reader.getSchema());
+    System.out.println("Row count: " + reader.getNumberOfRows());
+
+    // Pick the schema we want to read using schema evolution
+    TypeDescription readSchema =
+        
TypeDescription.fromString("struct<first:int,second:int,third:map<string,int>>");
+    // Read the row data
+    VectorizedRowBatch batch = readSchema.createRowBatch();
+    RecordReader rowIterator = reader.rows(reader.options()
+        .schema(readSchema));
+    LongColumnVector x = (LongColumnVector) batch.cols[0];
+    LongColumnVector y = (LongColumnVector) batch.cols[1];
+    MapColumnVector z = (MapColumnVector) batch.cols[2];
+
+    /**
+     * cause the batch max size = 1024
+     * so at the row 1024 (from 0 begin,actually is row 1025)、the value is 
reset
+     * the final line is row 1499,and the map value from 2375 to 2379
+     */
+    while (rowIterator.nextBatch(batch)) {
+      for (int row = 0; row < batch.size; ++row) {
+        int xRow = x.isRepeating ? 0 : row;
+        int yRow = y.isRepeating ? 0 : row;
+        int zRow = z.isRepeating ? 0 : row;
+
+        System.out.println("x: " +
+            (x.noNulls || !x.isNull[xRow] ? x.vector[xRow] : null));
+        System.out.println("y: " + (y.noNulls || !y.isNull[yRow] ? 
y.vector[yRow] : null));
+
+        System.out.print("z: [");
+        long index = z.offsets[zRow];
+        for (long i = 0; i < z.lengths[zRow]; i++) {
+          final BytesColumnVector keys = (BytesColumnVector) z.keys;
+          final LongColumnVector values = (LongColumnVector) z.values;
+          String key = keys.toString((int) (index + i));
+          final long value = values.vector[(int) (index + i)];
+          System.out.print(key + ":" + value);
+          System.out.print(" ");
+        }
+        System.out.println("]");
+
+      }
+    }
+    rowIterator.close();
+  }
+
+  public static void main(String[] args) throws IOException {
+    main(new Configuration(), args);
+  }
+}
diff --git a/java/examples/src/java/org/apache/orc/examples/Driver.java 
b/java/examples/src/java/org/apache/orc/examples/Driver.java
index 87dfc5c..7d13a1f 100644
--- a/java/examples/src/java/org/apache/orc/examples/Driver.java
+++ b/java/examples/src/java/org/apache/orc/examples/Driver.java
@@ -74,6 +74,7 @@ public class Driver {
       System.err.println("   write - write a sample ORC file");
       System.err.println("   read - read a sample ORC file");
       System.err.println("   write2 - write a sample ORC file with a map");
+      System.err.println("   read2 - read a sample ORC file with a map");
       System.err.println();
       System.err.println("To get more help, provide -h to the command");
       System.exit(1);
@@ -92,6 +93,8 @@ public class Driver {
       CoreWriter.main(conf, options.commandArgs);
     } else if ("write2".equals(options.command)) {
       AdvancedWriter.main(conf, options.commandArgs);
+    } else if ("read2".equals(options.command)) {
+      AdvancedReader.main(conf, options.commandArgs);
     } else {
       System.err.println("Unknown subcommand: " + options.command);
       System.exit(1);

Reply via email to