Search in sources :

Example 6 with ReflectionDatumReader

use of io.cdap.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.

the class ASMDatumCodecTest method testRecord.

@Test
public void testRecord() throws IOException, UnsupportedTypeException {
    TypeToken<Record> type = new TypeToken<Record>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<Record> writer = getWriter(type);
    Record writeValue = new Record(10, "testing", ImmutableList.of("a", "b", "c"), TestEnum.VALUE2);
    writer.encode(writeValue, new BinaryEncoder(os));
    ReflectionDatumReader<Record> reader = new ReflectionDatumReader<>(getSchema(type), type);
    Record value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(writeValue, value);
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 7 with ReflectionDatumReader

use of io.cdap.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.

the class ASMDatumCodecTest method testEnum.

@Test
public void testEnum() throws UnsupportedTypeException, IOException {
    TypeToken<TestEnum> type = new TypeToken<TestEnum>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<TestEnum> writer = getWriter(type);
    BinaryEncoder encoder = new BinaryEncoder(os);
    writer.encode(TestEnum.VALUE1, encoder);
    writer.encode(TestEnum.VALUE4, encoder);
    writer.encode(TestEnum.VALUE3, encoder);
    ReflectionDatumReader<TestEnum> reader = new ReflectionDatumReader<>(getSchema(type), type);
    TestEnum value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(TestEnum.VALUE1, value);
    value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(TestEnum.VALUE4, value);
    value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(TestEnum.VALUE3, value);
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 8 with ReflectionDatumReader

use of io.cdap.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.

the class RecordWithString method testEnum.

@Test
public void testEnum() throws IOException, UnsupportedTypeException {
    PipedOutputStream output = new PipedOutputStream();
    PipedInputStream input = new PipedInputStream(output);
    Schema schema = new ReflectionSchemaGenerator().generate(TestEnum.class);
    ReflectionDatumWriter<TestEnum> writer = new ReflectionDatumWriter<>(schema);
    BinaryEncoder encoder = new BinaryEncoder(output);
    writer.encode(TestEnum.VALUE1, encoder);
    writer.encode(TestEnum.VALUE3, encoder);
    writer.encode(TestEnum.VALUE2, encoder);
    BinaryDecoder decoder = new BinaryDecoder(input);
    Schema readSchema = Schema.parseJson(schema.toString());
    ReflectionDatumReader<TestEnum> reader = new ReflectionDatumReader<>(readSchema, TypeToken.of(TestEnum.class));
    Assert.assertEquals(TestEnum.VALUE1, reader.read(decoder, readSchema));
    Assert.assertEquals(TestEnum.VALUE3, reader.read(decoder, readSchema));
    Assert.assertEquals(TestEnum.VALUE2, reader.read(decoder, readSchema));
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) Schema(io.cdap.cdap.api.data.schema.Schema) ReflectionDatumWriter(io.cdap.cdap.internal.io.ReflectionDatumWriter) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) ReflectionSchemaGenerator(io.cdap.cdap.internal.io.ReflectionSchemaGenerator) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 9 with ReflectionDatumReader

use of io.cdap.cdap.internal.io.ReflectionDatumReader in project cdap by caskdata.

the class MessagingMetricsCollectionServiceTest method testMessagingPublish.

@Test
public void testMessagingPublish() throws TopicNotFoundException {
    MetricsCollectionService collectionService = new MessagingMetricsCollectionService(CConfiguration.create(), messagingService, recordWriter);
    collectionService.startAndWait();
    // publish metrics for different context
    for (int i = 1; i <= 3; i++) {
        collectionService.getContext(ImmutableMap.of("tag", "" + i)).increment("processed", i);
    }
    collectionService.stopAndWait();
    // <Context, metricName, value>
    Table<String, String, Long> expected = HashBasedTable.create();
    expected.put("tag.1", "processed", 1L);
    expected.put("tag.2", "processed", 2L);
    expected.put("tag.3", "processed", 3L);
    ReflectionDatumReader<MetricValues> recordReader = new ReflectionDatumReader<>(schema, metricValueType);
    assertMetricsFromMessaging(schema, recordReader, expected);
}
Also used : MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) MetricValues(io.cdap.cdap.api.metrics.MetricValues) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) Test(org.junit.Test)

Example 10 with ReflectionDatumReader

use of io.cdap.cdap.internal.io.ReflectionDatumReader in project cdap by cdapio.

the class MessagingMetricsCollectionServiceTest method testMessagingPublish.

@Test
public void testMessagingPublish() throws TopicNotFoundException {
    MetricsCollectionService collectionService = new MessagingMetricsCollectionService(CConfiguration.create(), messagingService, recordWriter);
    collectionService.startAndWait();
    // publish metrics for different context
    for (int i = 1; i <= 3; i++) {
        collectionService.getContext(ImmutableMap.of("tag", "" + i)).increment("processed", i);
    }
    collectionService.stopAndWait();
    // <Context, metricName, value>
    Table<String, String, Long> expected = HashBasedTable.create();
    expected.put("tag.1", "processed", 1L);
    expected.put("tag.2", "processed", 2L);
    expected.put("tag.3", "processed", 3L);
    ReflectionDatumReader<MetricValues> recordReader = new ReflectionDatumReader<>(schema, metricValueType);
    assertMetricsFromMessaging(schema, recordReader, expected);
}
Also used : MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) MetricValues(io.cdap.cdap.api.metrics.MetricValues) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) Test(org.junit.Test)

Aggregations

ReflectionDatumReader (io.cdap.cdap.internal.io.ReflectionDatumReader)36 Test (org.junit.Test)36 BinaryDecoder (io.cdap.cdap.common.io.BinaryDecoder)34 BinaryEncoder (io.cdap.cdap.common.io.BinaryEncoder)34 PipedInputStream (java.io.PipedInputStream)32 PipedOutputStream (java.io.PipedOutputStream)32 TypeToken (com.google.common.reflect.TypeToken)30 ImmutableList (com.google.common.collect.ImmutableList)8 List (java.util.List)8 Schema (io.cdap.cdap.api.data.schema.Schema)4 ReflectionDatumWriter (io.cdap.cdap.internal.io.ReflectionDatumWriter)4 ReflectionSchemaGenerator (io.cdap.cdap.internal.io.ReflectionSchemaGenerator)4 ImmutableMap (com.google.common.collect.ImmutableMap)2 MetricValues (io.cdap.cdap.api.metrics.MetricValues)2 MetricsCollectionService (io.cdap.cdap.api.metrics.MetricsCollectionService)2 TypeRepresentation (io.cdap.cdap.internal.io.TypeRepresentation)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 URI (java.net.URI)2 Map (java.util.Map)2