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


Java DatumWriter.setSchema方法代码示例

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


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

示例1: getJsonString

import org.apache.avro.io.DatumWriter; //导入方法依赖的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

示例2: serializeAvro

import org.apache.avro.io.DatumWriter; //导入方法依赖的package包/类
protected  byte[] serializeAvro( GenericData.Record record,  Schema schema, String topic,  byte opType) throws IOException {
       short schemaId = getSchemaId(topic, schema);
    EncoderFactory encoderFactory = EncoderFactory.get();
    DatumWriter<GenericRecord> writer  = new GenericDatumWriter<GenericRecord>();
    writer.setSchema(schema);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    out.write(PROTO_MAGIC_V0);
    out.write(ByteBuffer.allocate(opTypeSize).put(opType).array() );
    out.write(ByteBuffer.allocate(idSize).putShort(schemaId).array());
    BinaryEncoder enc = encoderFactory.binaryEncoder(out, null);
    writer.write(record, enc);
    enc.flush();
    return out.toByteArray();
}
 
开发者ID:rogers,项目名称:change-data-capture,代码行数:15,代码来源:GenericAvroMutationSerializer.java

示例3: serializeToByteBuffer

import org.apache.avro.io.DatumWriter; //导入方法依赖的package包/类
/**
 * Serialize Avro data to a in-memory ByteBuffer.
 * @return A ByteBuffer that contains avro data.
 * @throws IOException if the parquet file couldn't be parsed correctly.
 */
public ByteBuffer serializeToByteBuffer() throws IOException {
  final ByteArrayOutputStream stream = new ByteArrayOutputStream();
  final Encoder encoder = EncoderFactory.get().binaryEncoder(stream, null);
  final DatumWriter writer = new GenericDatumWriter<GenericRecord>();
  writer.setSchema(createAvroSchema());
  final AvroParquetReader<GenericRecord> reader = createAvroReader();

  GenericRecord record = reader.read();
  while (record != null) {
    writer.write(record, encoder);
    record = reader.read();
  }

  try {
    reader.close();
  } catch (IOException ex){
    LOG.log(Level.SEVERE, ex.getMessage());
    throw ex;
  }

  encoder.flush();
  final ByteBuffer buf = ByteBuffer.wrap(stream.toByteArray());
  buf.order(ByteOrder.LITTLE_ENDIAN);
  return buf;
}
 
开发者ID:apache,项目名称:reef,代码行数:31,代码来源:ParquetReader.java

示例4: main

import org.apache.avro.io.DatumWriter; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {

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

        ByteArrayOutputStream buf = new ByteArrayOutputStream();

        DataFileWriter<MyRecord> fw = new DataFileWriter<>(dw);

        fw.create(MyRecord.getClassSchema(), buf, MyConstants.MY_SYNC);

        fw.append(AvroUtils.createMyRecord());
        fw.append(AvroUtils.createMyRecord());
        fw.fSync();
        fw.append(AvroUtils.createMyRecord());

        // Will it work? No, it doesn't work because it adds a signature and schema CRC in the beginning.
        //fw.appendEncoded(schema.AvroUtils.createMyRecord().toByteBuffer());

        // Remove the first 10 bytes. They are the object header.
        byte[] arr = AvroUtils.createMyRecord().toByteBuffer().array();
        arr = Arrays.copyOfRange(arr, MyConstants.HEADER_SIZE, arr.length);

        // Append the raw bytes.
        fw.appendEncoded(ByteBuffer.wrap(arr));

        // Close the file. It will add a sync block.
        fw.close();

        System.out.println("\n> Dump the file content.");
        HexDump.dump(buf.toByteArray(), 0, System.out, 0);

        Files.write(Paths.get(MyConstants.AVRO_FILE_PATH), buf.toByteArray(), StandardOpenOption.TRUNCATE_EXISTING);
    }
 
开发者ID:glzbcrt,项目名称:avro,代码行数:35,代码来源:AvroFileWriter02.java

示例5: main

import org.apache.avro.io.DatumWriter; //导入方法依赖的package包/类
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);

    }
 
开发者ID:glzbcrt,项目名称:avro,代码行数:28,代码来源:AvroFileWriter01.java


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