org.apache.flink.formats.avro.typeutils.AvroTypeInfo Java Examples

The following examples show how to use org.apache.flink.formats.avro.typeutils.AvroTypeInfo. 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: AvroDeserializationSchema.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public TypeInformation<T> getProducedType() {
	if (SpecificRecord.class.isAssignableFrom(recordClazz)) {
		return new AvroTypeInfo(recordClazz);
	} else {
		return (TypeInformation<T>) new GenericRecordAvroTypeInfo(this.reader);
	}
}
 
Example #2
Source File: AvroKryoSerializerUtils.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #3
Source File: AvroRecordInputFormatTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
	}
}
 
Example #4
Source File: AvroDeserializationSchema.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public TypeInformation<T> getProducedType() {
	if (SpecificRecord.class.isAssignableFrom(recordClazz)) {
		return new AvroTypeInfo(recordClazz);
	} else {
		return (TypeInformation<T>) new GenericRecordAvroTypeInfo(this.reader);
	}
}
 
Example #5
Source File: AvroKryoSerializerUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #6
Source File: AvroRecordInputFormatTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
	}
}
 
Example #7
Source File: AvroDeserializationSchema.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public TypeInformation<T> getProducedType() {
	if (SpecificRecord.class.isAssignableFrom(recordClazz)) {
		return new AvroTypeInfo(recordClazz);
	} else {
		return (TypeInformation<T>) new GenericRecordAvroTypeInfo(this.reader);
	}
}
 
Example #8
Source File: AvroKryoSerializerUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #9
Source File: AvroRecordInputFormatTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
	}
}