Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.parquet.cli.json;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.BinaryNode;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.MissingNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.NumericNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.avro.AvroRuntimeException;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.parquet.cli.util.RecordException;
import org.apache.parquet.cli.util.RuntimeIOException;
import org.apache.parquet.cli.util.Schemas;
public class AvroJson {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final JsonFactory FACTORY = new JsonFactory(MAPPER);
public static Iterator parser(final InputStream stream) {
try {
// Don't close the parser until the iterator has been consumed
JsonParser parser = FACTORY.createParser(stream);
return parser.readValuesAs(JsonNode.class);
} catch (IOException e) {
throw new RuntimeIOException("Cannot read from stream", e);
}
}
public static JsonNode parse(String json) {
return parse(json, JsonNode.class);
}
public static T parse(String json, Class returnType) {
try {
return MAPPER.readValue(json, returnType);
} catch (JsonParseException | JsonMappingException e) {
throw new IllegalArgumentException("Invalid JSON", e);
} catch (IOException e) {
throw new RuntimeIOException("Cannot initialize JSON parser", e);
}
}
public static JsonNode parse(InputStream json) {
return parse(json, JsonNode.class);
}
public static T parse(InputStream json, Class returnType) {
try {
return MAPPER.readValue(json, returnType);
} catch (JsonParseException | JsonMappingException e) {
throw new IllegalArgumentException("Invalid JSON stream", e);
} catch (IOException e) {
throw new RuntimeIOException("Cannot initialize JSON parser", e);
}
}
public static Object convertToAvro(GenericData model, JsonNode datum, Schema schema) {
if (datum == null) {
return null;
}
switch (schema.getType()) {
case RECORD:
RecordException.check(datum.isObject(), "Cannot convert non-object to record: %s", datum);
Object record = model.newRecord(null, schema);
for (Schema.Field field : schema.getFields()) {
model.setField(
record, field.name(), field.pos(), convertField(model, datum.get(field.name()), field));
}
return record;
case MAP:
RecordException.check(datum.isObject(), "Cannot convert non-object to map: %s", datum);
Map map = Maps.newLinkedHashMap();
Iterator> iter = datum.fields();
while (iter.hasNext()) {
Map.Entry entry = iter.next();
map.put(entry.getKey(), convertToAvro(model, entry.getValue(), schema.getValueType()));
}
return map;
case ARRAY:
RecordException.check(datum.isArray(), "Cannot convert to array: %s", datum);
List