org.apache.avro.io.DatumWriter Java Examples
The following examples show how to use
org.apache.avro.io.DatumWriter.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonUtils.java From localization_nifi with Apache License 2.0 | 8 votes |
/** * Writes provided {@link GenericRecord} into the provided * {@link OutputStream} as JSON. */ public static void write(GenericRecord record, OutputStream out) { try { DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(record.getSchema()); JsonEncoder encoder = EncoderFactory.get().jsonEncoder(record.getSchema(), out); writer.write(record, encoder); encoder.flush(); } catch (Exception e) { throw new IllegalStateException("Failed to read GenericRecord", e); } }
Example #2
Source File: TestConvertAvroToJSON.java From localization_nifi with Apache License 2.0 | 7 votes |
@Test public void testSingleAvroMessage() throws IOException { final TestRunner runner = TestRunners.newTestRunner(new ConvertAvroToJSON()); final Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc")); final GenericRecord user1 = new GenericData.Record(schema); user1.put("name", "Alyssa"); user1.put("favorite_number", 256); final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema); final ByteArrayOutputStream out1 = AvroTestUtil.serializeAvroRecord(schema, datumWriter, user1); runner.enqueue(out1.toByteArray()); runner.run(); runner.assertAllFlowFilesTransferred(ConvertAvroToJSON.REL_SUCCESS, 1); final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertAvroToJSON.REL_SUCCESS).get(0); out.assertContentEquals("{\"name\": \"Alyssa\", \"favorite_number\": 256, \"favorite_color\": null}"); }
Example #3
Source File: AvroKeyValueSinkWriter.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
AvroKeyValueWriter(Schema keySchema, Schema valueSchema, CodecFactory compressionCodec, OutputStream outputStream, int syncInterval) throws IOException { // Create the generic record schema for the key/value pair. mKeyValuePairSchema = AvroKeyValue .getSchema(keySchema, valueSchema); // Create an Avro container file and a writer to it. DatumWriter<GenericRecord> genericDatumWriter = new GenericDatumWriter<GenericRecord>( mKeyValuePairSchema); mAvroFileWriter = new DataFileWriter<GenericRecord>( genericDatumWriter); mAvroFileWriter.setCodec(compressionCodec); mAvroFileWriter.setSyncInterval(syncInterval); mAvroFileWriter.create(mKeyValuePairSchema, outputStream); // Create a reusable output record. mOutputRecord = new AvroKeyValue<Object, Object>( new GenericData.Record(mKeyValuePairSchema)); }
Example #4
Source File: AvroFileAppender.java From iceberg with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") private static <D> DataFileWriter<D> newAvroWriter( Schema schema, OutputFile file, Function<Schema, DatumWriter<?>> createWriterFunc, CodecFactory codec, Map<String, String> metadata) throws IOException { DataFileWriter<D> writer = new DataFileWriter<>( (DatumWriter<D>) createWriterFunc.apply(schema)); writer.setCodec(codec); for (Map.Entry<String, String> entry : metadata.entrySet()) { writer.setMeta(entry.getKey(), entry.getValue()); } // TODO: support overwrite return writer.create(schema, file.create()); }
Example #5
Source File: AvroFileReaderTest.java From kafka-connect-fs with Apache License 2.0 | 6 votes |
@Override protected Path createDataFile(ReaderFsTestConfig fsConfig, Object... args) throws IOException { File avroFile = File.createTempFile("test-", "." + getFileExtension()); DatumWriter<GenericRecord> writer = new GenericDatumWriter<>(schema); try (DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<>(writer)) { dataFileWriter.setFlushOnEveryBlock(true); dataFileWriter.setSyncInterval(32); dataFileWriter.create(schema, avroFile); IntStream.range(0, NUM_RECORDS).forEach(index -> { GenericRecord datum = new GenericData.Record(schema); datum.put(FIELD_INDEX, index); datum.put(FIELD_NAME, String.format("%d_name_%s", index, UUID.randomUUID())); datum.put(FIELD_SURNAME, String.format("%d_surname_%s", index, UUID.randomUUID())); try { fsConfig.offsetsByIndex().put(index, dataFileWriter.sync() - 16L); dataFileWriter.append(datum); } catch (IOException ioe) { throw new RuntimeException(ioe); } }); } Path path = new Path(new Path(fsConfig.getFsUri()), avroFile.getName()); fsConfig.getFs().moveFromLocalFile(new Path(avroFile.getAbsolutePath()), path); return path; }
Example #6
Source File: TestAvroSerializer.java From flume-elasticsearch-sink with Apache License 2.0 | 6 votes |
/** * tests Avro Serializer */ @Test public void testSerializer() throws Exception { Context context = new Context(); String schemaFile = getClass().getResource("/schema.avsc").getFile(); context.put(ES_AVRO_SCHEMA_FILE, schemaFile); avroSerializer.configure(context); Schema schema = new Schema.Parser().parse(new File(schemaFile)); GenericRecord user = generateGenericRecord(schema); DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Encoder encoder = new EncoderFactory().binaryEncoder(outputStream, null); datumWriter.write(user, encoder); encoder.flush(); Event event = EventBuilder.withBody(outputStream.toByteArray()); XContentBuilder expected = generateContentBuilder(); XContentBuilder actual = avroSerializer.serialize(event); JsonParser parser = new JsonParser(); assertEquals(parser.parse(Strings.toString(expected)), parser.parse(Strings.toString(actual))); }
Example #7
Source File: DataSerializer.java From jMetalSP with MIT License | 6 votes |
public byte[] serializeMessage(S clazz, String path) { byte[] result = null; try { File file = new File(path); Schema schema = new Schema.Parser().parse(file); ByteArrayOutputStream out = new ByteArrayOutputStream(); BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null); DatumWriter<S> dataFileWriter = new SpecificDatumWriter<S>(schema); dataFileWriter.write(clazz, encoder); encoder.flush(); result=out.toByteArray(); out.close(); }catch (Exception ex){ ex.printStackTrace(); } return result; }
Example #8
Source File: Hdfs.java From pxf with Apache License 2.0 | 6 votes |
@Override public void writeAvroFile(String pathToFile, String schemaName, String codecName, IAvroSchema[] data) throws Exception { Path path = getDatapath(pathToFile); OutputStream outStream = fs.create(path, true, bufferSize, replicationSize, blockSize); Schema schema = new Schema.Parser().parse(new FileInputStream( schemaName)); DatumWriter<GenericRecord> writer = new GenericDatumWriter<>( schema); DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<>( writer); if (!StringUtils.isEmpty(codecName)) { dataFileWriter.setCodec(CodecFactory.fromString(codecName)); } dataFileWriter.create(schema, outStream); for (IAvroSchema iAvroSchema : data) { GenericRecord datum = iAvroSchema.serialize(); dataFileWriter.append(datum); } dataFileWriter.close(); }
Example #9
Source File: TransformTest.java From schema-registry-transfer-smt with Apache License 2.0 | 6 votes |
private ByteArrayOutputStream encodeAvroObject(org.apache.avro.Schema schema, int sourceId, Object datum) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(MAGIC_BYTE); out.write(ByteBuffer.allocate(ID_SIZE).putInt(sourceId).array()); EncoderFactory encoderFactory = EncoderFactory.get(); BinaryEncoder encoder = encoderFactory.directBinaryEncoder(out, null); Object value = datum instanceof NonRecordContainer ? ((NonRecordContainer) datum).getValue() : datum; DatumWriter<Object> writer = new GenericDatumWriter<>(schema); writer.write(value, encoder); encoder.flush(); return out; }
Example #10
Source File: AvroKeyValueSinkWriter.java From flink with Apache License 2.0 | 6 votes |
AvroKeyValueWriter(Schema keySchema, Schema valueSchema, CodecFactory compressionCodec, OutputStream outputStream, int syncInterval) throws IOException { // Create the generic record schema for the key/value pair. mKeyValuePairSchema = AvroKeyValue .getSchema(keySchema, valueSchema); // Create an Avro container file and a writer to it. DatumWriter<GenericRecord> genericDatumWriter = new GenericDatumWriter<GenericRecord>( mKeyValuePairSchema); mAvroFileWriter = new DataFileWriter<GenericRecord>( genericDatumWriter); mAvroFileWriter.setCodec(compressionCodec); mAvroFileWriter.setSyncInterval(syncInterval); mAvroFileWriter.create(mKeyValuePairSchema, outputStream); // Create a reusable output record. mOutputRecord = new AvroKeyValue<Object, Object>( new GenericData.Record(mKeyValuePairSchema)); }
Example #11
Source File: AvroSerializationSchema.java From pulsar with Apache License 2.0 | 6 votes |
@Override public byte[] serialize(T t) { if (null == t) { return null; } // Writer to serialize Avro record into a byte array. DatumWriter<T> writer = new SpecificDatumWriter<>(t.getSchema()); // Output stream to serialize records into byte array. ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); // Low-level class for serialization of Avro values. Encoder encoder = EncoderFactory.get().binaryEncoder(arrayOutputStream, null); arrayOutputStream.reset(); try { writer.write(t, encoder); encoder.flush(); } catch (IOException e) { throw new RuntimeException("Error while serializing the record to Avro", e); } return arrayOutputStream.toByteArray(); }
Example #12
Source File: TestConvertAvroToJSON.java From localization_nifi with Apache License 2.0 | 6 votes |
@Test public void testZeroRecords() throws IOException { final TestRunner runner = TestRunners.newTestRunner(new ConvertAvroToJSON()); final Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc")); final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema); final ByteArrayOutputStream out1 = serializeAvroRecord(schema, datumWriter); runner.enqueue(out1.toByteArray()); runner.run(); runner.assertAllFlowFilesTransferred(ConvertAvroToJSON.REL_SUCCESS, 1); final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertAvroToJSON.REL_SUCCESS).get(0); out.assertContentEquals("{}"); }
Example #13
Source File: TestConvertAvroToJSON.java From localization_nifi with Apache License 2.0 | 6 votes |
@Test public void testMultipleAvroMessagesContainerNone() throws IOException { final TestRunner runner = TestRunners.newTestRunner(new ConvertAvroToJSON()); final Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc")); runner.setProperty(ConvertAvroToJSON.CONTAINER_OPTIONS, ConvertAvroToJSON.CONTAINER_NONE); final GenericRecord user1 = new GenericData.Record(schema); user1.put("name", "Alyssa"); user1.put("favorite_number", 256); final GenericRecord user2 = new GenericData.Record(schema); user2.put("name", "George"); user2.put("favorite_number", 1024); user2.put("favorite_color", "red"); final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema); final ByteArrayOutputStream out1 = serializeAvroRecord(schema, datumWriter, user1, user2); runner.enqueue(out1.toByteArray()); runner.run(); runner.assertAllFlowFilesTransferred(ConvertAvroToJSON.REL_SUCCESS, 1); final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertAvroToJSON.REL_SUCCESS).get(0); out.assertContentEquals("{\"name\": \"Alyssa\", \"favorite_number\": 256, \"favorite_color\": null}\n{\"name\": \"George\", \"favorite_number\": 1024, \"favorite_color\": \"red\"}"); }
Example #14
Source File: TestAvroNameMapping.java From iceberg with Apache License 2.0 | 6 votes |
private Record writeAndRead(Schema writeSchema, Schema readSchema, Record record, NameMapping nameMapping) throws IOException { File file = temp.newFile(); // Write without file ids org.apache.avro.Schema writeAvroSchema = RemoveIds.removeIds(writeSchema); DatumWriter<Record> datumWriter = new GenericDatumWriter<>(writeAvroSchema); try (DataFileWriter<Record> dataFileWriter = new DataFileWriter<>(datumWriter)) { dataFileWriter.create(writeAvroSchema, file); dataFileWriter.append(record); } Iterable<GenericData.Record> records = Avro.read(Files.localInput(file)) .project(readSchema) .withNameMapping(nameMapping) .build(); return Iterables.getOnlyElement(records); }
Example #15
Source File: TestConvertAvroToJSON.java From localization_nifi with Apache License 2.0 | 6 votes |
@Test public void testMultipleAvroMessages() throws IOException { final TestRunner runner = TestRunners.newTestRunner(new ConvertAvroToJSON()); final Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc")); runner.setProperty(ConvertAvroToJSON.CONTAINER_OPTIONS, ConvertAvroToJSON.CONTAINER_ARRAY); final GenericRecord user1 = new GenericData.Record(schema); user1.put("name", "Alyssa"); user1.put("favorite_number", 256); final GenericRecord user2 = new GenericData.Record(schema); user2.put("name", "George"); user2.put("favorite_number", 1024); user2.put("favorite_color", "red"); final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema); final ByteArrayOutputStream out1 = AvroTestUtil.serializeAvroRecord(schema, datumWriter, user1, user2); runner.enqueue(out1.toByteArray()); runner.run(); runner.assertAllFlowFilesTransferred(ConvertAvroToJSON.REL_SUCCESS, 1); final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertAvroToJSON.REL_SUCCESS).get(0); out.assertContentEquals("[{\"name\": \"Alyssa\", \"favorite_number\": 256, \"favorite_color\": null},{\"name\": \"George\", \"favorite_number\": 1024, \"favorite_color\": \"red\"}]"); }
Example #16
Source File: TestConvertAvroToJSON.java From localization_nifi with Apache License 2.0 | 6 votes |
@Test public void testSingleAvroMessage_wrapSingleMessage_noContainer() throws IOException { final TestRunner runner = TestRunners.newTestRunner(new ConvertAvroToJSON()); // Verify we do not wrap output for a single record if not configured to use a container runner.setProperty(ConvertAvroToJSON.CONTAINER_OPTIONS, ConvertAvroToJSON.CONTAINER_NONE); runner.setProperty(ConvertAvroToJSON.WRAP_SINGLE_RECORD, Boolean.toString(true)); final Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc")); final GenericRecord user1 = new GenericData.Record(schema); user1.put("name", "Alyssa"); user1.put("favorite_number", 256); final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema); final ByteArrayOutputStream out1 = AvroTestUtil.serializeAvroRecord(schema, datumWriter, user1); runner.enqueue(out1.toByteArray()); runner.run(); runner.assertAllFlowFilesTransferred(ConvertAvroToJSON.REL_SUCCESS, 1); final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertAvroToJSON.REL_SUCCESS).get(0); out.assertContentEquals("{\"name\": \"Alyssa\", \"favorite_number\": 256, \"favorite_color\": null}"); }
Example #17
Source File: TestConvertAvroToJSON.java From localization_nifi with Apache License 2.0 | 6 votes |
@Test public void testSingleAvroMessage_wrapSingleMessage() throws IOException { final TestRunner runner = TestRunners.newTestRunner(new ConvertAvroToJSON()); runner.setProperty(ConvertAvroToJSON.CONTAINER_OPTIONS, ConvertAvroToJSON.CONTAINER_ARRAY); runner.setProperty(ConvertAvroToJSON.WRAP_SINGLE_RECORD, Boolean.toString(true)); final Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc")); final GenericRecord user1 = new GenericData.Record(schema); user1.put("name", "Alyssa"); user1.put("favorite_number", 256); final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema); final ByteArrayOutputStream out1 = AvroTestUtil.serializeAvroRecord(schema, datumWriter, user1); runner.enqueue(out1.toByteArray()); runner.run(); runner.assertAllFlowFilesTransferred(ConvertAvroToJSON.REL_SUCCESS, 1); final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertAvroToJSON.REL_SUCCESS).get(0); out.assertContentEquals("[{\"name\": \"Alyssa\", \"favorite_number\": 256, \"favorite_color\": null}]"); }
Example #18
Source File: TestExtractAvroMetadata.java From localization_nifi with Apache License 2.0 | 6 votes |
@Test public void testExtractionWithCodec() throws IOException { final TestRunner runner = TestRunners.newTestRunner(new ExtractAvroMetadata()); runner.setProperty(ExtractAvroMetadata.METADATA_KEYS, AVRO_CODEC_ATTR); // test dynamic attribute avro.codec final Schema schema = new Schema.Parser().parse(new File("src/test/resources/array.avsc")); final GenericData.Array<String> data = new GenericData.Array<>(schema, Arrays.asList("one", "two", "three")); final DatumWriter<GenericData.Array<String>> datumWriter = new GenericDatumWriter<>(schema); final ByteArrayOutputStream out = new ByteArrayOutputStream(); final DataFileWriter<GenericData.Array<String>> dataFileWriter = new DataFileWriter<>(datumWriter); dataFileWriter.setCodec(CodecFactory.deflateCodec(1)); dataFileWriter.create(schema, out); dataFileWriter.append(data); dataFileWriter.close(); runner.enqueue(out.toByteArray()); runner.run(); runner.assertAllFlowFilesTransferred(ExtractAvroMetadata.REL_SUCCESS, 1); final MockFlowFile flowFile = runner.getFlowFilesForRelationship(ExtractAvroMetadata.REL_SUCCESS).get(0); flowFile.assertAttributeEquals("avro.codec", "deflate"); }
Example #19
Source File: TestConvertAvroToJSON.java From localization_nifi with Apache License 2.0 | 6 votes |
@Test public void testSingleAvroMessage_noContainer() throws IOException { final TestRunner runner = TestRunners.newTestRunner(new ConvertAvroToJSON()); runner.setProperty(ConvertAvroToJSON.CONTAINER_OPTIONS, ConvertAvroToJSON.CONTAINER_NONE); final Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc")); final GenericRecord user1 = new GenericData.Record(schema); user1.put("name", "Alyssa"); user1.put("favorite_number", 256); final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema); final ByteArrayOutputStream out1 = AvroTestUtil.serializeAvroRecord(schema, datumWriter, user1); runner.enqueue(out1.toByteArray()); runner.run(); runner.assertAllFlowFilesTransferred(ConvertAvroToJSON.REL_SUCCESS, 1); final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertAvroToJSON.REL_SUCCESS).get(0); out.assertContentEquals("{\"name\": \"Alyssa\", \"favorite_number\": 256, \"favorite_color\": null}"); }
Example #20
Source File: TestConvertAvroToJSON.java From localization_nifi with Apache License 2.0 | 5 votes |
@Test public void testSingleSchemalessAvroMessage() throws IOException { final TestRunner runner = TestRunners.newTestRunner(new ConvertAvroToJSON()); Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc")); String stringSchema = schema.toString(); runner.setProperty(ConvertAvroToJSON.SCHEMA, stringSchema); final GenericRecord user1 = new GenericData.Record(schema); user1.put("name", "Alyssa"); user1.put("favorite_number", 256); final ByteArrayOutputStream out1 = new ByteArrayOutputStream(); final BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out1, null); final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema); datumWriter.write(user1, encoder); encoder.flush(); out1.flush(); byte[] test = out1.toByteArray(); runner.enqueue(test); runner.run(); runner.assertAllFlowFilesTransferred(ConvertAvroToJSON.REL_SUCCESS, 1); final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertAvroToJSON.REL_SUCCESS).get(0); out.assertContentEquals("{\"name\": \"Alyssa\", \"favorite_number\": 256, \"favorite_color\": null}"); }
Example #21
Source File: GenericRecordBinaryEncoder.java From gcp-ingestion with Mozilla Public License 2.0 | 5 votes |
/** * Encode a GenericRecord into an avro encoded payload. */ public byte[] encodeRecord(GenericRecord record, Schema schema) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { DatumWriter<GenericRecord> writer = new GenericDatumWriter<>(schema); Encoder encoder = EncoderFactory.get().binaryEncoder(baos, null); writer.write(record, encoder); encoder.flush(); return baos.toByteArray(); } catch (IOException e) { throw new UncheckedIOException(e); } }
Example #22
Source File: TestConvertAvroToJSON.java From localization_nifi with Apache License 2.0 | 5 votes |
@Test public void testSingleSchemalessAvroMessage_wrapSingleMessage_noContainer() throws IOException { final TestRunner runner = TestRunners.newTestRunner(new ConvertAvroToJSON()); runner.setProperty(ConvertAvroToJSON.CONTAINER_OPTIONS, ConvertAvroToJSON.CONTAINER_NONE); runner.setProperty(ConvertAvroToJSON.WRAP_SINGLE_RECORD, Boolean.toString(true)); Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc")); String stringSchema = schema.toString(); runner.setProperty(ConvertAvroToJSON.SCHEMA, stringSchema); final GenericRecord user1 = new GenericData.Record(schema); user1.put("name", "Alyssa"); user1.put("favorite_number", 256); final ByteArrayOutputStream out1 = new ByteArrayOutputStream(); final BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out1, null); final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema); datumWriter.write(user1, encoder); encoder.flush(); out1.flush(); byte[] test = out1.toByteArray(); runner.enqueue(test); runner.run(); runner.assertAllFlowFilesTransferred(ConvertAvroToJSON.REL_SUCCESS, 1); final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertAvroToJSON.REL_SUCCESS).get(0); out.assertContentEquals("{\"name\": \"Alyssa\", \"favorite_number\": 256, \"favorite_color\": null}"); }
Example #23
Source File: TestConvertAvroToJSON.java From localization_nifi with Apache License 2.0 | 5 votes |
@Test public void testSingleSchemalessAvroMessage_wrapSingleMessage() throws IOException { final TestRunner runner = TestRunners.newTestRunner(new ConvertAvroToJSON()); runner.setProperty(ConvertAvroToJSON.CONTAINER_OPTIONS, ConvertAvroToJSON.CONTAINER_ARRAY); runner.setProperty(ConvertAvroToJSON.WRAP_SINGLE_RECORD, Boolean.toString(true)); Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc")); String stringSchema = schema.toString(); runner.setProperty(ConvertAvroToJSON.SCHEMA, stringSchema); final GenericRecord user1 = new GenericData.Record(schema); user1.put("name", "Alyssa"); user1.put("favorite_number", 256); final ByteArrayOutputStream out1 = new ByteArrayOutputStream(); final BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out1, null); final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema); datumWriter.write(user1, encoder); encoder.flush(); out1.flush(); byte[] test = out1.toByteArray(); runner.enqueue(test); runner.run(); runner.assertAllFlowFilesTransferred(ConvertAvroToJSON.REL_SUCCESS, 1); final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertAvroToJSON.REL_SUCCESS).get(0); out.assertContentEquals("[{\"name\": \"Alyssa\", \"favorite_number\": 256, \"favorite_color\": null}]"); }
Example #24
Source File: RegistrylessAvroConverter.java From registryless-avro-converter with Apache License 2.0 | 5 votes |
@Override public byte[] fromConnectData(String topic, Schema schema, Object value) { DatumWriter<GenericRecord> datumWriter; if (avroSchema != null) { datumWriter = new GenericDatumWriter<GenericRecord>(avroSchema); } else { datumWriter = new GenericDatumWriter<GenericRecord>(); } GenericRecord avroInstance = (GenericRecord)avroDataHelper.fromConnectData(schema, value); try ( ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<GenericRecord>(datumWriter); ) { dataFileWriter.setCodec(CodecFactory.nullCodec()); if (avroSchema != null) { dataFileWriter.create(avroSchema, baos); } else { dataFileWriter.create(avroInstance.getSchema(), baos); } dataFileWriter.append(avroInstance); dataFileWriter.flush(); return baos.toByteArray(); } catch (IOException ioe) { throw new DataException("Error serializing Avro", ioe); } }
Example #25
Source File: TestConvertAvroToJSON.java From localization_nifi with Apache License 2.0 | 5 votes |
@Test public void testSingleSchemalessAvroMessage_noContainer() throws IOException { final TestRunner runner = TestRunners.newTestRunner(new ConvertAvroToJSON()); runner.setProperty(ConvertAvroToJSON.CONTAINER_OPTIONS, ConvertAvroToJSON.CONTAINER_NONE); Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc")); String stringSchema = schema.toString(); runner.setProperty(ConvertAvroToJSON.SCHEMA, stringSchema); final GenericRecord user1 = new GenericData.Record(schema); user1.put("name", "Alyssa"); user1.put("favorite_number", 256); final ByteArrayOutputStream out1 = new ByteArrayOutputStream(); final BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out1, null); final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema); datumWriter.write(user1, encoder); encoder.flush(); out1.flush(); byte[] test = out1.toByteArray(); runner.enqueue(test); runner.run(); runner.assertAllFlowFilesTransferred(ConvertAvroToJSON.REL_SUCCESS, 1); final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertAvroToJSON.REL_SUCCESS).get(0); out.assertContentEquals("{\"name\": \"Alyssa\", \"favorite_number\": 256, \"favorite_color\": null}"); }
Example #26
Source File: SparkVerifierTest.java From tablasco with Apache License 2.0 | 5 votes |
private static void writeAvroData(List<GenericRecord> data, File avroFile) throws IOException { FileUtils.forceMkdir(avroFile.getParentFile()); Schema schema = data.get(0).getSchema(); DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema); DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<>(datumWriter); dataFileWriter.create(schema, avroFile); for (GenericRecord genericRecord : data) { dataFileWriter.append(genericRecord); } dataFileWriter.close(); }
Example #27
Source File: AvroCoder.java From beam with Apache License 2.0 | 5 votes |
protected AvroCoder(Class<T> type, Schema schema) { this.type = type; this.schemaSupplier = new SerializableSchemaSupplier(schema); typeDescriptor = TypeDescriptor.of(type); nonDeterministicReasons = new AvroDeterminismChecker().check(TypeDescriptor.of(type), schema); // Decoder and Encoder start off null for each thread. They are allocated and potentially // reused inside encode/decode. this.decoder = new EmptyOnDeserializationThreadLocal<>(); this.encoder = new EmptyOnDeserializationThreadLocal<>(); this.reflectData = Suppliers.memoize(new SerializableReflectDataSupplier(getType())); // Reader and writer are allocated once per thread per Coder this.reader = new EmptyOnDeserializationThreadLocal<DatumReader<T>>() { private final AvroCoder<T> myCoder = AvroCoder.this; @Override public DatumReader<T> initialValue() { return myCoder.getType().equals(GenericRecord.class) ? new GenericDatumReader<>(myCoder.getSchema()) : new ReflectDatumReader<>( myCoder.getSchema(), myCoder.getSchema(), myCoder.reflectData.get()); } }; this.writer = new EmptyOnDeserializationThreadLocal<DatumWriter<T>>() { private final AvroCoder<T> myCoder = AvroCoder.this; @Override public DatumWriter<T> initialValue() { return myCoder.getType().equals(GenericRecord.class) ? new GenericDatumWriter<>(myCoder.getSchema()) : new ReflectDatumWriter<>(myCoder.getSchema(), myCoder.reflectData.get()); } }; }
Example #28
Source File: TimelineMetadataUtils.java From hudi with Apache License 2.0 | 5 votes |
public static <T extends SpecificRecordBase> Option<byte[]> serializeAvroMetadata(T metadata, Class<T> clazz) throws IOException { DatumWriter<T> datumWriter = new SpecificDatumWriter<>(clazz); DataFileWriter<T> fileWriter = new DataFileWriter<>(datumWriter); ByteArrayOutputStream baos = new ByteArrayOutputStream(); fileWriter.create(metadata.getSchema(), baos); fileWriter.append(metadata); fileWriter.flush(); return Option.of(baos.toByteArray()); }
Example #29
Source File: AvroCodec.java From schema-evolution-samples with Apache License 2.0 | 5 votes |
@Override public void encode(Object object, OutputStream outputStream) throws IOException { Schema schema = getSchema(object); Integer id = schemaRegistryClient.register(schema); DatumWriter writer = getDatumWriter(object.getClass(),schema); Encoder encoder = EncoderFactory.get().binaryEncoder(outputStream, null); outputStream.write(ByteBuffer.allocate(4).putInt(id).array()); writer.write(object,encoder); encoder.flush(); }
Example #30
Source File: TestConvertAvroToJSON.java From localization_nifi with Apache License 2.0 | 5 votes |
private ByteArrayOutputStream serializeAvroRecord(final Schema schema, final DatumWriter<GenericRecord> datumWriter, final GenericRecord... users) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<GenericRecord>(datumWriter); dataFileWriter.create(schema, out); for (final GenericRecord user : users) { dataFileWriter.append(user); } dataFileWriter.close(); return out; }