Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 阿夫罗和卡夫卡利用SchemaBuilder_Java_Apache Kafka_Avro - Fatal编程技术网

Java 阿夫罗和卡夫卡利用SchemaBuilder

Java 阿夫罗和卡夫卡利用SchemaBuilder,java,apache-kafka,avro,Java,Apache Kafka,Avro,我从一开始就阅读了教程。他们提到有两种创建模式的方法 通过编写json表示并添加maven插件来生成类 通过使用他们提到的SchemaBuilder,这是一个更好的选择 不幸的是,在git示例中,我只看到json的方式 假设我有一个Avro模式: { "type":"record", "name":"TestFile", "namespace":"com.example.kafka.data.ingestion.model", "fields":[ { "

我从一开始就阅读了教程。他们提到有两种创建模式的方法

  • 通过编写json表示并添加maven插件来生成类
  • 通过使用他们提到的
    SchemaBuilder
    ,这是一个更好的选择
不幸的是,在git示例中,我只看到json的方式

假设我有一个Avro模式:

{
  "type":"record",
  "name":"TestFile",
  "namespace":"com.example.kafka.data.ingestion.model",
  "fields":[
    {
      "name":"date",
      "type":"long"
    },
    {
      "name":"counter",
      "type":"int"
    },
    {
      "name":"mc",
      "type":"string"
    }
  ]
}
通过在我的pom文件中添加此插件:

<plugin>
   <groupId>org.apache.avro</groupId>
   <artifactId>avro-maven-plugin</artifactId>
   <version>1.8.0</version>
   <executions>
      <execution>
         <id>schemas</id>
         <phase>generate-sources</phase>
         <goals>
            <goal>schema</goal>
            <goal>protocol</goal>
            <goal>idl-protocol</goal>
         </goals>
         <configuration>
            <sourceDirectory>${project.basedir}/src/main/resources/</sourceDirectory>
            <outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
         </configuration>
      </execution>
   </executions>
</plugin>
使用
SchemaBuilder
创建模式的等效方法是:

Schema testFileSchema = SchemaBuilder   .record("TestFile")
                                            .namespace("com.example.kafka.data.ingestion.model")
                                            .fields()
                                            .requiredLong("date")
                                            .requiredInt("counter")
                                            .requiredString("mc")
                                            .endRecord();

但是我现在如何生成POJO并将我的
TestFile
数据发送到我的kafka主题?

您将无法访问
TestFile
对象,因为模式是在运行时创建的,而不是预编译的。如果您想保留该POJO,那么您需要为
公共测试文件(genericord avroRecord)

您需要使用该
Schema
对象创建一个
genericord
,就像从字符串或文件解析它一样

比如说,

Schema schema = SchemaBuilder.record("TestFile")
            .namespace("com.example.kafka.data.ingestion.model")
            .fields()
            .requiredLong("date")
            .requiredInt("counter")
            .requiredString("mc")
            .endRecord();

GenericRecord entry1 = new GenericData.Record(schema);
entry1.put("date", 1L);
entry1.put("counter", 2);
entry1.put("mc", "3");

// producer.send(new ProducerRecord<>(topic, entry1);
Schema Schema=schemabilder.record(“TestFile”)
.namespace(“com.example.kafka.data.ingestion.model”)
.fields()
.requiredLong(“日期”)
.要求(“计数器”)
.requiredString(“mc”)
.endRecord();
GenericRecord entry1=新的GenericData.Record(模式);
条目1.输入(“日期”,1L);
入口1.放置(“计数器”,2);
分录1.put(“mc”、“3”);
//producer.send(新ProducerRecord(主题,entry1);
一个完整的卡夫卡例子是

如果您输入不包含必填字段,它将抛出一个错误,并且类型的值不会被检查(我可以输入
“counter”,“2”
,它将发送一个字符串值(对我来说这似乎是一个bug)。基本上,
genericord==HashMap
,并增加了required/nullable字段的好处

您需要配置一个Avro序列化程序,比如Confluent,它需要运行模式注册表,或者类似的版本

否则,您需要将Avro对象转换为
字节[]
(如链接中所示,只需使用
ByteArraySerializer

Schema schema = SchemaBuilder.record("TestFile")
            .namespace("com.example.kafka.data.ingestion.model")
            .fields()
            .requiredLong("date")
            .requiredInt("counter")
            .requiredString("mc")
            .endRecord();

GenericRecord entry1 = new GenericData.Record(schema);
entry1.put("date", 1L);
entry1.put("counter", 2);
entry1.put("mc", "3");

// producer.send(new ProducerRecord<>(topic, entry1);