• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java GenericAvroCodecs类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: deserialize

import com.twitter.bijection.avro.GenericAvroCodecs; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public T deserialize(String topic, byte[] data) {
  LOGGER.debug("data to deserialize='{}'", DatatypeConverter.printHexBinary(data));
  try {
    // get the schema
    Schema schema = targetType.newInstance().getSchema();

    Injection<GenericRecord, byte[]> genericRecordInjection = GenericAvroCodecs.toBinary(schema);
    GenericRecord genericRecord = genericRecordInjection.invert((byte[]) data).get();
    T result = (T) SpecificData.get().deepCopy(schema, genericRecord);

    LOGGER.debug("data='{}'", result);
    return result;
  } catch (Exception e) {
    throw new SerializationException(
        "Can't deserialize data [" + Arrays.toString(data) + "] from topic [" + topic + "]", e);
  }
}
 
开发者ID:code-not-found,项目名称:spring-kafka,代码行数:20,代码来源:AvroDeserializer.java


示例2: run

import com.twitter.bijection.avro.GenericAvroCodecs; //导入依赖的package包/类
/**
 *
 */
@Override
public void run() {
  Properties props = HopsUtil.getKafkaProperties().getConsumerConfig();
  consumer = new KafkaConsumer<>(props);
  //Subscribe to the Kafka topic
  consumer.subscribe(Collections.singletonList(topic));
  while (consume) {
    ConsumerRecords<Integer, String> records = consumer.poll(1000);
    //Get the records
    for (ConsumerRecord<Integer, String> record : records) {
      //Convert the record using the schema
      Injection<GenericRecord, byte[]> recordInjection = GenericAvroCodecs.toBinary(schema);
      GenericRecord genericRecord = recordInjection.invert(record.value().getBytes()).get();
      LOGGER.log(Level.INFO, "Consumer received message:{0}", genericRecord);
    }
  }
}
 
开发者ID:hopshadoop,项目名称:hops-util,代码行数:21,代码来源:DelaConsumer.java


示例3: serialize

import com.twitter.bijection.avro.GenericAvroCodecs; //导入依赖的package包/类
/**
 * 
 * @param t
 * @return 
 */
@Override
public byte[] serialize(Tuple4<String, String, String, String> t) {

  if (!initialized) {
    parser = new Schema.Parser();
    schema = parser.parse(schemaJson);
    recordInjection = GenericAvroCodecs.toBinary(schema);
    initialized = true;
  }
  GenericData.Record avroRecord = new GenericData.Record(schema);
  for (int i = 0; i < t.getArity() - 1; i += 2) {
    avroRecord.put(t.getField(i).toString(), t.getField(i + 1).toString());
  }

  byte[] bytes = recordInjection.apply(avroRecord);
  return bytes;
}
 
开发者ID:hopshadoop,项目名称:hops-util,代码行数:23,代码来源:AvroDeserializer.java


示例4: run

import com.twitter.bijection.avro.GenericAvroCodecs; //导入依赖的package包/类
public void run() {
	try {
		ConsumerIterator<byte[], byte[]> it = m_stream.iterator();
		Injection<GenericRecord, byte[]> recordInjection = GenericAvroCodecs.toBinary(User.getClassSchema());

		while (it.hasNext()) {
			MessageAndMetadata<byte[], byte[]> messageAndMetadata = it.next();
			String key = new String(messageAndMetadata.key());
			User user = genericRecordToUser(recordInjection.invert(messageAndMetadata.message()).get());
			// User user = (User)
			// recordInjection.invert(messageAndMetadata.message()).get();
			String summary = "Thread " + m_threadNumber + ", topic=" + messageAndMetadata.topic() + ", partition="
					+ messageAndMetadata.partition() + ", key=" + key + ", user=" + user.toString() + ", offset="
					+ messageAndMetadata.offset() + ", timestamp=" + messageAndMetadata.timestamp()
					+ ", timestampType=" + messageAndMetadata.timestampType();
			System.out.println(summary);
		}
		System.out.println("Shutting down Thread: " + m_threadNumber);
	} catch (Exception e) {
		System.out.println("Exception in thread "+m_threadNumber);
		System.out.println(e);
		e.printStackTrace();
	}
}
 
开发者ID:petezybrick,项目名称:iote2e,代码行数:25,代码来源:AvroConsumerThread.java


示例5: main

import com.twitter.bijection.avro.GenericAvroCodecs; //导入依赖的package包/类
/**
 *
 * @param args
 * @throws InterruptedException
 */
public static void main(String[] args) throws InterruptedException {
    Properties props = new Properties();
    props.put("bootstrap.servers", "localhost:9092");
    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    props.put("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");

    Schema.Parser parser = new Schema.Parser();
    Schema schema = parser.parse(USER_SCHEMA);
    Injection<GenericRecord, byte[]> recordInjection = GenericAvroCodecs.toBinary(schema);

    KafkaProducer<String, byte[]> producer = new KafkaProducer<>(props);
    SplittableRandom random = new SplittableRandom();

    while (true) {
        GenericData.Record avroRecord = new GenericData.Record(schema);
        avroRecord.put("str1", "Str 1-" + random.nextInt(10));
        avroRecord.put("str2", "Str 2-" + random.nextInt(1000));
        avroRecord.put("int1", random.nextInt(10000));

        byte[] bytes = recordInjection.apply(avroRecord);

        ProducerRecord<String, byte[]> record = new ProducerRecord<>("mytopic", bytes);
        producer.send(record);
        Thread.sleep(100);
    }

}
 
开发者ID:Neuw84,项目名称:structured-streaming-avro-demo,代码行数:33,代码来源:GeneratorDemo.java


示例6: DelaProducer

import com.twitter.bijection.avro.GenericAvroCodecs; //导入依赖的package包/类
/**
 *
 * @param topic
 * @param schema
 * @param lingerDelay
 */
public DelaProducer(String topic, Schema schema, long lingerDelay) {
  super(HopsProcessType.PRODUCER, topic, schema);
  Properties props = HopsUtil.getKafkaProperties().defaultProps();
  props.put(ProducerConfig.CLIENT_ID_CONFIG, "DelaProducer");
  props.put(ProducerConfig.LINGER_MS_CONFIG, lingerDelay);
  producer = new KafkaProducer<>(props);
  recordInjection = GenericAvroCodecs.toBinary(schema);
}
 
开发者ID:hopshadoop,项目名称:hops-util,代码行数:15,代码来源:DelaProducer.java


示例7: HopsProducer

import com.twitter.bijection.avro.GenericAvroCodecs; //导入依赖的package包/类
/**
 * Create a Producer to stream messages to Kafka.
 *
 * @param topic
 * @param userProps
 * @throws SchemaNotFoundException
 * @throws io.hops.util.CredentialsNotFoundException
 */
public HopsProducer(String topic, Properties userProps) throws SchemaNotFoundException, CredentialsNotFoundException {
  super(HopsProcessType.PRODUCER, topic);
  Properties props = HopsUtil.getKafkaProperties().defaultProps();
  props.put(ProducerConfig.CLIENT_ID_CONFIG, "HopsProducer");
  if (userProps != null) {
    props.putAll(userProps);
  }
  producer = new KafkaProducer<>(props);
  recordInjection = GenericAvroCodecs.toBinary(schema);
}
 
开发者ID:hopshadoop,项目名称:hops-util,代码行数:19,代码来源:HopsProducer.java


示例8: deserialize

import com.twitter.bijection.avro.GenericAvroCodecs; //导入依赖的package包/类
/**
 * 
 * @param bytes
 * @return
 * @throws IOException 
 */
@Override
public String deserialize(byte[] bytes) throws IOException {
  if (!initialized) {
    parser = new Schema.Parser();
    schema = parser.parse(schemaJson);
    recordInjection = GenericAvroCodecs.toBinary(schema);
    initialized = true;
  }
  GenericRecord genericRecord = recordInjection.invert(bytes).get();
  return genericRecord.toString().replaceAll("\\\\u001A", "");
}
 
开发者ID:hopshadoop,项目名称:hops-util,代码行数:18,代码来源:AvroDeserializer.java


示例9: getRecordInjections

import com.twitter.bijection.avro.GenericAvroCodecs; //导入依赖的package包/类
/**
 * Get record Injections for Avro messages, using topics extracted from Spark
 * HopsWorks Jobs UI.
 *
 * @return
 */
public static Map<String, Injection<GenericRecord, byte[]>> getRecordInjections() {
  Map<String, Schema> schemas = HopsUtil.getSchemas();
  if (schemas.isEmpty()) {
    return null;
  }
  Map<String, Injection<GenericRecord, byte[]>> recordInjections
      = new HashMap<>();
  for (String topic : HopsUtil.getTopics()) {
    recordInjections.
        put(topic, GenericAvroCodecs.toBinary(schemas.get(topic)));

  }
  return recordInjections;
}
 
开发者ID:hopshadoop,项目名称:hops-util,代码行数:21,代码来源:HopsUtil.java


示例10: main

import com.twitter.bijection.avro.GenericAvroCodecs; //导入依赖的package包/类
public static void main(String[] args) throws InterruptedException {
    Properties props = new Properties();
    props.put("bootstrap.servers", "localhost:9092");
    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    props.put("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");

    Schema.Parser parser = new Schema.Parser();
    Schema schema = parser.parse(USER_SCHEMA);
    Injection<GenericRecord, byte[]> recordInjection = GenericAvroCodecs.toBinary(schema);

    KafkaProducer<String, byte[]> producer = new KafkaProducer<>(props);
    for (int i = 0; i < 1000; i++) {
        GenericData.Record avroRecord = new GenericData.Record(schema);
        avroRecord.put("str1", "Str 1-" + i);
        avroRecord.put("str2", "Str 2-" + i);
        avroRecord.put("int1", i);

        byte[] bytes = recordInjection.apply(avroRecord);

        ProducerRecord<String, byte[]> record = new ProducerRecord<>("mytopic", bytes);
        producer.send(record);

        Thread.sleep(250);
    }

    producer.close();
}
 
开发者ID:aseigneurin,项目名称:kafka-sandbox,代码行数:28,代码来源:SimpleAvroProducer.java


示例11: main

import com.twitter.bijection.avro.GenericAvroCodecs; //导入依赖的package包/类
/**
 * The main method.
 *
 * @param args the arguments
 */
public static void main(String[] args) {
	LogTool.initConsole();
    long events = Long.parseLong(args[0]);
    Random rnd = new Random();
 
    Properties props = new Properties();
    //props.put("metadata.broker.list", "hp-lt-ubuntu-1:9092");
    props.put("bootstrap.servers", "hp-lt-ubuntu-1:9092");
    props.put("producer.type", "sync");
    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    props.put("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");
    //props.put("partition.assignment.strategy", "range");
    //props.put("partitioner.class", "com.pzybrick.kafka1.training.KafkaPartitionerTest");
    props.put("request.required.acks", "1");
    props.put("group.id", "group1");
 
    //Injection<GenericRecord, byte[]> recordInjection = GenericAvroCodecs.toBinary(User.getClassSchema());
    Injection<GenericRecord, byte[]> recordInjectionUser = GenericAvroCodecs.toBinary(User.getClassSchema());
    
    KafkaProducer<String, byte[]> producer = new KafkaProducer<String, byte[]>(props);
    long keyNum = System.currentTimeMillis();
 
    for (long nEvents = 0; nEvents < events; nEvents++) { 
    	   System.out.println("creating event "+nEvents);
           String key = String.valueOf(keyNum);
           
           User user = User.newBuilder().setName("name"+keyNum).setFavoriteColor("color"+keyNum).setFavoriteNumber((int)keyNum).build();
           byte[] bytes = recordInjectionUser.apply(user);
           ProducerRecord<String, byte[]> data = new ProducerRecord<String, byte[]>("pzmultiptn1", key, bytes);
           producer.send(data);
           keyNum++;
    }
    producer.close();
}
 
开发者ID:petezybrick,项目名称:iote2e,代码行数:40,代码来源:AvroProducer.java


示例12: serialize

import com.twitter.bijection.avro.GenericAvroCodecs; //导入依赖的package包/类
@Override
public byte[] serialize(String topic, T data) {
  LOGGER.debug("data to serialize='{}'", data);

  Injection<GenericRecord, byte[]> genericRecordInjection =
      GenericAvroCodecs.toBinary(data.getSchema());
  byte[] result = genericRecordInjection.apply(data);

  LOGGER.debug("serialized data='{}'", DatatypeConverter.printHexBinary(result));
  return result;
}
 
开发者ID:code-not-found,项目名称:spring-kafka,代码行数:12,代码来源:AvroSerializer.java


示例13: SerializedPersistedData

import com.twitter.bijection.avro.GenericAvroCodecs; //导入依赖的package包/类
/**
 * Instantiates a new serialized persisted data.
 *
 * @param uuid the uuid
 * @param datatype
 * @param avroSchema the avro schema
 * @param serializedData the serialized data
 * @param timestamp the timestamp
 */
public SerializedPersistedData(UUID uuid, String datatype, String avroSchema, byte[] serializedData, Date timestamp, UUID freedomoticInstanceId) {
    super();
    this.uuid = uuid;
    this.datatype = datatype;
    this.avroSchema = new Schema.Parser().parse(avroSchema);
    this.serializedData = serializedData;
    this.persistenceTimestamp = timestamp;
    this.recordInjection = GenericAvroCodecs.toBinary(this.avroSchema);
    this.freedomoticInstanceId = freedomoticInstanceId;
}
 
开发者ID:freedomotic,项目名称:freedomotic,代码行数:20,代码来源:SerializedPersistedData.java


示例14: main

import com.twitter.bijection.avro.GenericAvroCodecs; //导入依赖的package包/类
public static void main(String[] args) {


        Schema schema = User.SCHEMA$;

        Injection<GenericRecord, byte[]> recordInjection = GenericAvroCodecs.toBinary(schema);
        User  user=    new User("1",1,"");

        byte[] bytes = recordInjection.apply(user);
        GenericRecord record = recordInjection.invert(bytes).get();

        Object name = record.get("name");

        System.out.println(record);

        }
 
开发者ID:spafka,项目名称:spark_deep,代码行数:17,代码来源:AvroUseAvsc.java



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java FailOnThreadViolationRepaintManager类代码示例发布时间:2022-05-23
下一篇:
Java ChangeDescriptor类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap