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


Java SpecificDatumReader類代碼示例

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


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

示例1: jsonReadWriteExample

import org.apache.avro.specific.SpecificDatumReader; //導入依賴的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.specific.SpecificDatumReader; //導入依賴的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: deserialize

import org.apache.avro.specific.SpecificDatumReader; //導入依賴的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

示例4: deserializeValue

import org.apache.avro.specific.SpecificDatumReader; //導入依賴的package包/類
private Event deserializeValue(byte[] value, boolean parseAsFlumeEvent) throws IOException {
  Event e;
  if (parseAsFlumeEvent) {
    ByteArrayInputStream in =
            new ByteArrayInputStream(value);
    decoder = DecoderFactory.get().directBinaryDecoder(in, decoder);
    if (!reader.isPresent()) {
      reader = Optional.of(
              new SpecificDatumReader<AvroFlumeEvent>(AvroFlumeEvent.class));
    }
    AvroFlumeEvent event = reader.get().read(null, decoder);
    e = EventBuilder.withBody(event.getBody().array(),
            toStringMap(event.getHeaders()));
  } else {
    e = EventBuilder.withBody(value, Collections.EMPTY_MAP);
  }
  return e;
}
 
開發者ID:moueimei,項目名稱:flume-release-1.7.0,代碼行數:19,代碼來源:KafkaChannel.java

示例5: deserUserCompile

import org.apache.avro.specific.SpecificDatumReader; //導入依賴的package包/類
public void deserUserCompile(){
	// Deserialize Users from disk
	DatumReader<User> userDatumReader = new SpecificDatumReader<User>(User.class);
	DataFileReader<User> dataFileReader = null;
	User user = null;
	try {
		dataFileReader = new DataFileReader<User>(
										new File("/Users/a/Desktop/tmp/users.avro"),
										userDatumReader);
		while (dataFileReader.hasNext()) {
			// Reuse user object by passing it to next(). This saves us from
			// allocating and garbage collecting many objects for files with
			// many items.
			user = dataFileReader.next(user);
			System.out.println(user);
		}
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
開發者ID:lrtdc,項目名稱:book_ldrtc,代碼行數:21,代碼來源:TestAvro.java

示例6: MemberInfoDynDeser

import org.apache.avro.specific.SpecificDatumReader; //導入依賴的package包/類
/**
 * 動態反序列:通過Schema文件進行動態反序列化操作
 * 
 * @throws IOException
 */
public void MemberInfoDynDeser() throws IOException {
    // 1.schema文件解析
    Parser parser = new Parser();
    Schema mSchema = parser.parse(this.getClass().getResourceAsStream("/Members.avsc"));

    // 2.構建數據讀對象
    DatumReader<GenericRecord> mGr = new SpecificDatumReader<GenericRecord>(mSchema);
    DataFileReader<GenericRecord> mDfr = new DataFileReader<GenericRecord>(new File("/Users/a/Desktop/tmp/members.avro"), mGr);
    // 3.從序列化文件中進行數據反序列化取出數據
    GenericRecord gr = null;
    while (mDfr.hasNext()) {
        gr = mDfr.next();
        System.err.println("deser data:" + gr.toString());
    }
    mDfr.close();
    System.out.println("Dyn Builder Ser Start Complete.");
}
 
開發者ID:lrtdc,項目名稱:book_ldrtc,代碼行數:23,代碼來源:MemberServerConsumer.java

示例7: EventReader

import org.apache.avro.specific.SpecificDatumReader; //導入依賴的package包/類
/**
 * Create a new Event Reader
 * @param in
 * @throws IOException
 */
@SuppressWarnings("deprecation")
public EventReader(DataInputStream in) throws IOException {
  this.in = in;
  this.version = in.readLine();

  Schema myschema = new SpecificData(Event.class.getClassLoader()).getSchema(Event.class);
  Schema.Parser parser = new Schema.Parser();
  this.schema = parser.parse(in.readLine());
  this.reader = new SpecificDatumReader(schema, myschema);
  if (EventWriter.VERSION.equals(version)) {
    this.decoder = DecoderFactory.get().jsonDecoder(schema, in);
  } else if (EventWriter.VERSION_BINARY.equals(version)) {
    this.decoder = DecoderFactory.get().binaryDecoder(in, null);
  } else {
    throw new IOException("Incompatible event log version: " + version);
  }
}
 
開發者ID:aliyun-beta,項目名稱:aliyun-oss-hadoop-fs,代碼行數:23,代碼來源:EventReader.java

示例8: fromBytes

import org.apache.avro.specific.SpecificDatumReader; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public static <T> T fromBytes( byte[] val, Schema schema
    , SpecificDatumReader<T> datumReader, T object)
throws IOException {
  Type type = schema.getType();
  switch (type) {
  case ENUM:
    String symbol = schema.getEnumSymbols().get(val[0]);
    return (T)Enum.valueOf(ReflectData.get().getClass(schema), symbol);
  case STRING:  return (T)new Utf8(toString(val));
  case BYTES:   return (T)ByteBuffer.wrap(val);
  case INT:     return (T)Integer.valueOf(bytesToVint(val));
  case LONG:    return (T)Long.valueOf(bytesToVlong(val));
  case FLOAT:   return (T)Float.valueOf(toFloat(val));
  case DOUBLE:  return (T)Double.valueOf(toDouble(val));
  case BOOLEAN: return (T)Boolean.valueOf(val[0] != 0);
  case RECORD:  //fall
  case MAP:
  case ARRAY:   return (T)IOUtils.deserialize(val, (SpecificDatumReader<SpecificRecord>)datumReader, schema, (SpecificRecord)object);
  default: throw new RuntimeException("Unknown type: "+type);
  }
}
 
開發者ID:jianglibo,項目名稱:gora-boot,代碼行數:23,代碼來源:ByteUtils.java

示例9: initialize

import org.apache.avro.specific.SpecificDatumReader; //導入依賴的package包/類
@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass,
    Properties properties) {
  setKeyClass(keyClass);
  setPersistentClass(persistentClass);
  if (this.beanFactory == null) {
    this.beanFactory = new BeanFactoryImpl<>(keyClass, persistentClass);
  }
  schema = this.beanFactory.getCachedPersistent().getSchema();
  fieldMap = AvroUtils.getFieldMap(schema);

  autoCreateSchema = DataStoreFactory.getAutoCreateSchema(properties, this);
  this.properties = properties;

  datumReader = new SpecificDatumReader<>(schema);
  datumWriter = new SpecificDatumWriter<>(schema);
}
 
開發者ID:jianglibo,項目名稱:gora-boot,代碼行數:18,代碼來源:DataStoreBase.java

示例10: initReader

import org.apache.avro.specific.SpecificDatumReader; //導入依賴的package包/類
private DataFileReader<E> initReader(FileInputSplit split) throws IOException {
	DatumReader<E> datumReader;

	if (org.apache.avro.generic.GenericRecord.class == avroValueType) {
		datumReader = new GenericDatumReader<E>();
	} else {
		datumReader = org.apache.avro.specific.SpecificRecordBase.class.isAssignableFrom(avroValueType)
			? new SpecificDatumReader<E>(avroValueType) : new ReflectDatumReader<E>(avroValueType);
	}
	if (LOG.isInfoEnabled()) {
		LOG.info("Opening split {}", split);
	}

	SeekableInput in = new FSDataInputStreamWrapper(stream, split.getPath().getFileSystem().getFileStatus(split.getPath()).getLen());
	DataFileReader<E> dataFileReader = (DataFileReader) DataFileReader.openReader(in, datumReader);

	if (LOG.isDebugEnabled()) {
		LOG.debug("Loaded SCHEMA: {}", dataFileReader.getSchema());
	}

	end = split.getStart() + split.getLength();
	recordsReadSinceLastSync = 0;
	return dataFileReader;
}
 
開發者ID:axbaretto,項目名稱:flink,代碼行數:25,代碼來源:AvroInputFormat.java

示例11: initializeAvro

import org.apache.avro.specific.SpecificDatumReader; //導入依賴的package包/類
private void initializeAvro() {
	final ClassLoader cl = Thread.currentThread().getContextClassLoader();

	if (SpecificRecord.class.isAssignableFrom(type)) {
		this.avroData = new SpecificData(cl);
		this.schema = this.avroData.getSchema(type);
		this.reader = new SpecificDatumReader<>(schema, schema, avroData);
		this.writer = new SpecificDatumWriter<>(schema, avroData);
	}
	else {
		final ReflectData reflectData = new ReflectData(cl);
		this.avroData = reflectData;
		this.schema = this.avroData.getSchema(type);
		this.reader = new ReflectDatumReader<>(schema, schema, reflectData);
		this.writer = new ReflectDatumWriter<>(schema, reflectData);
	}

	this.encoder = new DataOutputEncoder();
	this.decoder = new DataInputDecoder();
}
 
開發者ID:axbaretto,項目名稱:flink,代碼行數:21,代碼來源:AvroSerializer.java

示例12: testDeserializeToSpecificType

import org.apache.avro.specific.SpecificDatumReader; //導入依賴的package包/類
/**
 * This test validates proper serialization with specific (generated POJO) types.
 */
@Test
public void testDeserializeToSpecificType() throws IOException {

	DatumReader<User> datumReader = new SpecificDatumReader<User>(userSchema);

	try (FileReader<User> dataFileReader = DataFileReader.openReader(testFile, datumReader)) {
		User rec = dataFileReader.next();

		// check if record has been read correctly
		assertNotNull(rec);
		assertEquals("name not equal", TEST_NAME, rec.get("name").toString());
		assertEquals("enum not equal", TEST_ENUM_COLOR.toString(), rec.get("type_enum").toString());

		// now serialize it with our framework:
		ExecutionConfig ec = new ExecutionConfig();
		TypeInformation<User> te = TypeExtractor.createTypeInfo(User.class);

		Assert.assertEquals(AvroTypeInfo.class, te.getClass());
		TypeSerializer<User> tser = te.createSerializer(ec);

		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try (DataOutputViewStreamWrapper outView = new DataOutputViewStreamWrapper(out)) {
			tser.serialize(rec, outView);
		}

		User newRec;
		try (DataInputViewStreamWrapper inView = new DataInputViewStreamWrapper(
				new ByteArrayInputStream(out.toByteArray())))
		{
			newRec = tser.deserialize(inView);
		}

		// check if it is still the same
		assertNotNull(newRec);
		assertEquals("name not equal", TEST_NAME, newRec.getName().toString());
		assertEquals("enum not equal", TEST_ENUM_COLOR.toString(), newRec.getTypeEnum().toString());
	}
}
 
開發者ID:axbaretto,項目名稱:flink,代碼行數:42,代碼來源:AvroRecordInputFormatTest.java

示例13: fromByteArray

import org.apache.avro.specific.SpecificDatumReader; //導入依賴的package包/類
/**
 * From byte array.
 *
 * @param bytes the bytes
 * @return the iote 2 e request
 * @throws Exception the exception
 */
public Iote2eRequest fromByteArray( byte[] bytes ) throws Exception {

	try {
		if( this.datumReaderIote2eRequest == null ) {
			this.datumReaderIote2eRequest = new SpecificDatumReader<Iote2eRequest>(Iote2eRequest.getClassSchema());
		}
		this.binaryDecoder = DecoderFactory.get().binaryDecoder( bytes, this.binaryDecoder );
		this.iote2eRequest = this.datumReaderIote2eRequest.read (null, this.binaryDecoder );
		return this.iote2eRequest;
	} catch (Exception e) {
		logger.error(e.getMessage(),e);
		throw e;
	} finally {
	}
}
 
開發者ID:petezybrick,項目名稱:iote2e,代碼行數:23,代碼來源:Iote2eRequestReuseItem.java

示例14: fromByteArray

import org.apache.avro.specific.SpecificDatumReader; //導入依賴的package包/類
/**
 * From byte array.
 *
 * @param bytes the bytes
 * @return the iote 2 e result
 * @throws Exception the exception
 */
public Iote2eResult fromByteArray( byte[] bytes ) throws Exception {

	try {
		if( this.datumReaderIote2eResult == null ) {
			this.datumReaderIote2eResult = new SpecificDatumReader<Iote2eResult>(Iote2eResult.getClassSchema());
		}
		this.binaryDecoder = DecoderFactory.get().binaryDecoder(bytes, this.binaryDecoder);
		this.iote2eResult = this.datumReaderIote2eResult.read(null,this.binaryDecoder);
		return this.iote2eResult;
	} catch (Exception e) {
		logger.error(e.getMessage(),e);
		throw e;
	} finally {
	}
}
 
開發者ID:petezybrick,項目名稱:iote2e,代碼行數:23,代碼來源:Iote2eResultReuseItem.java

示例15: processList

import org.apache.avro.specific.SpecificDatumReader; //導入依賴的package包/類
/**
 * Process list.
 *
 * @param weathers the weathers
 * @throws Exception the exception
 */
public void processList(List<Weather> weathers) throws Exception {
	long before = System.currentTimeMillis();
	BinaryEncoder binaryEncoder = null;
	BinaryDecoder binaryDecoder = null;
	Weather weatherRead = null;
	for (Weather weather : weathers) {
		DatumWriter<Weather> datumWriterWeather = new SpecificDatumWriter<Weather>(Weather.getClassSchema());
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] byteData = null;
		try {
			binaryEncoder = EncoderFactory.get().binaryEncoder(baos, binaryEncoder);
			datumWriterWeather.write(weather, binaryEncoder);
			binaryEncoder.flush();
			byteData = baos.toByteArray();
		} finally {
			baos.close();
		}

		DatumReader<Weather> datumReaderWeather = new SpecificDatumReader<Weather>(Weather.getClassSchema());
		binaryDecoder = DecoderFactory.get().binaryDecoder(byteData, binaryDecoder);
		weatherRead = datumReaderWeather.read(weatherRead, binaryDecoder);
		// System.out.println("After Binary Read: " + weatherRead.toString());
	}
	System.out.println("size=" + weathers.size() + ", elapsed: " + (System.currentTimeMillis()-before));
}
 
開發者ID:petezybrick,項目名稱:iote2e,代碼行數:32,代碼來源:WeatherByteDemo.java


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