1. Flink版本1.7.2
2. 引入依赖
使用maven构建工程,因此pom.xml添加如下依赖:
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table_2.11</artifactId>
<version>1.7.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.flink/flink-json -->
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-json</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>
3. Google Protobuf消息定义
3.1 消息定义
response.proto文件
syntax = "proto3";
package com.google.protos;
//搜索响应
message SearchResponse {
uint64 search_time = 1;
uint32 code = 2;
Result results = 3;
}
//搜索结果
message Result {
string id = 1;
repeated Item items = 2;
}
//搜索结果项
message Item{
string id = 1;
string name = 2;
string title = 3;
string url = 4;
uint64 publish_time = 5;
float score = 6; //推荐或者相似加权分值
}
消息示例,包含嵌套对象results以及数组对象items:
{
"search_time":1553650604,
"code":200,
"results":{
"id":"449",
"items":[
{
"id":"47",
"name":"name47",
"title":"标题47",
"url":"https://www.google.com.hk/item-47",
"publish_time":1552884870,
"score":96.03
},
{
"id":"2",
"name":"name2",
"title":"标题2",
"url":"https://www.google.com.hk/item-2",
"publish_time":1552978902,
"score":16.06
},
{
"id":"60",
"name":"name60",
"title":"标题60",
"url":"https://www.google.com.hk/item-60",
"publish_time":1553444982,
"score":62.58
},
{
"id":"67",
"name":"name67",
"title":"标题67",
"url":"https://www.google.com.hk/item-67",
"publish_time":1553522957,
"score":12.17
},
{
"id":"15",
"name":"name15",
"title":"标题15",
"url":"https://www.google.com.hk/item-15",
"publish_time":1553525421,
"score":32.36
},
{
"id":"53",
"name":"name53",
"title":"标题53",
"url":"https://www.google.com.hk/item-53",
"publish_time":1553109227,
"score":52.13
},
{
"id":"70",
"name":"name70",
"title":"标题70",
"url":"https://www.google.com.hk/item-70",
"publish_time":1552781921,
"score":1.72
},
{
"id":"53",
"name":"name53",
"title":"标题53",
"url":"https://www.google.com.hk/item-53",
"publish_time":1553229003,
"score":5.31
},
{
"id":"30",
"name":"name30",
"title":"标题30",
"url":"https://www.google.com.hk/item-30",
"publish_time":1553282629,
"score":26.51
},
{
"id":"36",
"name":"name36",
"title":"标题36",
"url":"https://www.google.com.hk/item-36",
"publish_time":1552665833,
"score":48.76
}
]
}
}
3.2 Kakfa Producer发布随机响应Json串
import com.google.protos.GoogleProtobuf.*;
import com.googlecode.protobuf.format.JsonFormat;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import or