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()));
}
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());
}
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);
}
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());
}
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));
}
Aggregations