@Test public void conversionTest() throws Exception { Json2Schema converter = new Json2Schema(); RecordConvertor recordConvertor = new RecordConvertor(); JsonParser parser = new JsonParser(); RecipePipeline executor = TestingRig.execute(directives); for (String test : TESTS) { Row row = new Row("body", test); List<Row> rows = executor.execute(Lists.newArrayList(row)); Schema schema = converter.toSchema("myrecord", rows.get(0)); if (schema.getType() != Schema.Type.RECORD) { schema = Schema.recordOf("array", Schema.Field.of("array", schema)); } Assert.assertNotNull(schema); List<StructuredRecord> structuredRecords = recordConvertor.toStructureRecord(rows, schema); String decode = StructuredRecordStringConverter.toJsonString(structuredRecords.get(0)); JsonElement originalObject = parser.parse(test); JsonElement roundTripObject = parser.parse(decode).getAsJsonObject().get("body"); Assert.assertEquals(originalObject, roundTripObject); Assert.assertTrue(structuredRecords.size() > 0); } }
public Schema toSchema(String id, Row row) throws RecordConvertorException { List<Schema.Field> fields = new ArrayList<>(); for (Pair<String, Object> column : row.getFields()) { String name = column.getFirst(); Object value = column.getSecond(); if (value instanceof Schema) { fields.addAll(((Schema) value).getFields()); } else { Schema schema = getSchema(value, name); if (schema != null) { fields.add(Schema.Field.of(name, schema)); } } } return Schema.recordOf(id, fields); }
Json2Schema { public Schema toSchema(String id, Row row) throws RecordConvertorException { List<Schema.Field> fields = new ArrayList<>(); for (Pair<String, Object> column : row.getFields()) { String name = column.getFirst(); Object value = column.getSecond(); if (value instanceof Schema) { fields.addAll(((Schema) value).getFields()); } else { Schema schema = getSchema(value, name); if (schema != null) { fields.add(Schema.Field.of(name, schema)); } } } return Schema.recordOf(id, fields); } }
Json2Schema { public Schema toSchema(String id, Row row) throws RecordConvertorException { List<Schema.Field> fields = new ArrayList<>(); for (Pair<String, Object> column : row.getFields()) { String name = column.getFirst(); Object value = column.getSecond(); if (value instanceof Schema) { fields.addAll(((Schema) value).getFields()); } else { Schema schema = getSchema(value, name); if (schema != null) { fields.add(Schema.Field.of(name, schema)); } } } return Schema.recordOf(id, fields); } Json2Schema(); }
Json2Schema { public Schema toSchema(String id, Row row) throws RecordConvertorException { List<Schema.Field> fields = new ArrayList<>(); for (Pair<String, Object> column : row.getFields()) { String name = column.getFirst(); Object value = column.getSecond(); if (value instanceof Schema) { fields.addAll(((Schema) value).getFields()); } else { Schema schema = getSchema(value, name); if (schema != null) { fields.add(Schema.Field.of(name, schema)); } } } return Schema.recordOf(id, fields); } Json2Schema(); Schema toSchema(String id, Row row); }
Json2Schema { public Schema toSchema(String id, Row row) throws RecordConvertorException { List<Schema.Field> fields = new ArrayList<>(); for (Pair<String, Object> column : row.getFields()) { String name = column.getFirst(); Object value = column.getSecond(); if (value instanceof Schema) { fields.addAll(((Schema) value).getFields()); } else { Schema schema = getSchema(value, name); if (schema != null) { fields.add(Schema.Field.of(name, schema)); } } } return Schema.recordOf(id, fields); } Json2Schema(); Schema toSchema(String id, Row row);  }