Search in sources :

Example 1 with User

use of org.apache.flink.formats.avro.generated.User in project flink by apache.

the class AvroOutputFormatTest method output.

private void output(final AvroOutputFormat<User> outputFormat) throws IOException {
    outputFormat.configure(new Configuration());
    outputFormat.open(1, 1);
    for (int i = 0; i < 100; i++) {
        User user = new User();
        user.setName("testUser");
        user.setFavoriteNumber(1);
        user.setFavoriteColor("blue");
        user.setTypeBoolTest(true);
        user.setTypeArrayString(Collections.emptyList());
        user.setTypeArrayBoolean(Collections.emptyList());
        user.setTypeEnum(Colors.BLUE);
        user.setTypeMap(Collections.emptyMap());
        user.setTypeBytes(ByteBuffer.allocate(10));
        user.setTypeDate(LocalDate.parse("2014-03-01"));
        user.setTypeTimeMillis(LocalTime.parse("12:12:12"));
        user.setTypeTimeMicros(LocalTime.ofSecondOfDay(0).plus(123456L, ChronoUnit.MICROS));
        user.setTypeTimestampMillis(Instant.parse("2014-03-01T12:12:12.321Z"));
        user.setTypeTimestampMicros(Instant.ofEpochSecond(0).plus(123456L, ChronoUnit.MICROS));
        // 20.00
        user.setTypeDecimalBytes(ByteBuffer.wrap(BigDecimal.valueOf(2000, 2).unscaledValue().toByteArray()));
        // 20.00
        user.setTypeDecimalFixed(new Fixed2(BigDecimal.valueOf(2000, 2).unscaledValue().toByteArray()));
        outputFormat.writeRecord(user);
    }
    outputFormat.close();
}
Also used : User(org.apache.flink.formats.avro.generated.User) Configuration(org.apache.flink.configuration.Configuration) Fixed2(org.apache.flink.formats.avro.generated.Fixed2)

Example 2 with User

use of org.apache.flink.formats.avro.generated.User in project flink by apache.

the class AvroRecordInputFormatTest method testDeserializeToSpecificType.

/**
 * 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());
    }
}
Also used : User(org.apache.flink.formats.avro.generated.User) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) Test(org.junit.Test)

Example 3 with User

use of org.apache.flink.formats.avro.generated.User in project flink by apache.

the class AvroSerializerSnapshotTest method recordSerializedShouldBeDeserializeWithTheResortedSerializer.

@Test
public void recordSerializedShouldBeDeserializeWithTheResortedSerializer() throws IOException {
    // user is an avro generated test object.
    final User user = TestDataGenerator.generateRandomUser(new Random());
    final AvroSerializer<User> originalSerializer = new AvroSerializer<>(User.class);
    // 
    // first serialize the record
    // 
    ByteBuffer serializedUser = serialize(originalSerializer, user);
    // 
    // then restore a serializer from the snapshot
    // 
    TypeSerializer<User> restoredSerializer = originalSerializer.snapshotConfiguration().restoreSerializer();
    // 
    // now deserialize the user with the resorted serializer.
    // 
    User restoredUser = deserialize(restoredSerializer, serializedUser);
    assertThat(user, is(restoredUser));
}
Also used : User(org.apache.flink.formats.avro.generated.User) Random(java.util.Random) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with User

use of org.apache.flink.formats.avro.generated.User in project flink by apache.

the class AvroTypesITCase method testAvroToAvro.

@Test
public void testAvroToAvro() {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
    DataStream<User> ds = testData(env);
    Table t = tEnv.fromDataStream(ds, selectFields(ds));
    Table result = t.select($("*"));
    List<User> results = CollectionUtil.iteratorToList(DataStreamUtils.collect(tEnv.toAppendStream(result, User.class)));
    List<User> expected = Arrays.asList(USER_1, USER_2, USER_3);
    assertEquals(expected, results);
}
Also used : User(org.apache.flink.formats.avro.generated.User) Table(org.apache.flink.table.api.Table) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) StreamTableEnvironment(org.apache.flink.table.api.bridge.java.StreamTableEnvironment) Test(org.junit.Test)

Example 5 with User

use of org.apache.flink.formats.avro.generated.User in project flink by apache.

the class AvroTypesITCase method testAvroStringAccess.

@Test
public void testAvroStringAccess() {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
    DataStream<User> ds = testData(env);
    Table t = tEnv.fromDataStream(ds, selectFields(ds));
    Table result = t.select($("name"));
    List<Utf8> results = CollectionUtil.iteratorToList(result.execute().collect()).stream().map(row -> (Utf8) row.getField(0)).collect(Collectors.toList());
    String expected = "Charlie\n" + "Terminator\n" + "Whatever";
    TestBaseUtils.compareResultAsText(results, expected);
}
Also used : PojoTypeInfo(org.apache.flink.api.java.typeutils.PojoTypeInfo) StreamTableEnvironment(org.apache.flink.table.api.bridge.java.StreamTableEnvironment) Arrays(java.util.Arrays) HashMap(java.util.HashMap) Expression(org.apache.flink.table.expressions.Expression) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) Expressions(org.apache.flink.table.api.Expressions) BigDecimal(java.math.BigDecimal) Colors(org.apache.flink.formats.avro.generated.Colors) LocalTime(java.time.LocalTime) AbstractTestBase(org.apache.flink.test.util.AbstractTestBase) Utf8(org.apache.avro.util.Utf8) Fixed16(org.apache.flink.formats.avro.generated.Fixed16) Expressions.$(org.apache.flink.table.api.Expressions.$) DataStreamUtils(org.apache.flink.streaming.api.datastream.DataStreamUtils) Test(org.junit.Test) Table(org.apache.flink.table.api.Table) CollectionUtil(org.apache.flink.util.CollectionUtil) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) DataStream(org.apache.flink.streaming.api.datastream.DataStream) List(java.util.List) Fixed2(org.apache.flink.formats.avro.generated.Fixed2) ChronoUnit(java.time.temporal.ChronoUnit) Address(org.apache.flink.formats.avro.generated.Address) LocalDate(java.time.LocalDate) User(org.apache.flink.formats.avro.generated.User) Row(org.apache.flink.types.Row) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) TestBaseUtils(org.apache.flink.test.util.TestBaseUtils) User(org.apache.flink.formats.avro.generated.User) Table(org.apache.flink.table.api.Table) Utf8(org.apache.avro.util.Utf8) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) StreamTableEnvironment(org.apache.flink.table.api.bridge.java.StreamTableEnvironment) Test(org.junit.Test)

Aggregations

User (org.apache.flink.formats.avro.generated.User)28 Test (org.junit.Test)19 Path (org.apache.flink.core.fs.Path)12 Fixed16 (org.apache.flink.formats.avro.generated.Fixed16)8 HashMap (java.util.HashMap)7 ExecutionEnvironment (org.apache.flink.api.java.ExecutionEnvironment)7 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)6 Configuration (org.apache.flink.configuration.Configuration)6 AvroInputFormat (org.apache.flink.formats.avro.AvroInputFormat)6 Address (org.apache.flink.formats.avro.generated.Address)6 Fixed2 (org.apache.flink.formats.avro.generated.Fixed2)6 GroupReduceFunction (org.apache.flink.api.common.functions.GroupReduceFunction)5 FileInputSplit (org.apache.flink.core.fs.FileInputSplit)5 AvroRecordInputFormatTest (org.apache.flink.formats.avro.AvroRecordInputFormatTest)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)4 Table (org.apache.flink.table.api.Table)4 StreamTableEnvironment (org.apache.flink.table.api.bridge.java.StreamTableEnvironment)4