Search in sources :

Example 1 with ReflectionSchemaGenerator

use of io.cdap.cdap.internal.io.ReflectionSchemaGenerator 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 2 with ReflectionSchemaGenerator

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

the class RecordWithString method testCollection.

@Test
public void testCollection() throws UnsupportedTypeException, IOException {
    List<String> list = Lists.newArrayList("1", "2", "3");
    Schema sourceSchema = new ReflectionSchemaGenerator().generate(new TypeToken<List<String>>() {
    }.getType());
    Schema targetSchema = new ReflectionSchemaGenerator().generate(new TypeToken<Set<String>>() {
    }.getType());
    PipedOutputStream output = new PipedOutputStream();
    PipedInputStream input = new PipedInputStream(output);
    new ReflectionDatumWriter<List<String>>(sourceSchema).encode(list, new BinaryEncoder(output));
    Set<String> set = new ReflectionDatumReader<>(targetSchema, new TypeToken<Set<String>>() {
    }).read(new BinaryDecoder(input), sourceSchema);
    Assert.assertEquals(Sets.newHashSet("1", "2", "3"), set);
    targetSchema = new ReflectionSchemaGenerator().generate(String[].class);
    new ReflectionDatumWriter<List<String>>(sourceSchema).encode(list, new BinaryEncoder(output));
    String[] array = new ReflectionDatumReader<>(targetSchema, new TypeToken<String[]>() {
    }).read(new BinaryDecoder(input), sourceSchema);
    Assert.assertArrayEquals(new String[] { "1", "2", "3" }, array);
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ReflectionSchemaGenerator(io.cdap.cdap.internal.io.ReflectionSchemaGenerator) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.junit.Test)

Example 3 with ReflectionSchemaGenerator

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

the class SchemaTest method testPrimitiveArray.

@Test
public void testPrimitiveArray() throws UnsupportedTypeException {
    Schema schema = new ReflectionSchemaGenerator().generate(int[].class);
    Assert.assertEquals(Schema.arrayOf(Schema.of(Schema.Type.INT)), schema);
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) ReflectionSchemaGenerator(io.cdap.cdap.internal.io.ReflectionSchemaGenerator) Test(org.junit.Test)

Example 4 with ReflectionSchemaGenerator

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

the class SchemaTest method testSameRecordDifferentLevels.

@Test
public void testSameRecordDifferentLevels() throws UnsupportedTypeException, IOException {
    Schema actual = new ReflectionSchemaGenerator().generate(Node6.class);
    Assert.assertEquals(Node6.SCHEMA, actual);
    // check serialization and deserialization.
    Assert.assertEquals(Node6.SCHEMA, Schema.parseJson(actual.toString()));
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) ReflectionSchemaGenerator(io.cdap.cdap.internal.io.ReflectionSchemaGenerator) Test(org.junit.Test)

Example 5 with ReflectionSchemaGenerator

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

the class SchemaTest method testCachedJavaSerialization.

@Test
public void testCachedJavaSerialization() throws UnsupportedTypeException {
    Schema s1 = SchemaCache.intern(new ReflectionSchemaGenerator().generate(Node.class));
    Assert.assertSame(s1, SerializationUtils.clone(s1));
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) TextNode(org.codehaus.jackson.node.TextNode) IntNode(org.codehaus.jackson.node.IntNode) ReflectionSchemaGenerator(io.cdap.cdap.internal.io.ReflectionSchemaGenerator) Test(org.junit.Test)

Aggregations

ReflectionSchemaGenerator (io.cdap.cdap.internal.io.ReflectionSchemaGenerator)29 Test (org.junit.Test)26 Schema (io.cdap.cdap.api.data.schema.Schema)23 BinaryEncoder (io.cdap.cdap.common.io.BinaryEncoder)6 ApplicationClass (io.cdap.cdap.api.artifact.ApplicationClass)5 Table (io.cdap.cdap.api.dataset.table.Table)5 BinaryDecoder (io.cdap.cdap.common.io.BinaryDecoder)5 PluginClass (io.cdap.cdap.api.plugin.PluginClass)4 Id (io.cdap.cdap.common.id.Id)4 InspectionApp (io.cdap.cdap.internal.app.runtime.artifact.app.inspection.InspectionApp)4 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)4 PipedInputStream (java.io.PipedInputStream)4 PipedOutputStream (java.io.PipedOutputStream)4 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)3 Put (io.cdap.cdap.api.dataset.table.Put)3 Row (io.cdap.cdap.api.dataset.table.Row)3 ReflectionPutWriter (io.cdap.cdap.internal.io.ReflectionPutWriter)3 TypeRepresentation (io.cdap.cdap.internal.io.TypeRepresentation)3 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)3 TransactionExecutor (org.apache.tephra.TransactionExecutor)3