pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/apache/orc/commit/8f802a67a87a5163d7b759de4176a7b4eae2f7a1

rimer-b48faa60c69660fa.css" /> ORC-452: Support converting JSON object to MAP type · apache/orc@8f802a6 · GitHub
Skip to content

Commit 8f802a6

Browse files
stiga-huangwgtmac
authored andcommitted
ORC-452: Support converting JSON object to MAP type
Fixes #354 Signed-off-by: Gang Wu <gangwu@apache.org>
1 parent 10bb583 commit 8f802a6

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

java/tools/src/java/org/apache/orc/tools/convert/JsonReader.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.gson.JsonArray;
2121
import com.google.gson.JsonElement;
2222
import com.google.gson.JsonObject;
23+
import com.google.gson.JsonPrimitive;
2324
import com.google.gson.JsonStreamParser;
2425
import org.apache.hadoop.conf.Configuration;
2526
import org.apache.hadoop.fs.FSDataInputStream;
@@ -33,6 +34,7 @@
3334
import org.apache.hadoop.hive.ql.exec.vector.DoubleColumnVector;
3435
import org.apache.hadoop.hive.ql.exec.vector.ListColumnVector;
3536
import org.apache.hadoop.hive.ql.exec.vector.LongColumnVector;
37+
import org.apache.hadoop.hive.ql.exec.vector.MapColumnVector;
3638
import org.apache.hadoop.hive.ql.exec.vector.StructColumnVector;
3739
import org.apache.hadoop.hive.ql.exec.vector.TimestampColumnVector;
3840
import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch;
@@ -52,6 +54,7 @@
5254
import java.sql.Timestamp;
5355
import java.util.Iterator;
5456
import java.util.List;
57+
import java.util.Map;
5558
import java.util.zip.GZIPInputStream;
5659

5760
public class JsonReader implements RecordReader {
@@ -222,6 +225,40 @@ public void convert(JsonElement value, ColumnVector vect, int row) {
222225
}
223226
}
224227

228+
static class MapColumnConverter implements JsonConverter {
229+
private JsonConverter keyConverter;
230+
private JsonConverter valueConverter;
231+
232+
public MapColumnConverter(TypeDescription schema) {
233+
TypeDescription keyType = schema.getChildren().get(0);
234+
if (keyType.getCategory() != TypeDescription.Category.STRING)
235+
throw new IllegalArgumentException("JSON can only support MAP key in STRING type: " + schema);
236+
keyConverter = createConverter(keyType);
237+
valueConverter = createConverter(schema.getChildren().get(1));
238+
}
239+
240+
public void convert(JsonElement value, ColumnVector vect, int row) {
241+
if (value == null || value.isJsonNull()) {
242+
vect.noNulls = false;
243+
vect.isNull[row] = true;
244+
} else {
245+
MapColumnVector vector = (MapColumnVector) vect;
246+
JsonObject obj = value.getAsJsonObject();
247+
vector.lengths[row] = obj.entrySet().size();
248+
vector.offsets[row] = vector.childCount;
249+
vector.childCount += vector.lengths[row];
250+
vector.keys.ensureSize(vector.childCount, true);
251+
vector.values.ensureSize(vector.childCount, true);
252+
int cnt = 0;
253+
for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
254+
int offset = (int) vector.offsets[row] + cnt++;
255+
keyConverter.convert(new JsonPrimitive(entry.getKey()), vector.keys, offset);
256+
valueConverter.convert(entry.getValue(), vector.values, offset);
257+
}
258+
}
259+
}
260+
}
261+
225262
static JsonConverter createConverter(TypeDescription schema) {
226263
switch (schema.getCategory()) {
227264
case BYTE:
@@ -248,6 +285,8 @@ static JsonConverter createConverter(TypeDescription schema) {
248285
return new StructColumnConverter(schema);
249286
case LIST:
250287
return new ListColumnConverter(schema);
288+
case MAP:
289+
return new MapColumnConverter(schema);
251290
default:
252291
throw new IllegalArgumentException("Unhandled type " + schema);
253292
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy