You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@orc.apache.org by om...@apache.org on 2017/04/25 21:22:42 UTC

orc git commit: Add a more advanced writing example to the core docs that includes a map column.

Repository: orc
Updated Branches:
  refs/heads/master ed56a9d26 -> aeacdc9be


Add a more advanced writing example to the core docs that includes
a map column.

Fixes #108

Signed-off-by: Owen O'Malley <om...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/orc/repo
Commit: http://git-wip-us.apache.org/repos/asf/orc/commit/aeacdc9b
Tree: http://git-wip-us.apache.org/repos/asf/orc/tree/aeacdc9b
Diff: http://git-wip-us.apache.org/repos/asf/orc/diff/aeacdc9b

Branch: refs/heads/master
Commit: aeacdc9bedca409997111a0cb33b34e8cc3f5c78
Parents: ed56a9d
Author: Philipp Blum <Ph...@webtrekk.com>
Authored: Mon Apr 10 18:26:06 2017 +0200
Committer: Owen O'Malley <om...@apache.org>
Committed: Tue Apr 25 14:21:43 2017 -0700

----------------------------------------------------------------------
 site/_docs/core-java.md | 67 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/orc/blob/aeacdc9b/site/_docs/core-java.md
----------------------------------------------------------------------
diff --git a/site/_docs/core-java.md b/site/_docs/core-java.md
index e0a0c61..7cbdfbe 100644
--- a/site/_docs/core-java.md
+++ b/site/_docs/core-java.md
@@ -210,6 +210,7 @@ public class MapColumnVector extends ColumnVector {
 
 ## Writing ORC Files
 
+### Simple Example
 To write an ORC file, you need to define the schema and use the
 [OrcFile]({{site.url}}/api/orc-core/index.html?org/apache/orc/OrcFile.html)
 class to create a
@@ -249,6 +250,72 @@ if (batch.size != 0) {
 writer.close();
 ~~~
 
+### Advanced Example
+
+The following example writes an ORC file with two integer
+columns and a map column. Each row's map has 5 elements with keys
+ranging from "&lt;row&gt;.0" to "&lt;row&gt;.4".
+
+~~~ java
+Path testFilePath = new Path("advanced-example.orc");
+Configuration conf = new Configuration();
+
+TypeDescription schema =
+    TypeDescription.fromString("struct<first:int," +
+                               "second:int,third:map<string,int>>");
+
+Writer writer =
+    OrcFile.createWriter(testFilePath,
+        OrcFile.writerOptions(conf).setSchema(schema));
+
+VectorizedRowBatch batch = schema.createRowBatch();
+LongColumnVector first = (LongColumnVector) batch.cols[0];
+LongColumnVector second = (LongColumnVector) batch.cols[1];
+
+//Define map. You need also to cast the key and value vectors
+MapColumnVector map = (MapColumnVector) batch.cols[2];
+BytesColumnVector mapKey = (BytesColumnVector) map.keys;
+LongColumnVector mapValue = (LongColumnVector) map.values;
+
+// Each map has 5 elements
+final int MAP_SIZE = 5;
+final int BATCH_SIZE = batch.getMaxSize();
+
+// Ensure the map is big enough
+mapKey.ensureSize(BATCH_SIZE * MAP_SIZE, false);
+mapValue.ensureSize(BATCH_SIZE * MAP_SIZE, false);
+
+// add 1500 rows to file
+for(int r=0; r < 1500; ++r) {
+  int row = batch.size++;
+
+  first.vector[row] = r;
+  second.vector[row] = r * 3;
+
+  map.offsets[row] = map.childCount;
+  map.lengths[row] = MAP_SIZE;
+  map.childCount += MAP_SIZE;
+
+  for (int mapElem = (int) map.offsets[row];
+       mapElem < map.offsets[row] + MAP_SIZE; ++mapElem) {
+    String key = "row " + r + "." + (mapElem - map.offsets[row]);
+    mapKey.setVal(mapElem, key.getBytes(StandardCharsets.UTF_8));
+    mapValue.vector[mapElem] = mapElem;
+  }
+  if (row == BATCH_SIZE - 1) {
+    writer.addRowBatch(batch);
+    batch.reset();
+  }
+}
+if (batch.size != 0) {
+  writer.addRowBatch(batch);
+  batch.reset();
+}
+writer.close();
+
+~~~
+
+
 ## Reading ORC Files
 
 To read ORC files, use the