當前位置: 首頁>>代碼示例>>Java>>正文


Java Decoder類代碼示例

本文整理匯總了Java中org.apache.avro.io.Decoder的典型用法代碼示例。如果您正苦於以下問題:Java Decoder類的具體用法?Java Decoder怎麽用?Java Decoder使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Decoder類屬於org.apache.avro.io包,在下文中一共展示了Decoder類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: jsonReadWriteExample

import org.apache.avro.io.Decoder; //導入依賴的package包/類
public void jsonReadWriteExample() throws IOException {
	Employee employee = Employee.newBuilder().setFirstName("Gaurav")
			.setLastName("Mazra").setSex(SEX.MALE).build();

	DatumWriter<Employee> employeeWriter = new SpecificDatumWriter<>(Employee.class);
	byte[] data;
	try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
		Encoder jsonEncoder = EncoderFactory.get().jsonEncoder(Employee.getClassSchema(), baos);
		employeeWriter.write(employee, jsonEncoder);
		jsonEncoder.flush();
		data = baos.toByteArray();
	}
	
	// serialized data
	System.out.println(new String(data));
	
	DatumReader<Employee> employeeReader = new SpecificDatumReader<>(Employee.class);
	Decoder decoder = DecoderFactory.get().jsonDecoder(Employee.getClassSchema(), new String(data));
	employee = employeeReader.read(null, decoder);
	//data after deserialization
	System.out.println(employee);
}
 
開發者ID:gauravrmazra,項目名稱:gauravbytes,代碼行數:23,代碼來源:AvroDatumExample.java

示例2: binaryReadWriteExample

import org.apache.avro.io.Decoder; //導入依賴的package包/類
public void binaryReadWriteExample() throws IOException {
	Employee employee = Employee.newBuilder().setFirstName("Gaurav")
			.setLastName("Mazra").setSex(SEX.MALE).build();

	DatumWriter<Employee> employeeWriter = new SpecificDatumWriter<>(Employee.class);
	byte[] data;
	try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
		Encoder binaryEncoder = EncoderFactory.get().binaryEncoder(baos, null);
		employeeWriter.write(employee, binaryEncoder);
		binaryEncoder.flush();
		data = baos.toByteArray();
	}
	
	// serialized data
	System.out.println(data);
	
	DatumReader<Employee> employeeReader = new SpecificDatumReader<>(Employee.class);
	Decoder binaryDecoder = DecoderFactory.get().binaryDecoder(data, null);
	employee = employeeReader.read(null, binaryDecoder);
	//data after deserialization
	System.out.println(employee);
}
 
開發者ID:gauravrmazra,項目名稱:gauravbytes,代碼行數:23,代碼來源:AvroDatumExample.java

示例3: checkNumeric

import org.apache.avro.io.Decoder; //導入依賴的package包/類
private void checkNumeric(String type, Object value) throws Exception {
	String def =
			"{\"type\":\"record\",\"name\":\"X\",\"fields\":"
					+"[{\"type\":\""+type+"\",\"name\":\"n\"}]}";
	Schema schema = Schema.parse(def);
	DatumReader<GenericRecord> reader =
			new GenericDatumReader<GenericRecord>(schema);
	
	String[] records = {"{\"n\":1}", "{\"n\":1.0}"};
	
	for (String record : records) {
		Decoder decoder = new ExtendedJsonDecoder(schema, record);
		GenericRecord r = reader.read(null, decoder);
		Assert.assertEquals(value, r.get("n"));
	}
}
 
開發者ID:Celos,項目名稱:avro-json-decoder,代碼行數:17,代碼來源:ExtendedJsonDecoderTest.java

示例4: deserialize

import org.apache.avro.io.Decoder; //導入依賴的package包/類
@SuppressWarnings("unchecked")
@Override
public T deserialize(String topic, byte[] data) {
	try {
		T result = null;

		if (data != null) {
			LOGGER.debug("data='{}'", DatatypeConverter.printHexBinary(data));

			DatumReader<GenericRecord> datumReader = new SpecificDatumReader<>(
					targetType.newInstance().getSchema());
			Decoder decoder = DecoderFactory.get().binaryDecoder(data, null);

			result = (T) datumReader.read(null, decoder);
			LOGGER.debug("deserialized data='{}'", result);
		}
		return result;
	} catch (Exception ex) {
		throw new SerializationException(
				"Can't deserialize data '" + Arrays.toString(data) + "' from topic '" + topic + "'", ex);
	}
}
 
開發者ID:italia,項目名稱:daf-replicate-ingestion,代碼行數:23,代碼來源:AvroDeserializer.java

示例5: serialize

import org.apache.avro.io.Decoder; //導入依賴的package包/類
/**
 * Converts the avro binary data to the json format
 */
@Override
public XContentBuilder serialize(Event event) {
    XContentBuilder builder = null;
    try {
        if (datumReader != null) {
            Decoder decoder = new DecoderFactory().binaryDecoder(event.getBody(), null);
            GenericRecord data = datumReader.read(null, decoder);
            logger.trace("Record in event " + data);
            XContentParser parser = XContentFactory
                    .xContent(XContentType.JSON)
                    .createParser(NamedXContentRegistry.EMPTY, data.toString());
            builder = jsonBuilder().copyCurrentStructure(parser);
            parser.close();
        } else {
            logger.error("Schema File is not configured");
        }
    } catch (IOException e) {
        logger.error("Exception in parsing avro format data but continuing serialization to process further records",
                e.getMessage(), e);
    }
    return builder;
}
 
開發者ID:cognitree,項目名稱:flume-elasticsearch-sink,代碼行數:26,代碼來源:AvroSerializer.java

示例6: fromBytes

import org.apache.avro.io.Decoder; //導入依賴的package包/類
public Object fromBytes(Schema schema, byte data[]) throws GoraException {
  Schema fromSchema = null;
  if (schema.getType() == Type.UNION) {
    try {
      Decoder decoder = DecoderFactory.get().binaryDecoder(data, null);
      int unionIndex = decoder.readIndex();
      List<Schema> possibleTypes = schema.getTypes();
      fromSchema = possibleTypes.get(unionIndex);
      Schema effectiveSchema = possibleTypes.get(unionIndex);
      if (effectiveSchema.getType() == Type.NULL) {
        decoder.readNull();
        return null;
      } else {
        data = decoder.readBytes(null).array();
      }
    } catch (IOException e) {
      LOG.error(e.getMessage());
      throw new GoraException("Error decoding union type: ", e);
    }
  } else {
    fromSchema = schema;
  }
  return fromBytes(encoder, fromSchema, data);
}
 
開發者ID:jianglibo,項目名稱:gora-boot,代碼行數:25,代碼來源:AccumuloStore.java

示例7: readBoolArray

import org.apache.avro.io.Decoder; //導入依賴的package包/類
/**
 * Reads a boolean[] from input
 * @throws IOException
 */
public static boolean[] readBoolArray(Decoder in) throws IOException {

  int length = in.readInt();
  boolean[] boolArr = new boolean[length];

  int byteArrLength = (int)Math.ceil(length / 8.0);
  byte[] byteArr = new byte[byteArrLength];
  in.readFixed(byteArr);

  int arrIndex = 0;
  byte b = 0;
  for(int i=0; i < length; i++) {
    if(i % 8 == 0) {
      b = byteArr[arrIndex++];
    }
    boolArr[i] = (b & 0x01) > 0;
    b >>= 1;
  }
  return boolArr;
}
 
開發者ID:jianglibo,項目名稱:gora-boot,代碼行數:25,代碼來源:IOUtils.java

示例8: processAvroMessage

import org.apache.avro.io.Decoder; //導入依賴的package包/類
/**
 * Processes an Avro Blob containing a single message and with no embedded
 * schema. This is the pattern when Avro objects are passed over messaging
 * infrastructure such as Apache Kafka.
 * 
 * @param avroMessage
 *            The Blob that holds the single Avro message object
 * @param avroKey
 *            The Blob that holds the single Avro key object (if passed)
 * @param outStream
 *            The stream to which the JSON string must be submitted
 * @param outTuple
 *            The tuple holding the JSON string
 * @param messageSchema
 *            The schema of the Avro messsage object
 * @param keySchema
 *            The schema of the Avro key object
 * @throws Exception
 */
private void processAvroMessage(Blob avroMessage, Blob avroKey, StreamingOutput<OutputTuple> outStream,
		OutputTuple outTuple, Schema messageSchema, Schema keySchema) throws Exception {
	// Deserialize message
	GenericDatumReader<GenericRecord> consumer = new GenericDatumReader<GenericRecord>(messageSchema);
	ByteArrayInputStream consumedByteArray = new ByteArrayInputStream(avroMessage.getData());
	Decoder consumedDecoder = DecoderFactory.get().binaryDecoder(consumedByteArray, null);
	GenericRecord consumedDatum = consumer.read(null, consumedDecoder);
	if (LOGGER.isTraceEnabled())
		LOGGER.log(TraceLevel.TRACE, "JSON representation of Avro message: " + consumedDatum.toString());
	outTuple.setString(outputJsonMessage, consumedDatum.toString());
	// Deserialize key (if specified)
	if (avroKey != null) {
		consumer = new GenericDatumReader<GenericRecord>(keySchema);
		consumedByteArray = new ByteArrayInputStream(avroKey.getData());
		consumedDecoder = DecoderFactory.get().binaryDecoder(consumedByteArray, null);
		consumedDatum = consumer.read(null, consumedDecoder);
		if (LOGGER.isTraceEnabled())
			LOGGER.log(TraceLevel.TRACE, "JSON representation of Avro key: " + consumedDatum.toString());
		if (outputJsonKey != null)
			outTuple.setString(outputJsonKey, consumedDatum.toString());
	}
	// Submit new tuple to output port 0
	outStream.submit(outTuple);
}
 
開發者ID:IBMStreams,項目名稱:streamsx.avro,代碼行數:44,代碼來源:AvroToJSON.java

示例9: decode

import org.apache.avro.io.Decoder; //導入依賴的package包/類
@Override
public <T> T decode(byte[] bytes, Class<T> type) throws IOException {
	Assert.notNull(bytes, "'bytes' cannot be null");
	Assert.notNull(bytes, "Class can not be null");
	ByteBuffer buf = ByteBuffer.wrap(bytes);
	byte[] payload = new byte[bytes.length-4];
	Integer schemaId = buf.getInt();
	buf.get(payload);
	Schema schema = schemaRegistryClient.fetch(schemaId);
	DatumReader reader = getDatumReader(type,schema);
	Decoder decoder = DecoderFactory.get().binaryDecoder(payload,null);
	return (T) reader.read(null,decoder);
}
 
開發者ID:viniciusccarvalho,項目名稱:schema-evolution-samples,代碼行數:14,代碼來源:AvroCodec.java

示例10: deserialize

import org.apache.avro.io.Decoder; //導入依賴的package包/類
@SuppressWarnings("unchecked")
@Override
public T deserialize(String topic, byte[] data) {
  try {
    T result = null;

    if (data != null) {
      LOGGER.debug("data='{}'", DatatypeConverter.printHexBinary(data));

      DatumReader<GenericRecord> datumReader =
          new SpecificDatumReader<>(targetType.newInstance().getSchema());
      Decoder decoder = DecoderFactory.get().binaryDecoder(data, null);

      result = (T) datumReader.read(null, decoder);
      LOGGER.debug("deserialized data='{}'", result);
    }
    return result;
  } catch (Exception ex) {
    throw new SerializationException(
        "Can't deserialize data '" + Arrays.toString(data) + "' from topic '" + topic + "'", ex);
  }
}
 
開發者ID:code-not-found,項目名稱:spring-kafka,代碼行數:23,代碼來源:AvroDeserializer.java

示例11: decodeRecord

import org.apache.avro.io.Decoder; //導入依賴的package包/類
@Override
protected GenericRecord decodeRecord(MessageAndOffset messageAndOffset) throws SchemaNotFoundException, IOException {
  byte[] payload = getBytes(messageAndOffset.message().payload());
  if (payload[0] != KafkaAvroSchemaRegistry.MAGIC_BYTE) {
    throw new RuntimeException(String.format("Unknown magic byte for partition %s", this.getCurrentPartition()));
  }

  byte[] schemaIdByteArray = Arrays.copyOfRange(payload, 1, 1 + KafkaAvroSchemaRegistry.SCHEMA_ID_LENGTH_BYTE);
  String schemaId = Hex.encodeHexString(schemaIdByteArray);
  Schema schema = null;
  schema = this.schemaRegistry.getSchemaById(schemaId);
  reader.get().setSchema(schema);
  Decoder binaryDecoder =
      DecoderFactory.get().binaryDecoder(payload, 1 + KafkaAvroSchemaRegistry.SCHEMA_ID_LENGTH_BYTE,
          payload.length - 1 - KafkaAvroSchemaRegistry.SCHEMA_ID_LENGTH_BYTE, null);
  try {
    GenericRecord record = reader.get().read(null, binaryDecoder);
    record = AvroUtils.convertRecordSchema(record, this.schema.get());
    return record;
  } catch (IOException e) {
    LOG.error(String.format("Error during decoding record for partition %s: ", this.getCurrentPartition()));
    throw e;
  }
}
 
開發者ID:Hanmourang,項目名稱:Gobblin,代碼行數:25,代碼來源:KafkaAvroExtractor.java

示例12: testEnums

import org.apache.avro.io.Decoder; //導入依賴的package包/類
@Test
public void testEnums() throws Exception {
  Schema schema = Enums.SCHEMA$;

  String avroJson = "{\"enum1\": \"X\", \"enum2\": {\"test.Enum2\": \"A\"}, \"enum3\": {\"null\": null}, \"enum4\": [{\"test.Enum4\": \"SAT\"}, {\"test.Enum4\": \"SUN\"}]}}";
  Decoder decoder = DecoderFactory.get().jsonDecoder(schema, avroJson);
  GenericDatumReader<Record> reader = new GenericDatumReader<Record>(schema);
  Record record1 = reader.read(null, decoder);

  String mongoJson = "{\"enum1\": \"X\", \"enum2\": \"A\", \"enum3\": null, \"enum4\": [\"SAT\", \"SUN\"]}}";
  BSONObject object = (BSONObject) JSON.parse(mongoJson);
  Record record2 = RecordConverter.toRecord(schema, object, getClass().getClassLoader());

  assertThat(record2, is(record1));
  assertThat(AvroHelper.toSimpleJson(schema, record2), is(AvroHelper.toSimpleJson(schema, record1)));
}
 
開發者ID:tfeng,項目名稱:toolbox,代碼行數:17,代碼來源:TestDocumentDecoder.java

示例13: testUnions

import org.apache.avro.io.Decoder; //導入依賴的package包/類
@Test
public void testUnions() throws Exception {
  Schema schema = Unions.SCHEMA$;

  String avroJson = "{\"union1\": {\"int\": 1}, \"union2\": {\"test.Union2\": {\"union21\": {\"long\": 2}}}, \"union3\": {\"array\": [{\"boolean\": true}, {\"boolean\": false}, {\"null\": null}]}, \"union4\": {\"map\": {\"a\": {\"string\": \"A\"}, \"b\": {\"string\": \"B\"}, \"c\": {\"string\": \"C\"}}}, \"union5\": {\"null\": null}, \"union6\": {\"null\": null}}";
  Decoder decoder = DecoderFactory.get().jsonDecoder(schema, avroJson);
  GenericDatumReader<Record> reader = new GenericDatumReader<Record>(schema);
  Record record1 = reader.read(null, decoder);

  String mongoJson = "{\"union1\": 1, \"union2\": {\"union21\": 2}, \"union3\": [true, false, null], \"union4\": {\"a\": \"A\", \"b\": \"B\", \"c\": \"C\"}, \"union5\": null, \"union6\": null}";
  DBObject object = (DBObject) JSON.parse(mongoJson);
  Record record2 = RecordConverter.toRecord(schema, object, getClass().getClassLoader());

  assertThat(record2, is(record1));
  assertThat(AvroHelper.toSimpleJson(schema, record2), is(AvroHelper.toSimpleJson(schema, record1)));
}
 
開發者ID:tfeng,項目名稱:toolbox,代碼行數:17,代碼來源:TestDocumentDecoder.java

示例14: upConvertPayload

import org.apache.avro.io.Decoder; //導入依賴的package包/類
/**
 * Convert the payload in the input record to a deserialized object with the latest schema
 *
 * @param inputRecord the input record
 * @return the schema'ed payload object
 */
protected P upConvertPayload(GenericRecord inputRecord) throws DataConversionException {
  try {
    Schema payloadSchema = getPayloadSchema(inputRecord);
    // Set writer schema
    latestPayloadReader.setSchema(payloadSchema);

    byte[] payloadBytes = getPayloadBytes(inputRecord);
    Decoder decoder = DecoderFactory.get().binaryDecoder(payloadBytes, null);

    // 'latestPayloadReader.read' will convert the record from 'payloadSchema' to the latest payload schema
    return latestPayloadReader.read(null, decoder);
  } catch (Exception e) {
    throw new DataConversionException(e);
  }
}
 
開發者ID:apache,項目名稱:incubator-gobblin,代碼行數:22,代碼來源:BaseEnvelopeSchemaConverter.java

示例15: parseJobSpec

import org.apache.avro.io.Decoder; //導入依賴的package包/類
@Override
public Collection<Either<JobSpec, URI>> parseJobSpec(byte[] message)
    throws IOException {

  InputStream is = new ByteArrayInputStream(message);
  this.versionWriter.readSchemaVersioningInformation(new DataInputStream(is));

  Decoder decoder = DecoderFactory.get().binaryDecoder(is, this.decoder.get());
  try {
    T decodedMessage = this.reader.get().read(null, decoder);
    return parseJobSpec(decodedMessage);
  } catch (AvroRuntimeException | IOException exc) {
    this.messageParseFailures.mark();
    if (this.messageParseFailures.getFiveMinuteRate() < 1) {
      log.warn("Unable to decode input message.", exc);
    } else {
      log.warn("Unable to decode input message.");
    }
    return Lists.newArrayList();
  }
}
 
開發者ID:apache,項目名稱:incubator-gobblin,代碼行數:22,代碼來源:KafkaAvroJobMonitor.java


注:本文中的org.apache.avro.io.Decoder類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。