Java 类org.apache.avro.io.JsonEncoder 实例源码

项目:avro-cli    文件:RawConverterUtil.java   
static void convertAvroToJson(InputStream inputStream, OutputStream outputStream, Schema schema)
        throws IOException {
    DatumReader<Object> reader = new GenericDatumReader<>(schema);
    DatumWriter<Object> writer = new GenericDatumWriter<>(schema);

    BinaryDecoder binaryDecoder = DecoderFactory.get().binaryDecoder(inputStream, null);

    JsonEncoder jsonEncoder = EncoderFactory.get().jsonEncoder(schema, outputStream, true);
    Object datum = null;
    while (!binaryDecoder.isEnd()) {
        datum = reader.read(datum, binaryDecoder);
        writer.write(datum, jsonEncoder);
        jsonEncoder.flush();
    }
    outputStream.flush();
}
项目:avro-ui    文件:AvroUiSandboxServiceImpl.java   
@Override
public String getJsonStringFromRecord(RecordField field)
        throws AvroUiSandboxServiceException {
    try {
        GenericRecord record = FormAvroConverter.createGenericRecordFromRecordField(field);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JsonGenerator jsonGenerator
            = new JsonFactory().createJsonGenerator(baos, JsonEncoding.UTF8);
        jsonGenerator.useDefaultPrettyPrinter();
        JsonEncoder jsonEncoder = EncoderFactory.get().jsonEncoder(record.getSchema(), jsonGenerator);
        DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<GenericRecord>(record.getSchema());
        datumWriter.write(record, jsonEncoder);
        jsonEncoder.flush();
        baos.flush();            
        return new String(baos.toByteArray(), UTF8);
    } catch (Exception e) {
        throw Utils.handleException(e);
    }
}
项目:reef    文件:YarnSubmissionParametersFileGenerator.java   
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();
}
项目:celos    文件:AvroToJsonConverter.java   
@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()));
}
项目:sparqles    文件:MongoDBManager.java   
private DBObject getObject(Object o, Schema s){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JsonEncoder e;
    try {
        e = EncoderFactory.get().jsonEncoder(s, baos);
        SpecificDatumWriter w = new SpecificDatumWriter(o.getClass());
        w.write(o, e);
        e.flush();
        DBObject dbObject = (DBObject)JSON.parse(baos.toString());
        return dbObject;

    } catch (IOException e1) {
        log.error("GET OBJECT Exception: {} {}", o.getClass(), ExceptionHandler.logAndtoString(e1,true));
    }
    return null;
}
项目:kiji-rest    文件:AvroToJsonStringSerializer.java   
/**
 * Returns an encoded JSON string for the given Avro object.
 *
 * @param record is the record to encode
 * @return the JSON string representing this Avro object.
 *
 * @throws IOException if there is an error.
 */
public static String getJsonString(GenericContainer record) throws IOException {
  ByteArrayOutputStream os = new ByteArrayOutputStream();
  JsonEncoder encoder = EncoderFactory.get().jsonEncoder(record.getSchema(), os);
  DatumWriter<GenericContainer> writer = new GenericDatumWriter<GenericContainer>();
  if (record instanceof SpecificRecord) {
    writer = new SpecificDatumWriter<GenericContainer>();
  }

  writer.setSchema(record.getSchema());
  writer.write(record, encoder);
  encoder.flush();
  String jsonString = new String(os.toByteArray(), Charset.forName("UTF-8"));
  os.close();
  return jsonString;
}
项目:wherehowsX    文件:AvroMessageFormatter.java   
private void writeTo(byte[] data, PrintStream output) throws IOException {
  Object object = deserialize(data);
  Schema schema = getSchema(object);

  try {
    JsonEncoder encoder = encoderFactory.jsonEncoder(schema, output);
    DatumWriter<Object> writer = new GenericDatumWriter<Object>(schema);
    writer.write(object, encoder);
    encoder.flush();
  } catch (AvroRuntimeException e) {
    throw new SerializationException(
        String.format("Error serializing Avro data of schema %s to json", schema), e);
  }
}
项目:kafka-monitor    文件:Utils.java   
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();
}
项目:toolbox    文件:AvroHelper.java   
public static String toJson(Schema schema, Object object) throws IOException {
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  SpecificDatumWriter<Object> writer = new SpecificDatumWriter<>(schema);
  JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, outputStream);
  writer.write(object, encoder);
  encoder.flush();
  return outputStream.toString();
}
项目:components    文件:FixedFlowInputRuntimeTest.java   
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);
}
项目:spring-usc    文件:AvroImport.java   
@Override
public Worksheet generateWorksheet() throws JSONException, IOException,
        KarmaException {
    DataFileReader<Void> schemareader = new DataFileReader<Void>(file, new GenericDatumReader<Void>());
    Schema schema = schemareader.getSchema();
    schemareader.close();
    DataFileReader<GenericRecord> reader = new DataFileReader<GenericRecord>(file, new GenericDatumReader<GenericRecord>(schema));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write('[');
    baos.write('\n');
    GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(reader.getSchema());
    while(reader.hasNext())
    {

        GenericRecord record = reader.next();
            JsonEncoder encoder = EncoderFactory.get().jsonEncoder(reader.getSchema(), new JsonFactory().createJsonGenerator(baos)).configure(baos);
            writer.write(record, encoder);
            encoder.flush();
            if(reader.hasNext())
            {
                baos.write(',');
            }


    }
    reader.close();
    baos.write('\n');
    baos.write(']');
    baos.flush();
    baos.close();
    String json = new String(baos.toByteArray());
    JsonImport jsonImport = new JsonImport(json, this.getFactory(), this.getWorksheet(), workspace, maxNumLines);
    return jsonImport.generateWorksheet();
}
项目:rico    文件:AvroStringSerde.java   
@Override
public void writeTo(Object msg, StringWriter writer) throws Exception {
    SpecificRecord record = (SpecificRecord) msg;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    _writer.setSchema(record.getSchema());
    JsonEncoder encoder = EncoderFactory.get().jsonEncoder(record.getSchema(), out);
    _writer.write(msg, encoder);
    encoder.flush();
    writer.write(out.toString("UTF-8"));
}
项目:reef    文件:YarnSubmissionParametersFileGenerator.java   
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();
}
项目:reef    文件:AvroDriverInfoSerializer.java   
/**
 * Convert AvroDriverInfo object to JSON string.
 */
@Override
public String toString(final AvroDriverInfo avroDriverInfo) {
  final DatumWriter<AvroDriverInfo> driverWriter = new SpecificDatumWriter<>(AvroDriverInfo.class);
  try (final 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);
  }
}
项目:reef    文件:AvroEvaluatorInfoSerializer.java   
/**
 * Convert AvroEvaluatorsInfo object to JSON string.
 */
@Override
public String toString(final AvroEvaluatorsInfo avroEvaluatorsInfo) {
  final DatumWriter<AvroEvaluatorsInfo> evaluatorWriter = new SpecificDatumWriter<>(AvroEvaluatorsInfo.class);
  try (final 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);
  }
}
项目:reef    文件:AvroEvaluatorListSerializer.java   
/**
 * Convert AvroEvaluatorList to JSON string.
 */
@Override
public String toString(final AvroEvaluatorList avroEvaluatorList) {
  final DatumWriter<AvroEvaluatorList> evaluatorWriter = new SpecificDatumWriter<>(AvroEvaluatorList.class);
  try (final 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);
  }
}
项目:Pipe    文件:Serializer.java   
public <T extends SpecificRecordBase> ByteArrayOutputStream serializer(T request, Class<T> clazz) throws IOException {
    SpecificDatumWriter<T> writer = new SpecificDatumWriter<T>(clazz);
    ByteArrayOutputStream os = new ByteArrayOutputStream();

    try {
        JsonEncoder encoder = EncoderFactory.get().jsonEncoder(request.getSchema(), os);
        writer.write(request, encoder);
        encoder.flush();

        return os;
    } catch (Exception e) {
        throw new IOException("failed to serialize object of type : " + clazz.getSimpleName());
    }
}
项目:gora-0.3-simplified    文件:AvroStore.java   
protected Encoder createEncoder() throws IOException {
  switch(codecType) {
    case BINARY:
      return new BinaryEncoder(getOrCreateOutputStream());
    case JSON:
      return new JsonEncoder(schema, getOrCreateOutputStream());
  }
  return null;
}
项目:play-plugins    文件:AvroHelper.java   
public static String toJson(Schema schema, Object object) throws IOException {
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  JsonGenerator generator =
      new JsonFactory().createJsonGenerator(outputStream, JsonEncoding.UTF8);
  generator.useDefaultPrettyPrinter();
  SpecificDatumWriter<Object> writer = new SpecificDatumWriter<>(schema);
  JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, generator);
  writer.write(object, encoder);
  encoder.flush();
  String json = outputStream.toString();
  return AvroHelper.convertToSimpleRecord(schema, json);
}
项目:AvroToolbox    文件:AvroToJson.java   
public final static void main(final String[] args) throws IOException, InterruptedException
{
    if (args.length == 0)
    {
        System.err.format("Usage: %s scheme://host:port/path/to/file.avro [count]\n", AvroToJson.class.getSimpleName());
    }
    else
    {
        int count = args.length == 2 ? Integer.parseInt(args[1]) : -1;
        final GenericDatumReader<Object> reader = new GenericDatumReader<Object>();
        final Path path = new Path(args[0]);
        final FsInput input = new FsInput(path, new Configuration());
        final FileReader<Object> fileReader = DataFileReader.openReader(input, reader);
        try
        {
            final Schema schema = fileReader.getSchema();
            final DatumWriter<Object> writer = new GenericDatumWriter<Object>(schema);
            final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, System.out);
            for (final Object datum : fileReader)
            {
                writer.write(datum, encoder);
                if (--count == 0)
                {
                    break;
                }
            }
            encoder.flush();
            System.out.println();
        }
        finally
        {
            fileReader.close();
        }
    }
}
项目:mapreduce-fork    文件:EventWriter.java   
EventWriter(FSDataOutputStream out) throws IOException {
  this.out = out;
  out.writeBytes(VERSION);
  out.writeBytes("\n");
  out.writeBytes(Event.SCHEMA$.toString());
  out.writeBytes("\n");
  this.encoder = new JsonEncoder(Event.SCHEMA$, out);
}
项目:gora-oraclenosql    文件:AvroStore.java   
protected Encoder createEncoder() throws IOException {
  switch(codecType) {
    case BINARY:
      return new BinaryEncoder(getOrCreateOutputStream());
    case JSON:
      return new JsonEncoder(schema, getOrCreateOutputStream());
  }
  return null;
}
项目:avro    文件:AvroFileWriter01.java   
public static void main(String[] args) throws Exception {

        MyRecord myRecord = AvroUtils.createMyRecord();

        DatumWriter<MyRecord> dw = new SpecificDatumWriter<>();
        dw.setSchema(myRecord.getSchema());

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        JsonEncoder json = EncoderFactory.get().jsonEncoder(myRecord.getSchema(), out);
        dw.write(myRecord, json);
        dw.write(myRecord, json);

        System.out.println("\n> Dump the two objects. They will be serialized in sequence as JSON.");
        HexDump.dump(out.toByteArray(), 0, System.out, 0);

        // Empty the buffer.
        out.reset();

        BinaryEncoder bin = EncoderFactory.get().directBinaryEncoder(out, null);
        dw.write(myRecord, bin);
        dw.write(myRecord, bin);

        System.out.println("\n> Dump the two objects. They will be serialized in sequence as BINARY.");
        HexDump.dump(out.toByteArray(), 0, System.out, 0);

    }