gpt4 book ai didi

java - 如何像使用 avro console producer 一样生成 Kafka avro 记录?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:48:22 27 4
gpt4 key购买 nike

我正在使用 Confluent 3.3.0。我的目的是使用 kafka-connect 将 Kafka 主题中的值插入到 Oracle 表中。我的连接器与我使用 avro console producer 制作的 avro 记录配合得很好,如下所示:

./kafka-avro-console-producer --broker-list 192.168.0.1:9092 --topic topic6 --property value.schema='{"type":"record","name":"flights3","fields":[{"name":"flight_id","type":"string"},{"name":"flight_to", "type": "string"}, {"name":"flight_from", "type": "string"}]}'

然后我插入如下值:

{"flight_id":"1","flight_to":"QWE","flight_from":"RTY"}

我想要实现的是使用 Java 应用程序使用对象插入相同的数据。以下是我的生产者代码:

public class Sender {
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "192.168.0.1:9092");
props.put("acks", "all");
props.put("retries", 0);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "serializers.custom.FlightSerializer");
props.put("schema.registry.url", "http://192.168.0.1:8081");
Producer<String, Flight> producer = new KafkaProducer<String, Flight>(props);
Flight myflight = new Flight("testflight1","QWE","RTY");
ProducerRecord<String, Flight> record = new ProducerRecord<String, Flight>("topic5","key",myflight);

try {
producer.send(record).get();
} catch (Exception e) {
e.printStackTrace();
}
}
}

下面是航类旁白:

package vo;

public class Flight {
String flight_id,flight_to,flight_from;

public Flight(String flight_id, String flight_to, String flight_from) {
this.flight_id = flight_id;
this.flight_to = flight_to;
this.flight_from = flight_from;
}

public Flight(){
}

public String getFlight_id() {
return flight_id;
}

public void setFlight_id(String flight_id) {
this.flight_id = flight_id;
}

public String getFlight_to() {
return flight_to;
}

public void setFlight_to(String flight_to) {
this.flight_to = flight_to;
}

public String getFlight_from() {
return flight_from;
}

public void setFlight_from(String flight_from) {
this.flight_from = flight_from;
}
}

最后是序列化程序:

package serializers.custom;

import java.util.Map;
import org.apache.kafka.common.serialization.Serializer;
import vo.Flight;
import com.fasterxml.jackson.databind.ObjectMapper;

public class FlightSerializer implements Serializer<Flight> {
@Override
public void close() {
}

@Override
public void configure(Map<String, ?> arg0, boolean arg1) {
}

@Override
public byte[] serialize(String arg0, Flight arg1) {
byte[] retVal = null;
ObjectMapper objectMapper = new ObjectMapper();

try {
retVal = objectMapper.writeValueAsString(arg1).getBytes();
} catch (Exception e) {
e.printStackTrace();
}

return retVal;
}
}

但我的理解是,需要定义模式之类的东西,并使用一些 avro 序列化器来获取精确数据,就像我使用 avro console consumer 所做的那样.我浏览了一些示例代码,但没有一个对我有用。

编辑

我尝试了以下代码。但是 avro console consumer 什么也没有。

package producer.serialized.avro;

import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericRecord;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import vo.Flight;
import java.util.Properties;

public class Sender {
public static void main(String[] args) {
String flightSchema = "{\"type\":\"record\"," + "\"name\":\"flights\","
+ "\"fields\":[{\"name\":\"flight_id\",\"type\":\"string\"},{\"name\":\"flight_to\",\"type\":\"string\"},{\"name\":\"flight_from\",\"type\":\"string\"}]}";
Properties props = new Properties();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.0.1:9092");
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,io.confluent.kafka.serializers.KafkaAvroSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,io.confluent.kafka.serializers.KafkaAvroSerializer.class);
props.put("schema.registry.url", "http://192.168.0.1:8081");
KafkaProducer producer = new KafkaProducer(props);
Schema.Parser parser = new Schema.Parser();
Schema schema = parser.parse(flightSchema);
GenericRecord avroRecord = new GenericData.Record(schema);
avroRecord.put("flight_id", "1");
avroRecord.put("flight_to", "QWE");
avroRecord.put("flight_from", "RTY");
ProducerRecord<String, GenericRecord> record = new ProducerRecord<>("topic6", avroRecord);

try {
producer.send(record);
} catch (Exception e) {
e.printStackTrace();
}
}
}

最佳答案

架构未定义,因此当 KafkaAvroSerializer 必须联系架构注册表以提交架构时,它不会拥有它。

你必须为你的对象 Flight 创建一个模式

下面的 file.avdl(avro 扩展文件之一)示例就可以了:

@namespace("vo")
protocol FlightSender {

record Flight {
union{null, string} flight_id = null;
union{null, string} flight_to = null;
union{null, string} flight_from = null;
}
}

参见 Avro IDL docs

At compile time, when you use the avro-maven-plugin ,上面的 avro 架构将生成您的 java Flight 类,因此您必须删除之前创建的类。

当它涉及到你的主类时,你必须像下面这样设置两个属性:

props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,io.confluent.kafka.serializers.KafkaAvroSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,io.confluent.kafka.serializers.KafkaAvroSerializer.class);

而您的制作人,您可以使用您生成的特定 Avro 类

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

希望对您有所帮助:-)

关于java - 如何像使用 avro console producer 一样生成 Kafka avro 记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48195856/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com