本文整理汇总了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();
}
示例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);
}
}
示例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"));
}
示例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();
}
示例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()));
}
示例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;
}
示例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;
}
示例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);
}
}
示例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();
}
示例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();
}
示例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);
}
示例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();
}
示例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();
}
示例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);
}
}
示例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);
}
}