如何优化多层次Avro生产者代码

时间:2018-04-13 01:16:37

标签: apache-kafka avro

以下是我的Kafka Avro生产者代码,因为我需要发送一个包含数组和记录类型的记录,所以我使用了三个模式。它有效,但它看起来很乱,有没有什么好方法可以使代码更干净整洁。

public static void main(String[] args) throws Exception {

    String ip = "localhost";
    String url = "http://"+ip+":8081";
    Properties props = new Properties();
    props.put("bootstrap.servers", ip+":9092");

    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    props.put("value.serializer", "io.confluent.kafka.serializers.KafkaAvroSerializer");
    props.put("schema.registry.url", url);

    String schemaRecord = "{\"namespace\": \"rdma.avro\", \"type\": \"record\", " + "\"name\": \"iprecord\","
            + "\"fields\": [" 
            + "{\"name\": \"ip\", \"type\": \"string\"},"
            + "{\"name\": \"count\", \"type\": \"long\"}"
            + "]}";

    String schemaArray = "{\"type\":\"array\", \"items\":{"
            + "\"name\": \"iprecord\","
            + "\"type\": \"record\","
            + "\"fields\": [" 
            + "{\"name\": \"ip\", \"type\": \"string\"},"
            + "{\"name\": \"count\", \"type\": \"long\"}"
            + "]}}";

    String schemaArraySup = "{\"namespace\": \"rdma.avro\", \"name\":\"ipsuper\", \"type\":\"record\",\"fields\":[{ \"name\":\"child\", \"type\": {\"type\":\"array\", \"items\":{"
            + "\"name\": \"iprecord\","
            + "\"type\": \"record\","
            + "\"fields\": [" 
            + "{\"name\": \"ip\", \"type\": \"string\"},"
            + "{\"name\": \"count\", \"type\": \"long\"}"
            + "]}}}]}";


    Producer<String, GenericRecord> producer = new KafkaProducer<String, GenericRecord>(props);

    Schema.Parser parser = new Schema.Parser();
    Schema recordSchema = parser.parse(schemaRecord);


    Schema.Parser parser2 = new Schema.Parser();
    Schema arrSchema = parser2.parse(schemaArray);

    Schema.Parser parser3 = new Schema.Parser();
    Schema arrSchemaSup = parser3.parse(schemaArraySup);


    GenericRecord page = new GenericData.Record(recordSchema);
    page.put("ip","ip100");
    page.put("count",100L);

    GenericRecord page2= new GenericData.Record(recordSchema);
    page2.put("ip","ip200");
    page2.put("count",200L);

    GenericArray av_arr =  new GenericData.Array(2,arrSchema);
    av_arr.add(page);
    av_arr.add(page2);

    GenericRecord pageSuper = new GenericData.Record(arrSchemaSup);
    pageSuper.put("child", av_arr);

    ProducerRecord<String, GenericRecord> data = new ProducerRecord<String, GenericRecord>("avro_test2",pageSuper);

    producer.send(data).get();

    producer.close();
}

0 个答案:

没有答案