Search in sources :

Example 61 with Schema

use of co.cask.cdap.api.data.schema.Schema in project cdap by caskdata.

the class SchemaTest method testAvroRecordSchema.

@Test
public void testAvroRecordSchema() throws Exception {
    org.apache.avro.Schema avroStringSchema = org.apache.avro.Schema.create(org.apache.avro.Schema.Type.STRING);
    org.apache.avro.Schema avroIntSchema = org.apache.avro.Schema.create(org.apache.avro.Schema.Type.INT);
    org.apache.avro.Schema schema = org.apache.avro.Schema.createRecord("UserInfo", "Describes user information", "org.example.schema", false);
    List<org.apache.avro.Schema.Field> fields = new ArrayList<>();
    org.apache.avro.Schema.Field field = new org.apache.avro.Schema.Field("username", avroStringSchema, "Field represents username", new TextNode("unknown"));
    fields.add(field);
    field = new org.apache.avro.Schema.Field("age", avroIntSchema, "Field represents age of user", new IntNode(-1));
    fields.add(field);
    schema.setFields(fields);
    Schema parsedSchema = Schema.parseJson(schema.toString());
    Assert.assertTrue("UserInfo".equals(parsedSchema.getRecordName()));
    Assert.assertEquals(2, parsedSchema.getFields().size());
    Schema.Field parsedField = parsedSchema.getFields().get(0);
    Assert.assertTrue("username".equals(parsedField.getName()));
    Assert.assertTrue("STRING".equals(parsedField.getSchema().getType().toString()));
    parsedField = parsedSchema.getFields().get(1);
    Assert.assertTrue("age".equals(parsedField.getName()));
    Assert.assertTrue("INT".equals(parsedField.getSchema().getType().toString()));
}
Also used : IntNode(org.codehaus.jackson.node.IntNode) Schema(co.cask.cdap.api.data.schema.Schema) ArrayList(java.util.ArrayList) TextNode(org.codehaus.jackson.node.TextNode) Test(org.junit.Test)

Example 62 with Schema

use of co.cask.cdap.api.data.schema.Schema in project cdap by caskdata.

the class StreamEventCodecTest method testEncodeDecodeWithDatumDecoder.

@Test
public void testEncodeDecodeWithDatumDecoder() throws UnsupportedTypeException, IOException {
    StreamEvent event = new StreamEvent(Maps.<String, String>newHashMap(), ByteBuffer.wrap("Event string".getBytes(Charsets.UTF_8)));
    StreamEventCodec codec = new StreamEventCodec();
    ByteBuffer payload = ByteBuffer.wrap(codec.encodePayload(event));
    SchemaHash schemaHash = new SchemaHash(payload);
    Schema schema = new ReflectionSchemaGenerator().generate(StreamEvent.class);
    Assert.assertEquals(schema.getSchemaHash(), schemaHash);
    StreamEvent decoded = new ReflectionDatumReader<>(schema, TypeToken.of(StreamEvent.class)).read(new BinaryDecoder(new ByteBufferInputStream(payload)), schema);
    Assert.assertEquals(event.getHeaders(), decoded.getHeaders());
    Assert.assertEquals(event.getBody(), decoded.getBody());
}
Also used : SchemaHash(co.cask.cdap.api.data.schema.SchemaHash) StreamEventCodec(co.cask.cdap.common.stream.StreamEventCodec) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) Schema(co.cask.cdap.api.data.schema.Schema) ByteBufferInputStream(co.cask.common.io.ByteBufferInputStream) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) ByteBuffer(java.nio.ByteBuffer) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 63 with Schema

use of co.cask.cdap.api.data.schema.Schema in project cdap by caskdata.

the class RecordWithString method testReduceProjection.

@Test
public void testReduceProjection() throws IOException, UnsupportedTypeException {
    PipedOutputStream output = new PipedOutputStream();
    PipedInputStream input = new PipedInputStream(output);
    Schema sourceSchema = new ReflectionSchemaGenerator().generate(MoreFields.class);
    Schema targetSchema = new ReflectionSchemaGenerator().generate(LessFields.class);
    MoreFields moreFields = new MoreFields(10, 20.2, "30", ImmutableList.of("1", "2"));
    new ReflectionDatumWriter<MoreFields>(sourceSchema).encode(moreFields, new BinaryEncoder(output));
    LessFields lessFields = new ReflectionDatumReader<>(targetSchema, TypeToken.of(LessFields.class)).read(new BinaryDecoder(input), sourceSchema);
    Assert.assertEquals("30", lessFields.k);
    Assert.assertEquals(moreFields.inner.b, lessFields.inner.b);
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) Schema(co.cask.cdap.api.data.schema.Schema) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 64 with Schema

use of co.cask.cdap.api.data.schema.Schema in project cdap by caskdata.

the class RecordWithString method testEmptyValue.

// this tests that the datum reader treats empty fields correctly. It reproduces the issue in ENG-2404.
@Test
public void testEmptyValue() throws UnsupportedTypeException, IOException {
    Schema schema = new ReflectionSchemaGenerator().generate(RecordWithString.class);
    TypeRepresentation typeRep = new TypeRepresentation(RecordWithString.class);
    DatumWriter<RecordWithString> datumWriter = new ReflectionDatumWriter<>(schema);
    @SuppressWarnings("unchecked") ReflectionDatumReader<RecordWithString> datumReader = new ReflectionDatumReader<>(schema, (TypeToken<RecordWithString>) TypeToken.of(typeRep.toType()));
    RecordWithString record = new RecordWithString();
    record.setA(42);
    record.setTheString("");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    BinaryEncoder encoder = new BinaryEncoder(bos);
    datumWriter.encode(record, encoder);
    byte[] bytes = bos.toByteArray();
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    BinaryDecoder decoder = new BinaryDecoder(bis);
    RecordWithString rec = datumReader.read(decoder, schema);
    Assert.assertEquals(record.getA(), rec.getA());
    Assert.assertEquals(record.getTheString(), rec.getTheString());
}
Also used : Schema(co.cask.cdap.api.data.schema.Schema) ReflectionDatumWriter(co.cask.cdap.internal.io.ReflectionDatumWriter) ReflectionDatumReader(co.cask.cdap.internal.io.ReflectionDatumReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) TypeRepresentation(co.cask.cdap.internal.io.TypeRepresentation) Test(org.junit.Test)

Example 65 with Schema

use of co.cask.cdap.api.data.schema.Schema in project cdap by caskdata.

the class SchemaTest method testGenerateSchema.

@Test
public void testGenerateSchema() throws UnsupportedTypeException {
    Schema schema = (new ReflectionSchemaGenerator()).generate((new TypeToken<Child<Node>>() {
    }).getType());
    Gson gson = new GsonBuilder().registerTypeAdapter(Schema.class, new SchemaTypeAdapter()).create();
    Assert.assertEquals(schema, gson.fromJson(gson.toJson(schema), Schema.class));
}
Also used : SchemaTypeAdapter(co.cask.cdap.internal.io.SchemaTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Schema(co.cask.cdap.api.data.schema.Schema) Gson(com.google.gson.Gson) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) Test(org.junit.Test)

Aggregations

Schema (co.cask.cdap.api.data.schema.Schema)210 Test (org.junit.Test)92 StructuredRecord (co.cask.cdap.api.data.format.StructuredRecord)69 Table (co.cask.cdap.api.dataset.table.Table)38 ETLStage (co.cask.cdap.etl.proto.v2.ETLStage)35 ApplicationId (co.cask.cdap.proto.id.ApplicationId)34 FormatSpecification (co.cask.cdap.api.data.format.FormatSpecification)32 ApplicationManager (co.cask.cdap.test.ApplicationManager)30 AppRequest (co.cask.cdap.proto.artifact.AppRequest)29 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)24 IOException (java.io.IOException)23 ETLBatchConfig (co.cask.cdap.etl.proto.v2.ETLBatchConfig)22 ReflectionSchemaGenerator (co.cask.cdap.internal.io.ReflectionSchemaGenerator)22 ArrayList (java.util.ArrayList)22 WorkflowManager (co.cask.cdap.test.WorkflowManager)20 Map (java.util.Map)18 Set (java.util.Set)14 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)12 HashMap (java.util.HashMap)12 HashSet (java.util.HashSet)11