Java源码示例:org.apache.flink.formats.avro.typeutils.AvroTypeInfo

示例1
@Override
@SuppressWarnings("unchecked")
public TypeInformation<T> getProducedType() {
	if (SpecificRecord.class.isAssignableFrom(recordClazz)) {
		return new AvroTypeInfo(recordClazz);
	} else {
		return (TypeInformation<T>) new GenericRecordAvroTypeInfo(this.reader);
	}
}
 
示例2
@Override
@SuppressWarnings({"rawtypes", "unchecked"})
public <T> TypeInformation<T> createAvroTypeInfo(Class<T> type) {
	// we have to be raw here because we cannot have "<T extends SpecificRecordBase>" in
	// the interface of AvroUtils
	return new AvroTypeInfo(type);
}
 
示例3
/**
 * This test validates proper serialization with specific (generated POJO) types.
 */
@Test
public void testDeserializeToSpecificType() throws IOException {

	DatumReader<User> datumReader = new SpecificDatumReader<>(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);

		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());
	}
}
 
示例4
@Override
@SuppressWarnings("unchecked")
public TypeInformation<T> getProducedType() {
	if (SpecificRecord.class.isAssignableFrom(recordClazz)) {
		return new AvroTypeInfo(recordClazz);
	} else {
		return (TypeInformation<T>) new GenericRecordAvroTypeInfo(this.reader);
	}
}
 
示例5
@Override
@SuppressWarnings({"rawtypes", "unchecked"})
public <T> TypeInformation<T> createAvroTypeInfo(Class<T> type) {
	// we have to be raw here because we cannot have "<T extends SpecificRecordBase>" in
	// the interface of AvroUtils
	return new AvroTypeInfo(type);
}
 
示例6
/**
 * This test validates proper serialization with specific (generated POJO) types.
 */
@Test
public void testDeserializeToSpecificType() throws IOException {

	DatumReader<User> datumReader = new SpecificDatumReader<>(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);

		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());
	}
}
 
示例7
@Override
@SuppressWarnings("unchecked")
public TypeInformation<T> getProducedType() {
	if (SpecificRecord.class.isAssignableFrom(recordClazz)) {
		return new AvroTypeInfo(recordClazz);
	} else {
		return (TypeInformation<T>) new GenericRecordAvroTypeInfo(this.reader);
	}
}
 
示例8
@Override
@SuppressWarnings({"rawtypes", "unchecked"})
public <T> TypeInformation<T> createAvroTypeInfo(Class<T> type) {
	// we have to be raw here because we cannot have "<T extends SpecificRecordBase>" in
	// the interface of AvroUtils
	return new AvroTypeInfo(type);
}
 
示例9
/**
 * This test validates proper serialization with specific (generated POJO) types.
 */
@Test
public void testDeserializeToSpecificType() throws IOException {

	DatumReader<User> datumReader = new SpecificDatumReader<>(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);

		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());
	}
}