org.apache.avro.io.JsonEncoder Java Examples

The following examples show how to use org.apache.avro.io.JsonEncoder. 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: 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 #3
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 #4
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()));
}
 
Example #5
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 #6
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 #7
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 #8
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 #9
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 #10
Source File: Utils.java    From kafka-monitor with Apache License 2.0 5 votes vote down vote up
public static String jsonFromGenericRecord(GenericRecord record) {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<>(DefaultTopicSchema.MESSAGE_V0);

  try {
    Encoder encoder = new JsonEncoder(DefaultTopicSchema.MESSAGE_V0, out);
    writer.write(record, encoder);
    encoder.flush();
  } catch (IOException e) {
    LOG.error("Unable to serialize avro record due to error " + e);
  }
  return out.toString();
}
 
Example #11
Source File: AvroCompatibilityHelper.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static JsonEncoder newJsonEncoder(Schema schema, JsonGenerator jsonGenerator) throws IOException {
  return FACTORY.newJsonEncoder(schema, jsonGenerator);
}
 
Example #12
Source File: AvroCompatibilityHelper.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static JsonEncoder newJsonEncoder(Schema schema, OutputStream out) throws IOException {
  return FACTORY.newJsonEncoder(schema, out);
}
 
Example #13
Source File: Avro17Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JsonEncoder newJsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException {
  return EncoderFactory.get().jsonEncoder(schema, out, pretty);
}
 
Example #14
Source File: Avro14Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JsonEncoder newJsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException {
  return new JsonEncoder(schema, out);
}
 
Example #15
Source File: Avro19Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JsonEncoder newJsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException {
  return EncoderFactory.get().jsonEncoder(schema, out, pretty);
}
 
Example #16
Source File: Avro18Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JsonEncoder newJsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException {
  return EncoderFactory.get().jsonEncoder(schema, out, pretty);
}
 
Example #17
Source File: Avro15Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JsonEncoder newJsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException {
  return EncoderFactory.get().jsonEncoder(schema, out);
}
 
Example #18
Source File: Avro16Adapter.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public JsonEncoder newJsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException {
  return EncoderFactory.get().jsonEncoder(schema, out);
}
 
Example #19
Source File: AvroUtils.java    From brooklin with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Convert an Avro record to Json and encode it into byte array
 * @param schema schema describing the desired layout of the bytes
 * @param record the instance of the Avro record
 * @return encoded bytes
 */
public static byte[] encodeAvroIndexedRecordAsJson(Schema schema, IndexedRecord record) throws IOException {
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, outputStream);
  return encodeAvroIndexedRecord(schema, record, outputStream, encoder);
}
 
Example #20
Source File: AvroCompatibilityHelper.java    From avro-util with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * constructs a {@link JsonEncoder} on top of the given {@link OutputStream} for the given {@link Schema}
 * @param schema a schema
 * @param out an output stream
 * @param pretty true to pretty-print the json (if supported by runtime avro version)
 * @return an encoder
 * @throws IOException in io errors
 */
public static JsonEncoder newJsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException {
  assertAvroAvailable();
  return ADAPTER.newJsonEncoder(schema, out, pretty);
}
 
Example #21
Source File: AvroAdapter.java    From avro-util with BSD 2-Clause "Simplified" License votes vote down vote up
JsonEncoder newJsonEncoder(Schema schema, OutputStream out, boolean pretty) throws IOException;