工具类
public class KafkaUtils {
public static DataStreamSource<String> getNewKafkaSource(StreamExecutionEnvironment env, String topic, String groupId, OffsetsInitializer offsets) {
KafkaSource<String> source = KafkaSource.<String>builder()
.setProperty("partition.discovery.interval.ms", "60000")
.setBootstrapServers(KafkaConfig.bootstrapServers)
.setTopics(topic)
.setGroupId(groupId)
.setValueOnlyDeserializer(new SimpleStringSchema())
.setStartingOffsets(offsets)
.build();
return env.fromSource(source, WatermarkStrategy.noWatermarks(), topic);
}
public static KafkaSink<String> kafkaSink(String topic, String filed) {
Properties properties = new Properties();
properties.setProperty("compression.type", "lz4");
properties.setProperty("compression.codec", "lz4");
return KafkaSink.<String>builder()
.setBootstrapServers(KafkaConfig.bootstrapServers)
.setRecordSerializer(KafkaRecordSerializationSchema.builder()
.setTopic(topic)
.setValueSerializationSchema(new SimpleStringSchema())
.setPartitioner(new FlinkKafkaPartitioner<String>() {
@Override
public int partition(String record, byte[] key, byte[] value, String targetTopic, int[] partitions) {
JSONObject jsonObject = JSONObject.parseObject(record);
Object o = jsonObject.get(filed);
return Math.abs(o.hashCode() % partitions.length);
}
} )
.build()
)
.setDeliverGuarantee(DeliveryGuarantee.AT_LEAST_ONCE)
.setKafkaProducerConfig(properties)
.build();
}
使用
KafkaUtils
.getNewKafkaSource(env, "topic", "groupId", OffsetsInitializer.latest())
.map(new MapFunction<String, JSONObject>() {
@Override
public JSONObject map(String value) throws Exception {
return JSON.parseObject(value,JSONObject.class);
}
})
.print("测试");