Java Code Examples for org.apache.flink.api.java.typeutils.runtime.kryo.Serializers#recursivelyRegisterType()
The following examples show how to use
org.apache.flink.api.java.typeutils.runtime.kryo.Serializers#recursivelyRegisterType() .
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: AvroRecordInputFormatTest.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
/** * Test if the Flink serialization is able to properly process GenericData.Record types. * Usually users of Avro generate classes (POJOs) from Avro schemas. * However, if generated classes are not available, one can also use GenericData.Record. * It is an untyped key-value record which is using a schema to validate the correctness of the data. * * <p>It is not recommended to use GenericData.Record with Flink. Use generated POJOs instead. */ @Test public void testDeserializeToGenericType() throws IOException { DatumReader<GenericData.Record> datumReader = new GenericDatumReader<>(userSchema); try (FileReader<GenericData.Record> dataFileReader = DataFileReader.openReader(testFile, datumReader)) { // initialize Record by reading it from disk (that's easier than creating it by hand) GenericData.Record rec = new GenericData.Record(userSchema); dataFileReader.next(rec); // 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()); assertEquals(null, rec.get("type_long_test")); // it is null for the first record. // now serialize it with our framework: TypeInformation<GenericData.Record> te = TypeExtractor.createTypeInfo(GenericData.Record.class); ExecutionConfig ec = new ExecutionConfig(); assertEquals(GenericTypeInfo.class, te.getClass()); Serializers.recursivelyRegisterType(te.getTypeClass(), ec, new HashSet<>()); TypeSerializer<GenericData.Record> tser = te.createSerializer(ec); assertEquals(1, ec.getDefaultKryoSerializerClasses().size()); assertTrue( ec.getDefaultKryoSerializerClasses().containsKey(Schema.class) && ec.getDefaultKryoSerializerClasses().get(Schema.class).equals(AvroKryoSerializerUtils.AvroSchemaSerializer.class)); ByteArrayOutputStream out = new ByteArrayOutputStream(); try (DataOutputViewStreamWrapper outView = new DataOutputViewStreamWrapper(out)) { tser.serialize(rec, outView); } GenericData.Record newRec; try (DataInputViewStreamWrapper inView = new DataInputViewStreamWrapper( new ByteArrayInputStream(out.toByteArray()))) { newRec = tser.deserialize(inView); } // check if it is still the same assertNotNull(newRec); assertEquals("enum not equal", TEST_ENUM_COLOR.toString(), newRec.get("type_enum").toString()); assertEquals("name not equal", TEST_NAME, newRec.get("name").toString()); assertEquals(null, newRec.get("type_long_test")); } }
Example 2
Source File: AvroRecordInputFormatTest.java From flink with Apache License 2.0 | 4 votes |
/** * Test if the Flink serialization is able to properly process GenericData.Record types. * Usually users of Avro generate classes (POJOs) from Avro schemas. * However, if generated classes are not available, one can also use GenericData.Record. * It is an untyped key-value record which is using a schema to validate the correctness of the data. * * <p>It is not recommended to use GenericData.Record with Flink. Use generated POJOs instead. */ @Test public void testDeserializeToGenericType() throws IOException { DatumReader<GenericData.Record> datumReader = new GenericDatumReader<>(userSchema); try (FileReader<GenericData.Record> dataFileReader = DataFileReader.openReader(testFile, datumReader)) { // initialize Record by reading it from disk (that's easier than creating it by hand) GenericData.Record rec = new GenericData.Record(userSchema); dataFileReader.next(rec); // 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()); assertEquals(null, rec.get("type_long_test")); // it is null for the first record. // now serialize it with our framework: TypeInformation<GenericData.Record> te = TypeExtractor.createTypeInfo(GenericData.Record.class); ExecutionConfig ec = new ExecutionConfig(); assertEquals(GenericTypeInfo.class, te.getClass()); Serializers.recursivelyRegisterType(te.getTypeClass(), ec, new HashSet<>()); TypeSerializer<GenericData.Record> tser = te.createSerializer(ec); assertEquals(1, ec.getDefaultKryoSerializerClasses().size()); assertTrue( ec.getDefaultKryoSerializerClasses().containsKey(Schema.class) && ec.getDefaultKryoSerializerClasses().get(Schema.class).equals(AvroKryoSerializerUtils.AvroSchemaSerializer.class)); ByteArrayOutputStream out = new ByteArrayOutputStream(); try (DataOutputViewStreamWrapper outView = new DataOutputViewStreamWrapper(out)) { tser.serialize(rec, outView); } GenericData.Record newRec; try (DataInputViewStreamWrapper inView = new DataInputViewStreamWrapper( new ByteArrayInputStream(out.toByteArray()))) { newRec = tser.deserialize(inView); } // check if it is still the same assertNotNull(newRec); assertEquals("enum not equal", TEST_ENUM_COLOR.toString(), newRec.get("type_enum").toString()); assertEquals("name not equal", TEST_NAME, newRec.get("name").toString()); assertEquals(null, newRec.get("type_long_test")); } }
Example 3
Source File: AvroRecordInputFormatTest.java From flink with Apache License 2.0 | 4 votes |
/** * Test if the Flink serialization is able to properly process GenericData.Record types. * Usually users of Avro generate classes (POJOs) from Avro schemas. * However, if generated classes are not available, one can also use GenericData.Record. * It is an untyped key-value record which is using a schema to validate the correctness of the data. * * <p>It is not recommended to use GenericData.Record with Flink. Use generated POJOs instead. */ @Test public void testDeserializeToGenericType() throws IOException { DatumReader<GenericData.Record> datumReader = new GenericDatumReader<>(userSchema); try (FileReader<GenericData.Record> dataFileReader = DataFileReader.openReader(testFile, datumReader)) { // initialize Record by reading it from disk (that's easier than creating it by hand) GenericData.Record rec = new GenericData.Record(userSchema); dataFileReader.next(rec); // 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()); assertEquals(null, rec.get("type_long_test")); // it is null for the first record. // now serialize it with our framework: TypeInformation<GenericData.Record> te = TypeExtractor.createTypeInfo(GenericData.Record.class); ExecutionConfig ec = new ExecutionConfig(); assertEquals(GenericTypeInfo.class, te.getClass()); Serializers.recursivelyRegisterType(te.getTypeClass(), ec, new HashSet<>()); TypeSerializer<GenericData.Record> tser = te.createSerializer(ec); assertEquals(1, ec.getDefaultKryoSerializerClasses().size()); assertTrue( ec.getDefaultKryoSerializerClasses().containsKey(Schema.class) && ec.getDefaultKryoSerializerClasses().get(Schema.class).equals(AvroKryoSerializerUtils.AvroSchemaSerializer.class)); ByteArrayOutputStream out = new ByteArrayOutputStream(); try (DataOutputViewStreamWrapper outView = new DataOutputViewStreamWrapper(out)) { tser.serialize(rec, outView); } GenericData.Record newRec; try (DataInputViewStreamWrapper inView = new DataInputViewStreamWrapper( new ByteArrayInputStream(out.toByteArray()))) { newRec = tser.deserialize(inView); } // check if it is still the same assertNotNull(newRec); assertEquals("enum not equal", TEST_ENUM_COLOR.toString(), newRec.get("type_enum").toString()); assertEquals("name not equal", TEST_NAME, newRec.get("name").toString()); assertEquals(null, newRec.get("type_long_test")); } }