org.apache.avro.generic.GenericArray Java Examples

The following examples show how to use org.apache.avro.generic.GenericArray. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestAvroDecoder.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testNestedLongArrayWithNulls()
{
    DecoderTestColumnHandle row = new DecoderTestColumnHandle(0, "row", new ArrayType(new ArrayType(BIGINT)), "array_field", null, null, false, false, false);
    Schema schema = SchemaBuilder.array().items().nullable().array().items().nullable().longType();
    List<List<Long>> data = Arrays.asList(
            ImmutableList.of(12L, 15L, 17L),
            ImmutableList.of(22L, 25L, 27L, 29L),
            null,
            Arrays.asList(3L, 5L, null, 6L));

    GenericArray<List<Long>> list = new GenericData.Array<>(schema, data);
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = buildAndDecodeColumn(row, "array_field", schema.toString(), list);

    checkArrayValue(decodedRow, row, list);
}
 
Example #2
Source File: ByteBufferBackedPrimitiveFloatList.java    From avro-util with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public int compareTo(GenericArray<Float> that) {
  cacheFromByteBuffer();
  if (that instanceof ByteBufferBackedPrimitiveFloatList) {
    ByteBufferBackedPrimitiveFloatList thatPrimitiveList = (ByteBufferBackedPrimitiveFloatList) that;
    if (this.size == thatPrimitiveList.size) {
      for (int i = 0; i < this.size; i++) {
        int compare = Float.compare(this.elements[i], thatPrimitiveList.elements[i]);
        if (compare != 0) {
          return compare;
        }
      }
      return 0;
    } else if (this.size > thatPrimitiveList.size) {
      return 1;
    } else {
      return -1;
    }
  } else {
    // Not our own type of primitive list, so we will delegate to the regular implementation, which will do boxing
    return GenericData.get().compare(this, that, this.getSchema());
  }
}
 
Example #3
Source File: PrimitiveArrayList.java    From avro-util with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public int compareTo(GenericArray<T> that) {
  if (isInstanceOfCorrectPrimitiveList(that)) {
    L thatPrimitiveList = (L) that;
    if (this.size == that.size()) {
      for (int i = 0; i < this.size; i++) {
        int compare = compareElementAtIndex(thatPrimitiveList, i);
        if (compare != 0) {
          return compare;
        }
      }
      return 0;
    } else if (this.size > that.size()) {
      return 1;
    } else {
      return -1;
    }
  } else {
    // Not our own type of primitive list, so we will delegate to the regular implementation, which will do boxing
    return GenericData.get().compare(this, that, this.getSchema());
  }
}
 
Example #4
Source File: Converter.java    From xml-avro with Apache License 2.0 6 votes vote down vote up
private static Element unwrapElement(GenericRecord record, Document doc) {
    String name = "" + record.get("name");
    Element el = doc.createElement(name);

    @SuppressWarnings("unchecked")
    GenericArray<GenericRecord> attrArray = (GenericArray<GenericRecord>) record.get("attributes");
    for (GenericRecord attrRecord : attrArray)
        el.setAttributeNode(unwrapAttr(attrRecord, doc));

    @SuppressWarnings("unchecked")
    GenericArray<Object> childArray = (GenericArray<Object>) record.get("children");
    for (Object childObj : childArray) {
        if (childObj instanceof GenericRecord)
            el.appendChild(unwrapElement((GenericRecord) childObj, doc));

        if (childObj instanceof Utf8)
            el.appendChild(doc.createTextNode("" + childObj));
    }

    return el;
}
 
Example #5
Source File: KafkaValueDeserializer.java    From kareldb with Apache License 2.0 6 votes vote down vote up
private NavigableMap<Long, VersionedValue> toValue(GenericArray<GenericRecord> array) {
    NavigableMap<Long, VersionedValue> map = new TreeMap<>();
    Schema recordSchema = avroSchema.getElementType();
    List<Schema.Field> fields = recordSchema.getFields();
    int size = fields.size();
    for (GenericRecord record : array) {
        Long version = (Long) record.get(0);
        Long commit = (Long) record.get(1);
        boolean deleted = (Boolean) record.get(2);
        Comparable[] row = new Comparable[size - 3];
        for (int i = 0; i < row.length; i++) {
            Schema schema = fields.get(i + 3).schema();
            Comparable value = (Comparable) record.get(i + 3);
            row[i] = AvroSchema.fromAvroValue(schema, value);
        }
        map.put(version, new VersionedValue(version, commit, deleted, row));
    }
    return map;
}
 
Example #6
Source File: AvroTupleWrapper.java    From spork with Apache License 2.0 6 votes vote down vote up
public static Object unionResolver(Object o) {
  if (o instanceof org.apache.avro.util.Utf8) {
    return o.toString();
  } else if (o instanceof IndexedRecord) {
    return new AvroTupleWrapper<IndexedRecord>((IndexedRecord) o);
  } else if (o instanceof GenericArray) {
    return new AvroBagWrapper<GenericData.Record>(
        (GenericArray<GenericData.Record>) o);
  } else if (o instanceof Map) {
    return new AvroMapWrapper((Map<CharSequence, Object>) o);
  } else if (o instanceof GenericData.Fixed) {
    return new DataByteArray(((GenericData.Fixed) o).bytes());
  } else if (o instanceof ByteBuffer) {
    return new DataByteArray(((ByteBuffer) o).array());
  } else if (o instanceof GenericEnumSymbol) {
    return o.toString();
  } else {
    return o;
  }
}
 
Example #7
Source File: TestAvroDecoder.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testArrayOfMapsWithNulls()
{
    Schema schema = SchemaBuilder.array()
            .items()
            .nullable().map()
            .values()
            .nullable().floatType();
    List<Map<String, Float>> data = Arrays.asList(
            buildMapFromKeysAndValues(ImmutableList.of("key1", "key2", "key3"), ImmutableList.of(1.3F, 2.3F, -.5F)),
            null,
            buildMapFromKeysAndValues(ImmutableList.of("key10", "key20", "key30"), ImmutableList.of(11.3F, 12.3F, -1.5F)),
            buildMapFromKeysAndValues(ImmutableList.of("key100", "key200", "key300"), Arrays.asList(111.3F, null, -11.5F)));

    DecoderTestColumnHandle row = new DecoderTestColumnHandle(0, "row", new ArrayType(REAL_MAP_TYPE), "array_field", null, null, false, false, false);
    GenericArray<Map<String, Float>> list = new GenericData.Array<>(schema, data);
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = buildAndDecodeColumn(row, "array_field", schema.toString(), list);
    checkArrayValues(getBlock(decodedRow, row), row.getType(), data);
}
 
Example #8
Source File: TestAvroDecoder.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testArrayOfMaps()
{
    Schema schema = SchemaBuilder.array()
            .items()
            .map()
            .values()
            .floatType();
    List<Map<String, Float>> data = ImmutableList.<Map<String, Float>>builder()
            .add(buildMapFromKeysAndValues(ImmutableList.of("key1", "key2", "key3"), ImmutableList.of(1.3F, 2.3F, -.5F)))
            .add(buildMapFromKeysAndValues(ImmutableList.of("key10", "key20", "key30"), ImmutableList.of(11.3F, 12.3F, -1.5F)))
            .build();

    DecoderTestColumnHandle row = new DecoderTestColumnHandle(0, "row", new ArrayType(REAL_MAP_TYPE), "array_field", null, null, false, false, false);
    GenericArray<Map<String, Float>> list = new GenericData.Array<>(schema, data);
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = buildAndDecodeColumn(row, "array_field", schema.toString(), list);
    checkArrayValues(getBlock(decodedRow, row), row.getType(), data);
}
 
Example #9
Source File: TestAvroDecoder.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeeplyNestedStringArrayWithNulls()
{
    Schema schema = SchemaBuilder.array()
            .items()
            .nullable().array()
            .items()
            .nullable().array()
            .items()
            .nullable().stringType();

    List<List<List<String>>> data = Arrays.asList(
            Arrays.asList(
                    ImmutableList.of("a", "bb", "ccc"),
                    null,
                    Arrays.asList("boo", "hoo", null, "hoo"),
                    ImmutableList.of("foo", "bar", "baz", "car")),
            null);

    GenericArray<List<List<String>>> list = new GenericData.Array<>(schema, data);
    DecoderTestColumnHandle row = new DecoderTestColumnHandle(0, "row", new ArrayType(new ArrayType(new ArrayType(VARCHAR))), "array_field", null, null, false, false, false);
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = buildAndDecodeColumn(row, "array_field", schema.toString(), list);

    checkArrayValue(decodedRow, row, list);
}
 
Example #10
Source File: TestAvroDecoder.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeeplyNestedStringArray()
{
    Schema schema = SchemaBuilder.array()
            .items()
            .array()
            .items()
            .array()
            .items()
            .stringType();

    List<List<List<String>>> data = ImmutableList.<List<List<String>>>builder()
            .add(ImmutableList.<List<String>>builder()
                    .add(ImmutableList.of("a", "bb", "ccc"))
                    .add(ImmutableList.of("foo", "bar", "baz", "car"))
                    .build())
            .build();

    GenericArray<List<List<String>>> list = new GenericData.Array<>(schema, data);
    DecoderTestColumnHandle row = new DecoderTestColumnHandle(0, "row", new ArrayType(new ArrayType(new ArrayType(VARCHAR))), "array_field", null, null, false, false, false);
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = buildAndDecodeColumn(row, "array_field", schema.toString(), list);

    checkArrayValue(decodedRow, row, list);
}
 
Example #11
Source File: TestAvroDecoder.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeeplyNestedLongArrayWithNulls()
{
    Schema schema = SchemaBuilder.array()
            .items()
            .nullable().array()
            .items()
            .nullable().array()
            .items()
            .nullable().longType();

    List<List<List<Long>>> data = Arrays.asList(
            Arrays.asList(
                    ImmutableList.of(12L, 15L, 17L),
                    null,
                    Arrays.asList(3L, 5L, null, 6L),
                    ImmutableList.of(22L, 25L, 27L, 29L)),
            null);

    GenericArray<List<List<Long>>> list = new GenericData.Array<>(schema, data);
    DecoderTestColumnHandle row = new DecoderTestColumnHandle(0, "row", new ArrayType(new ArrayType(new ArrayType(BIGINT))), "array_field", null, null, false, false, false);
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = buildAndDecodeColumn(row, "array_field", schema.toString(), list);

    checkArrayValue(decodedRow, row, list);
}
 
Example #12
Source File: TestAvroDecoder.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeeplyNestedLongArray()
{
    Schema schema = SchemaBuilder.array()
            .items()
            .array()
            .items()
            .array()
            .items()
            .longType();

    List<List<List<Long>>> data = ImmutableList.<List<List<Long>>>builder()
            .add(ImmutableList.<List<Long>>builder()
                    .add(ImmutableList.of(12L, 15L, 17L))
                    .add(ImmutableList.of(22L, 25L, 27L, 29L))
                    .build())
            .build();

    GenericArray<List<List<Long>>> list = new GenericData.Array<>(schema, data);
    DecoderTestColumnHandle row = new DecoderTestColumnHandle(0, "row", new ArrayType(new ArrayType(new ArrayType(BIGINT))), "array_field", null, null, false, false, false);
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = buildAndDecodeColumn(row, "array_field", schema.toString(), list);

    checkArrayValue(decodedRow, row, list);
}
 
Example #13
Source File: TestAvroDecoder.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testNestedStringArrayWithNulls()
{
    DecoderTestColumnHandle row = new DecoderTestColumnHandle(0, "row", new ArrayType(new ArrayType(VARCHAR)), "array_field", null, null, false, false, false);
    Schema schema = SchemaBuilder.array().items().nullable().array().items().nullable().stringType();
    List<List<String>> data = Arrays.asList(
            ImmutableList.of("a", "bb", "ccc"),
            ImmutableList.of("foo", "bar", "baz", "car"),
            null,
            Arrays.asList("boo", "hoo", null, "hoo"));

    GenericArray<List<String>> list = new GenericData.Array<>(schema, data);
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = buildAndDecodeColumn(row, "array_field", schema.toString(), list);

    checkArrayValue(decodedRow, row, list);
}
 
Example #14
Source File: AvroTupleWrapper.java    From spork with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object get(final int pos) throws ExecException {

  Schema s = avroObject.getSchema().getFields().get(pos).schema();
  Object o = avroObject.get(pos);

  switch(s.getType()) {
    case STRING:
      // unwrap avro UTF8 encoding
      return o.toString();
    case MAP:
      return new AvroMapWrapper((Map<CharSequence, Object>) o);
    case RECORD:
      return new AvroTupleWrapper<T>((T) o);
    case ENUM:
      return o.toString();
    case ARRAY:
      return new AvroBagWrapper<GenericData.Record>(
          (GenericArray<GenericData.Record>) o);
    case FIXED:
      return new DataByteArray(((GenericData.Fixed) o).bytes());
    case BYTES:
      return new DataByteArray(((ByteBuffer) o).array());
    case UNION:
      return unionResolver(o);
    default:
      return o;
  }
}
 
Example #15
Source File: AvroGenericRecordAccessor.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private List convertToList(String fieldName, GenericArray arr) {
  List ret = new ArrayList();
  for (int i = 0; i < arr.size(); i++) {
    ret.add(convertAvroToJava(fieldName + "." + String.valueOf(i), arr.get(i)));
  }

  return ret;
}
 
Example #16
Source File: AvroTupleWrapper.java    From spork with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
private long getMemorySize(final IndexedRecord r) {
  int total = 0;
  final int bitsPerByte = 8;
  for (Field f : r.getSchema().getFields()) {
    switch (f.schema().getType()) {
    case BOOLEAN:
    case ENUM:
    case INT:
      total += Integer.SIZE << bitsPerByte;
      break;
    case DOUBLE:
      total += Double.SIZE << bitsPerByte;
      break;
    case FLOAT:
      total += Float.SIZE << bitsPerByte;
      break;
    case NULL:
      break;
    case STRING:
      total += ((String) r.get(f.pos())).length()
         * (Character.SIZE << bitsPerByte);
      break;
    case BYTES:
      total += ((Byte[]) r.get(f.pos())).length;
      break;
    case RECORD:
      total += new AvroTupleWrapper(
          (IndexedRecord) r.get(f.pos())).getMemorySize();
      break;
    case ARRAY:
      total += new AvroBagWrapper(
          (GenericArray) r.get(f.pos())).getMemorySize();
      break;
    }
  }
  return total;
}
 
Example #17
Source File: Array_of_UNION_GenericDeserializer_585074122056792963_585074122056792963.java    From avro-util with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public List<IndexedRecord> deserialize(List<IndexedRecord> reuse, Decoder decoder)
    throws IOException
{
    List<IndexedRecord> array0 = null;
    long chunkLen0 = (decoder.readArrayStart());
    if (chunkLen0 > 0) {
        if ((reuse) instanceof List) {
            array0 = ((List)(reuse));
            array0 .clear();
        } else {
            array0 = new org.apache.avro.generic.GenericData.Array<IndexedRecord>(((int) chunkLen0), readerSchema);
        }
        do {
            for (int counter0 = 0; (counter0 <chunkLen0); counter0 ++) {
                Object arrayArrayElementReuseVar0 = null;
                if ((reuse) instanceof GenericArray) {
                    arrayArrayElementReuseVar0 = ((GenericArray)(reuse)).peek();
                }
                int unionIndex0 = (decoder.readIndex());
                if (unionIndex0 == 0) {
                    decoder.readNull();
                }
                if (unionIndex0 == 1) {
                    array0 .add(deserializerecord0(arrayArrayElementReuseVar0, (decoder)));
                }
            }
            chunkLen0 = (decoder.arrayNext());
        } while (chunkLen0 > 0);
    } else {
        array0 = new org.apache.avro.generic.GenericData.Array<IndexedRecord>(((int) chunkLen0), readerSchema);
    }
    return array0;
}
 
Example #18
Source File: AvroGenericRecordAccessor.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private Object convertAvroToJava(String fieldName, Object val) {
  if (val == null || val instanceof String || val instanceof Long || val instanceof Integer) {
    return val;
  }

  if (val instanceof Utf8) {
    return convertToString(fieldName, val);
  }

  if (val instanceof GenericArray) {
    return convertToList(fieldName, (GenericArray) val);
  }

  throw new IllegalArgumentException("Don't know how to parse object of type " + val.getClass().getCanonicalName());
}
 
Example #19
Source File: AvroStringFieldEncryptorConverterTest.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private GenericArray<String> buildTestArray() {
  Schema s = Schema.createArray(Schema.create(Schema.Type.STRING));
  GenericArray<String> arr = new GenericData.Array<>(3, s);
  arr.add("one");
  arr.add("two");
  arr.add("three");

  return arr;
}
 
Example #20
Source File: AvroStringFieldEncryptorConverterTest.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testEncryptionOfArray()
    throws SchemaConversionException, DataConversionException, IOException {
  AvroStringFieldEncryptorConverter converter = new AvroStringFieldEncryptorConverter();
  WorkUnitState wuState = new WorkUnitState();

  wuState.getJobState().setProp("converter.fieldsToEncrypt", "favorite_quotes");
  wuState.getJobState().setProp("converter.encrypt.algorithm", "insecure_shift");

  converter.init(wuState);
  GenericRecord inputRecord =
      getRecordFromFile(getClass().getClassLoader().getResource("fieldPickInput_arrays.avro").getPath());
  GenericArray origValues = (GenericArray) inputRecord.get("favorite_quotes");
  for (int i = 0; i < origValues.size(); i++) {
    origValues.set(i, origValues.get(i).toString());
  }

  Schema inputSchema = inputRecord.getSchema();
  Schema outputSchema = converter.convertSchema(inputSchema, wuState);

  Iterable<GenericRecord> recordIt = converter.convertRecord(outputSchema, inputRecord, wuState);
  GenericRecord encryptedRecord = recordIt.iterator().next();

  Assert.assertEquals(outputSchema, inputSchema);

  GenericArray<String> encryptedVals = (GenericArray<String>) encryptedRecord.get("favorite_quotes");
  List<String> decryptedValues = Lists.newArrayList();
  for (String encryptedValue: encryptedVals) {
    InsecureShiftCodec codec = new InsecureShiftCodec(Maps.<String, Object>newHashMap());
    InputStream in =
        codec.decodeInputStream(new ByteArrayInputStream(encryptedValue.getBytes(StandardCharsets.UTF_8)));
    byte[] decryptedValue = new byte[in.available()];
    in.read(decryptedValue);
    decryptedValues.add(new String(decryptedValue, StandardCharsets.UTF_8));
  }

  Assert.assertEquals(decryptedValues, origValues);
}
 
Example #21
Source File: AvroRecord.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
private <T> T doMap(final Class<T> expectedType, final org.apache.avro.Schema fieldSchema, final Object value) {
    if (Boolean.parseBoolean(readProp(fieldSchema, Schema.Type.DATETIME.name())) && Long.class.isInstance(value)
            && expectedType != Long.class) {
        return RECORD_CONVERTERS.coerce(expectedType, value, fieldSchema.getName());
    }
    if (IndexedRecord.class.isInstance(value) && (Record.class == expectedType || Object.class == expectedType)) {
        return expectedType.cast(new AvroRecord(IndexedRecord.class.cast(value)));
    }
    if (GenericArray.class.isInstance(value) && !GenericArray.class.isAssignableFrom(expectedType)) {
        final Class<?> itemType = expectedType == Collection.class ? Object.class : expectedType;
        return expectedType
                .cast(doMapCollection(itemType, Collection.class.cast(value), fieldSchema.getElementType()));
    }
    if (ByteBuffer.class.isInstance(value) && byte[].class == expectedType) {
        return expectedType.cast(ByteBuffer.class.cast(value).array());
    }
    if (org.joda.time.DateTime.class.isInstance(value) && ZonedDateTime.class == expectedType) {
        final long epochMilli = org.joda.time.DateTime.class.cast(value).getMillis();
        return expectedType.cast(ZonedDateTime.ofInstant(java.time.Instant.ofEpochMilli(epochMilli), UTC));
    }
    if (!expectedType.isInstance(value)) {
        if (Utf8.class.isInstance(value) && String.class == expectedType) {
            return expectedType.cast(value.toString());
        }
        return RECORD_CONVERTERS.coerce(expectedType, value, fieldSchema.getName());
    }
    if (Utf8.class.isInstance(value) && Object.class == expectedType) {
        return expectedType.cast(value.toString());
    }
    return expectedType.cast(value);
}
 
Example #22
Source File: Array_of_UNION_GenericDeserializer_585074122056792963_585074122056792963.java    From avro-util with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public List<IndexedRecord> deserialize(List<IndexedRecord> reuse, Decoder decoder)
    throws IOException
{
    List<IndexedRecord> array0 = null;
    long chunkLen0 = (decoder.readArrayStart());
    if (chunkLen0 > 0) {
        if ((reuse) instanceof List) {
            array0 = ((List)(reuse));
            array0 .clear();
        } else {
            array0 = new org.apache.avro.generic.GenericData.Array<IndexedRecord>(((int) chunkLen0), readerSchema);
        }
        do {
            for (int counter0 = 0; (counter0 <chunkLen0); counter0 ++) {
                Object arrayArrayElementReuseVar0 = null;
                if ((reuse) instanceof GenericArray) {
                    arrayArrayElementReuseVar0 = ((GenericArray)(reuse)).peek();
                }
                int unionIndex0 = (decoder.readIndex());
                if (unionIndex0 == 0) {
                    decoder.readNull();
                }
                if (unionIndex0 == 1) {
                    array0 .add(deserializerecord0(arrayArrayElementReuseVar0, (decoder)));
                }
            }
            chunkLen0 = (decoder.arrayNext());
        } while (chunkLen0 > 0);
    } else {
        array0 = new org.apache.avro.generic.GenericData.Array<IndexedRecord>(((int) chunkLen0), readerSchema);
    }
    return array0;
}
 
Example #23
Source File: Array_of_record_GenericDeserializer_1629046702287533603_1629046702287533603.java    From avro-util with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public List<IndexedRecord> deserialize(List<IndexedRecord> reuse, Decoder decoder)
    throws IOException
{
    List<IndexedRecord> array0 = null;
    long chunkLen0 = (decoder.readArrayStart());
    if (chunkLen0 > 0) {
        if ((reuse) instanceof List) {
            array0 = ((List)(reuse));
            array0 .clear();
        } else {
            array0 = new org.apache.avro.generic.GenericData.Array<IndexedRecord>(((int) chunkLen0), readerSchema);
        }
        do {
            for (int counter0 = 0; (counter0 <chunkLen0); counter0 ++) {
                Object arrayArrayElementReuseVar0 = null;
                if ((reuse) instanceof GenericArray) {
                    arrayArrayElementReuseVar0 = ((GenericArray)(reuse)).peek();
                }
                array0 .add(deserializerecord0(arrayArrayElementReuseVar0, (decoder)));
            }
            chunkLen0 = (decoder.arrayNext());
        } while (chunkLen0 > 0);
    } else {
        array0 = new org.apache.avro.generic.GenericData.Array<IndexedRecord>(((int) chunkLen0), readerSchema);
    }
    return array0;
}
 
Example #24
Source File: Array_of_UNION_GenericDeserializer_585074122056792963_585074122056792963.java    From avro-util with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public List<IndexedRecord> deserialize(List<IndexedRecord> reuse, Decoder decoder)
    throws IOException
{
    List<IndexedRecord> array0 = null;
    long chunkLen0 = (decoder.readArrayStart());
    if (chunkLen0 > 0) {
        if ((reuse) instanceof List) {
            array0 = ((List)(reuse));
            array0 .clear();
        } else {
            array0 = new org.apache.avro.generic.GenericData.Array<IndexedRecord>(((int) chunkLen0), readerSchema);
        }
        do {
            for (int counter0 = 0; (counter0 <chunkLen0); counter0 ++) {
                Object arrayArrayElementReuseVar0 = null;
                if ((reuse) instanceof GenericArray) {
                    arrayArrayElementReuseVar0 = ((GenericArray)(reuse)).peek();
                }
                int unionIndex0 = (decoder.readIndex());
                if (unionIndex0 == 0) {
                    decoder.readNull();
                }
                if (unionIndex0 == 1) {
                    array0 .add(deserializerecord0(arrayArrayElementReuseVar0, (decoder)));
                }
            }
            chunkLen0 = (decoder.arrayNext());
        } while (chunkLen0 > 0);
    } else {
        array0 = new org.apache.avro.generic.GenericData.Array<IndexedRecord>(((int) chunkLen0), readerSchema);
    }
    return array0;
}
 
Example #25
Source File: Array_of_record_GenericDeserializer_1629046702287533603_1629046702287533603.java    From avro-util with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public List<IndexedRecord> deserialize(List<IndexedRecord> reuse, Decoder decoder)
    throws IOException
{
    List<IndexedRecord> array0 = null;
    long chunkLen0 = (decoder.readArrayStart());
    if (chunkLen0 > 0) {
        if ((reuse) instanceof List) {
            array0 = ((List)(reuse));
            array0 .clear();
        } else {
            array0 = new org.apache.avro.generic.GenericData.Array<IndexedRecord>(((int) chunkLen0), readerSchema);
        }
        do {
            for (int counter0 = 0; (counter0 <chunkLen0); counter0 ++) {
                Object arrayArrayElementReuseVar0 = null;
                if ((reuse) instanceof GenericArray) {
                    arrayArrayElementReuseVar0 = ((GenericArray)(reuse)).peek();
                }
                array0 .add(deserializerecord0(arrayArrayElementReuseVar0, (decoder)));
            }
            chunkLen0 = (decoder.arrayNext());
        } while (chunkLen0 > 0);
    } else {
        array0 = new org.apache.avro.generic.GenericData.Array<IndexedRecord>(((int) chunkLen0), readerSchema);
    }
    return array0;
}
 
Example #26
Source File: TestAvroDecoder.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testNestedLongArray()
{
    DecoderTestColumnHandle row = new DecoderTestColumnHandle(0, "row", new ArrayType(new ArrayType(BIGINT)), "array_field", null, null, false, false, false);
    Schema schema = SchemaBuilder.array().items().array().items().longType();
    List<List<Long>> data = ImmutableList.<List<Long>>builder()
            .add(ImmutableList.of(12L, 15L, 17L))
            .add(ImmutableList.of(22L, 25L, 27L, 29L))
            .build();
    GenericArray<List<Long>> list = new GenericData.Array<>(schema, data);
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = buildAndDecodeColumn(row, "array_field", schema.toString(), list);

    checkArrayValue(decodedRow, row, list);
}
 
Example #27
Source File: Array_of_record_GenericDeserializer_1629046702287533603_1629046702287533603.java    From avro-util with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public List<IndexedRecord> deserialize(List<IndexedRecord> reuse, Decoder decoder)
    throws IOException
{
    List<IndexedRecord> array0 = null;
    long chunkLen0 = (decoder.readArrayStart());
    if (chunkLen0 > 0) {
        if ((reuse) instanceof List) {
            array0 = ((List)(reuse));
            array0 .clear();
        } else {
            array0 = new org.apache.avro.generic.GenericData.Array<IndexedRecord>(((int) chunkLen0), readerSchema);
        }
        do {
            for (int counter0 = 0; (counter0 <chunkLen0); counter0 ++) {
                Object arrayArrayElementReuseVar0 = null;
                if ((reuse) instanceof GenericArray) {
                    arrayArrayElementReuseVar0 = ((GenericArray)(reuse)).peek();
                }
                array0 .add(deserializerecord0(arrayArrayElementReuseVar0, (decoder)));
            }
            chunkLen0 = (decoder.arrayNext());
        } while (chunkLen0 > 0);
    } else {
        array0 = new org.apache.avro.generic.GenericData.Array<IndexedRecord>(((int) chunkLen0), readerSchema);
    }
    return array0;
}
 
Example #28
Source File: ByteBufferBackedPrimitiveFloatList.java    From avro-util with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public boolean equals(Object o) {
  cacheFromByteBuffer();
  if (o instanceof GenericArray) {
    return compareTo((GenericArray) o) == 0;
  } else {
    return super.equals(o);
  }
}
 
Example #29
Source File: AvroWrapper.java    From transport with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static StdData createStdData(Object avroData, Schema avroSchema) {
  switch (avroSchema.getType()) {
    case INT:
      return new AvroInteger((Integer) avroData);
    case LONG:
      return new AvroLong((Long) avroData);
    case BOOLEAN:
      return new AvroBoolean((Boolean) avroData);
    case STRING:
      return new AvroString((Utf8) avroData);
    case FLOAT:
      return new AvroFloat((Float) avroData);
    case DOUBLE:
      return new AvroDouble((Double) avroData);
    case BYTES:
      return new AvroBinary((ByteBuffer) avroData);
    case ARRAY:
      return new AvroArray((GenericArray<Object>) avroData, avroSchema);
    case MAP:
      return new AvroMap((Map<Object, Object>) avroData, avroSchema);
    case RECORD:
      return new AvroStruct((GenericRecord) avroData, avroSchema);
    case NULL:
      return null;
    default:
      throw new RuntimeException("Unrecognized Avro Schema: " + avroSchema.getClass());
  }
}
 
Example #30
Source File: KafkaValueDeserializer.java    From kareldb with Apache License 2.0 5 votes vote down vote up
@Override
public NavigableMap<Long, VersionedValue> deserialize(String topic, byte[] payload) throws SerializationException {
    if (payload == null) {
        return null;
    }
    try {
        ByteBuffer buffer = getByteBuffer(payload);
        int version = buffer.getInt();
        int length = buffer.limit() - 1 - VERSION_SIZE;
        int start = buffer.position() + buffer.arrayOffset();
        DatumReader<GenericArray<GenericRecord>> reader = readers.get(version);
        if (reader == null) {
            KafkaSchema schema = (KafkaSchema) table.getSchema();
            KafkaSchemaValue schemaValue = schema.getSchemaValue(table.getName(), version);
            Schema writerSchema = AvroUtils.parseSchema(schemaValue.getSchema());
            Pair<Schema, Schema> schemas = getKeyValueSchemas(writerSchema);
            Schema valueSchema = schemas.right;
            reader = new GenericDatumReader<>(valueSchema, avroSchema, KafkaTable.GENERIC);
            readers.put(version, reader);
        }
        GenericArray<GenericRecord> array = reader.read(
            null, decoderFactory.binaryDecoder(buffer.array(), start, length, null));
        return toValue(array);
    } catch (IOException | RuntimeException e) {
        // avro deserialization may throw AvroRuntimeException, NullPointerException, etc
        LOG.error("Error deserializing Avro value " + e.getMessage());
        throw new SerializationException("Error deserializing Avro value", e);
    }
}