当前位置: 首页>>代码示例>>Java>>正文


Java JsonEncoder类代码示例

本文整理汇总了Java中org.apache.avro.io.JsonEncoder的典型用法代码示例。如果您正苦于以下问题:Java JsonEncoder类的具体用法?Java JsonEncoder怎么用?Java JsonEncoder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


JsonEncoder类属于org.apache.avro.io包,在下文中一共展示了JsonEncoder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: convertAvroToJson

import org.apache.avro.io.JsonEncoder; //导入依赖的package包/类
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();
}
 
开发者ID:rkluszczynski,项目名称:avro-cli,代码行数:17,代码来源:RawConverterUtil.java

示例2: getJsonStringFromRecord

import org.apache.avro.io.JsonEncoder; //导入依赖的package包/类
@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);
    }
}
 
开发者ID:kaaproject,项目名称:avro-ui,代码行数:20,代码来源:AvroUiSandboxServiceImpl.java

示例3: writeTo

import org.apache.avro.io.JsonEncoder; //导入依赖的package包/类
@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"));
}
 
开发者ID:quantiply,项目名称:rico,代码行数:11,代码来源:AvroStringSerde.java

示例4: writeAvroYarnJobSubmissionParametersToOutputStream

import org.apache.avro.io.JsonEncoder; //导入依赖的package包/类
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();
}
 
开发者ID:apache,项目名称:reef,代码行数:17,代码来源:YarnSubmissionParametersFileGenerator.java

示例5: convert

import org.apache.avro.io.JsonEncoder; //导入依赖的package包/类
@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()));
}
 
开发者ID:collectivemedia,项目名称:celos,代码行数:24,代码来源:AvroToJsonConverter.java

示例6: getObject

import org.apache.avro.io.JsonEncoder; //导入依赖的package包/类
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;
}
 
开发者ID:pyvandenbussche,项目名称:sparqles,代码行数:17,代码来源:MongoDBManager.java

示例7: getJsonString

import org.apache.avro.io.JsonEncoder; //导入依赖的package包/类
/**
 * 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;
}
 
开发者ID:kijiproject,项目名称:kiji-rest,代码行数:24,代码来源:AvroToJsonStringSerializer.java

示例8: writeTo

import org.apache.avro.io.JsonEncoder; //导入依赖的package包/类
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);
  }
}
 
开发者ID:thomas-young-2013,项目名称:wherehowsX,代码行数:15,代码来源:AvroMessageFormatter.java

示例9: jsonFromGenericRecord

import org.apache.avro.io.JsonEncoder; //导入依赖的package包/类
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();
}
 
开发者ID:linkedin,项目名称:kafka-monitor,代码行数:14,代码来源:Utils.java

示例10: toJson

import org.apache.avro.io.JsonEncoder; //导入依赖的package包/类
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();
}
 
开发者ID:tfeng,项目名称:toolbox,代码行数:9,代码来源:AvroHelper.java

示例11: generateInputJSON

import org.apache.avro.io.JsonEncoder; //导入依赖的package包/类
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);
}
 
开发者ID:Talend,项目名称:components,代码行数:10,代码来源:FixedFlowInputRuntimeTest.java

示例12: generateWorksheet

import org.apache.avro.io.JsonEncoder; //导入依赖的package包/类
@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();
}
 
开发者ID:therelaxist,项目名称:spring-usc,代码行数:35,代码来源:AvroImport.java

示例13: writeAvroYarnAppSubmissionParametersToOutputStream

import org.apache.avro.io.JsonEncoder; //导入依赖的package包/类
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();
}
 
开发者ID:apache,项目名称:reef,代码行数:14,代码来源:YarnSubmissionParametersFileGenerator.java

示例14: toString

import org.apache.avro.io.JsonEncoder; //导入依赖的package包/类
/**
 * 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);
  }
}
 
开发者ID:apache,项目名称:reef,代码行数:16,代码来源:AvroDriverInfoSerializer.java

示例15: toString

import org.apache.avro.io.JsonEncoder; //导入依赖的package包/类
/**
 * 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);
  }
}
 
开发者ID:apache,项目名称:reef,代码行数:16,代码来源:AvroEvaluatorInfoSerializer.java


注:本文中的org.apache.avro.io.JsonEncoder类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。