Java Code Examples for org.apache.avro.io.JsonEncoder#flush()

The following examples show how to use org.apache.avro.io.JsonEncoder#flush() . 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 vote down vote up
/**
 * 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: YarnSubmissionParametersFileGenerator.java    From reef with Apache License 2.0 6 votes vote down vote up
static void writeAvroYarnJobSubmissionParametersToOutputStream(
    final YarnClusterSubmissionFromCS yarnClusterSubmissionFromCS,
    final String jobFolderOnDFSPath,
    final OutputStream outputStream) throws IOException {
  final DatumWriter<AvroYarnJobSubmissionParameters> datumWriter =
      new SpecificDatumWriter<>(AvroYarnJobSubmissionParameters.class);

  final AvroYarnJobSubmissionParameters jobSubmissionParameters =
      yarnClusterSubmissionFromCS.getYarnJobSubmissionParameters();
  jobSubmissionParameters.setDfsJobSubmissionFolder(jobFolderOnDFSPath);
  final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(jobSubmissionParameters.getSchema(),
      outputStream);
  datumWriter.write(jobSubmissionParameters, encoder);
  encoder.flush();
  outputStream.flush();
}
 
Example 3
Source File: AvroEvaluatorListSerializer.java    From reef with Apache License 2.0 6 votes vote down vote up
/**
 * Convert AvroEvaluatorList to JSON string.
 */
@Override
public String toString(final AvroEvaluatorList avroEvaluatorList) {
  final DatumWriter<AvroEvaluatorList> evaluatorWriter = new SpecificDatumWriter<>(AvroEvaluatorList.class);
  try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
    final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(avroEvaluatorList.getSchema(), out);
    evaluatorWriter.write(avroEvaluatorList, encoder);
    encoder.flush();
    return out.toString(AvroHttpSerializer.JSON_CHARSET);
  } catch (final IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 4
Source File: HoodieAvroUtils.java    From hudi with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a given avro record to json and return the encoded bytes.
 *
 * @param record The GenericRecord to convert
 * @param pretty Whether to pretty-print the json output
 */
public static byte[] avroToJson(GenericRecord record, boolean pretty) throws IOException {
  DatumWriter<Object> writer = new GenericDatumWriter<>(record.getSchema());
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  JsonEncoder jsonEncoder = EncoderFactory.get().jsonEncoder(record.getSchema(), out, pretty);
  writer.write(record, jsonEncoder);
  jsonEncoder.flush();
  return out.toByteArray();
}
 
Example 5
Source File: FixedFlowInputRuntimeTest.java    From components with Apache License 2.0 5 votes vote down vote up
private static String generateInputJSON(Schema inputSchema, IndexedRecord inputIndexedRecord) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DatumWriter<IndexedRecord> writer = new GenericDatumWriter<IndexedRecord>(inputSchema);
    JsonEncoder encoder = EncoderFactory.get().jsonEncoder(inputSchema, baos, false);
    writer.write(inputIndexedRecord, encoder);
    encoder.flush();
    baos.flush();
    return new String(baos.toByteArray(), StandardCharsets.UTF_8);
}
 
Example 6
Source File: YarnSubmissionParametersFileGenerator.java    From reef with Apache License 2.0 5 votes vote down vote up
static void writeAvroYarnAppSubmissionParametersToOutputStream(
    final YarnClusterSubmissionFromCS yarnClusterSubmissionFromCS,
    final OutputStream outputStream) throws IOException {
  final DatumWriter<AvroYarnAppSubmissionParameters> datumWriter =
      new SpecificDatumWriter<>(AvroYarnAppSubmissionParameters.class);

  final AvroYarnAppSubmissionParameters appSubmissionParameters =
      yarnClusterSubmissionFromCS.getYarnAppSubmissionParameters();
  final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(appSubmissionParameters.getSchema(), outputStream);
  datumWriter.write(appSubmissionParameters, encoder);
  encoder.flush();
  outputStream.flush();
}
 
Example 7
Source File: AvroDriverInfoSerializer.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * Convert AvroDriverInfo object to JSON string.
 */
@Override
public String toString(final AvroDriverInfo avroDriverInfo) {
  final DatumWriter<AvroDriverInfo> driverWriter = new SpecificDatumWriter<>(AvroDriverInfo.class);
  try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
    final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(avroDriverInfo.getSchema(), out);
    driverWriter.write(avroDriverInfo, encoder);
    encoder.flush();
    return out.toString(AvroHttpSerializer.JSON_CHARSET);
  } catch (final IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 8
Source File: AvroEvaluatorInfoSerializer.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * Convert AvroEvaluatorsInfo object to JSON string.
 */
@Override
public String toString(final AvroEvaluatorsInfo avroEvaluatorsInfo) {
  final DatumWriter<AvroEvaluatorsInfo> evaluatorWriter = new SpecificDatumWriter<>(AvroEvaluatorsInfo.class);
  try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
    final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(avroEvaluatorsInfo.getSchema(), out);
    evaluatorWriter.write(avroEvaluatorsInfo, encoder);
    encoder.flush();
    return out.toString(AvroHttpSerializer.JSON_CHARSET);
  } catch (final IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 9
Source File: AvroToJsonConverter.java    From celos with Apache License 2.0 5 votes vote down vote up
@Override
public FixFile convert(TestRun testRun, FixFile ff) throws IOException {
    byte[] bytes = IOUtils.toByteArray(ff.getContent());
    if (bytes.length == 0) {
        return ff;
    }
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    GenericDatumReader<Object> reader = new GenericDatumReader<>();
    FileReader<Object> fileReader =  DataFileReader.openReader(new SeekableByteArrayInput(bytes), reader);
    try {
        Schema schema = fileReader.getSchema();
        DatumWriter<Object> writer = new GenericDatumWriter<>(schema);
        JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, os);

        for (Object datum : fileReader) {
            writer.write(datum, encoder);
        }
        encoder.flush();
    } finally {
        fileReader.close();
    }
    return new FixFile(new ByteArrayInputStream(os.toByteArray()));
}