當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。