Package org.apache.avro.generic

Examples of org.apache.avro.generic.IndexedRecord


  }

  public static class AvroIndexedRecordPartitioner extends Partitioner<Object, Object> {
    @Override
    public int getPartition(Object key, Object value, int numPartitions) {
      IndexedRecord record;
      if (key instanceof AvroWrapper) {
        record = (IndexedRecord) ((AvroWrapper) key).datum();
      } else if (key instanceof IndexedRecord) {
        record = (IndexedRecord) key;
      } else {
        throw new UnsupportedOperationException("Unknown avro key type: " + key);
      }
      return (Math.abs(record.get(0).hashCode()) & Integer.MAX_VALUE) % numPartitions;
    }
View Full Code Here


    avroPartitioner = new AvroIndexedRecordPartitioner();
  }

  @Test
  public void testGetPartition() {
    IndexedRecord indexedRecord = new MockIndexedRecord(3);
    AvroKey<IndexedRecord> avroKey = new AvroKey<IndexedRecord>(indexedRecord);

    assertEquals(3, avroPartitioner.getPartition(avroKey, new AvroValue<Object>(), 5));
    assertEquals(1, avroPartitioner.getPartition(avroKey, new AvroValue<Object>(), 2));
  }
View Full Code Here

    assertEquals(1, avroPartitioner.getPartition(avroKey, new AvroValue<Object>(), 2));
  }

  @Test
  public void testGetPartition_NegativeHashValue() {
    IndexedRecord indexedRecord = new MockIndexedRecord(-3);
    AvroKey<IndexedRecord> avroKey = new AvroKey<IndexedRecord>(indexedRecord);

    assertEquals(0, avroPartitioner.getPartition(avroKey, new AvroValue<Object>(), 5));
    assertEquals(1, avroPartitioner.getPartition(avroKey, new AvroValue<Object>(), 2));
  }
View Full Code Here

    assertEquals(1, avroPartitioner.getPartition(avroKey, new AvroValue<Object>(), 2));
  }

  @Test
  public void testGetPartition_IntegerMinValue() {
    IndexedRecord indexedRecord = new MockIndexedRecord(Integer.MIN_VALUE);
    AvroKey<IndexedRecord> avroKey = new AvroKey<IndexedRecord>(indexedRecord);

    assertEquals(0, avroPartitioner.getPartition(avroKey, new AvroValue<Object>(), Integer.MAX_VALUE));
  }
View Full Code Here

  }

  public static class AvroIndexedRecordPartitioner extends Partitioner<Object, Object> {
    @Override
    public int getPartition(Object key, Object value, int numPartitions) {
      IndexedRecord record;
      if (key instanceof AvroWrapper) {
        record = (IndexedRecord) ((AvroWrapper) key).datum();
      } else if (key instanceof IndexedRecord) {
        record = (IndexedRecord) key;
      } else {
        throw new UnsupportedOperationException("Unknown avro key type: " + key);
      }
      return (record.get(0).hashCode() & Integer.MAX_VALUE) % numPartitions;
    }
View Full Code Here

  }

  public static class AvroIndexedRecordPartitioner extends Partitioner<Object, Object> {
    @Override
    public int getPartition(Object key, Object value, int numPartitions) {
      IndexedRecord record;
      if (key instanceof AvroWrapper) {
        record = (IndexedRecord) ((AvroWrapper) key).datum();
      } else if (key instanceof IndexedRecord) {
        record = (IndexedRecord) key;
      } else {
        throw new UnsupportedOperationException("Unknown avro key type: " + key);
      }
      return (Math.abs(record.get(0).hashCode()) & Integer.MAX_VALUE) % numPartitions;
    }
View Full Code Here

  }

  public static class AvroIndexedRecordPartitioner<K, V> extends Partitioner<AvroKey<K>, AvroValue<V>> {
    @Override
    public int getPartition(AvroKey<K> key, AvroValue<V> value, int numPartitions) {
      IndexedRecord record = (IndexedRecord) key.datum();
      return (Math.abs(record.get(0).hashCode()) & Integer.MAX_VALUE) % numPartitions;
    }
View Full Code Here

      return jsonObject;
    }

    case RECORD: {
      final ObjectNode jsonObject = JSON_NODE_FACTORY.objectNode();
      final IndexedRecord record = (IndexedRecord) value;
      if (!record.getSchema().equals(schema)) {
        throw new IOException(String.format(
            "Avro schema specifies record type '%s' but got '%s'.",
            schema.getFullName(), record.getSchema().getFullName()));
      }
      for (Schema.Field field : schema.getFields()) {
        final Object fieldValue = record.get(field.pos());
        final JsonNode fieldNode = toJsonNode(fieldValue, field.schema());
        // Outputs the field only if its value differs from the field's default:
        if ((field.defaultValue() == null) || !fieldNode.equals(field.defaultValue())) {
          jsonObject.put(field.name(), fieldNode);
        }
View Full Code Here

  }

  public static class AvroIndexedRecordPartitioner<K, V> extends Partitioner<AvroKey<K>, AvroValue<V>> {
    @Override
    public int getPartition(AvroKey<K> key, AvroValue<V> value, int numPartitions) {
      IndexedRecord record = (IndexedRecord) key.datum();
      return (Math.abs(record.get(0).hashCode()) & Integer.MAX_VALUE) % numPartitions;
    }
View Full Code Here

    avroPartitioner = new AvroIndexedRecordPartitioner();
  }

  @Test
  public void testGetPartition() {
    IndexedRecord indexedRecord = new MockIndexedRecord(3);
    AvroKey<IndexedRecord> avroKey = new AvroKey<IndexedRecord>(indexedRecord);

    assertEquals(3, avroPartitioner.getPartition(avroKey, new AvroValue<Object>(), 5));
    assertEquals(1, avroPartitioner.getPartition(avroKey, new AvroValue<Object>(), 2));
  }
View Full Code Here

TOP

Related Classes of org.apache.avro.generic.IndexedRecord

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.