1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| package com.bigdata.flink.tableSqlAppendUpsertRetract.append;
import com.alibaba.fastjson.JSON; import com.bigdata.flink.beans.table.UserBrowseLog; import lombok.extern.slf4j.Slf4j; import org.apache.flink.api.common.serialization.SimpleStringSchema; import org.apache.flink.api.common.typeinfo.TypeInformation; import org.apache.flink.api.java.utils.ParameterTool; import org.apache.flink.streaming.api.datastream.DataStream; import org.apache.flink.streaming.api.datastream.DataStreamSink; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.streaming.api.functions.ProcessFunction; import org.apache.flink.streaming.api.functions.sink.RichSinkFunction; import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer010; import org.apache.flink.table.api.DataTypes; import org.apache.flink.table.api.EnvironmentSettings; import org.apache.flink.table.api.TableSchema; import org.apache.flink.table.api.java.StreamTableEnvironment; import org.apache.flink.table.sinks.AppendStreamTableSink; import org.apache.flink.table.sinks.TableSink; import org.apache.flink.table.types.DataType; import org.apache.flink.types.Row; import org.apache.flink.util.Collector; import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.Properties;
@Slf4j public class Test { public static void main(String[] args) throws Exception{
args=new String[]{"--application","flink/src/main/java/com/bigdata/flink/tableSqlAppendUpsertRetract/application.properties"};
ParameterTool fromArgs = ParameterTool.fromArgs(args); ParameterTool parameterTool = ParameterTool.fromPropertiesFile(fromArgs.getRequired("application"));
String kafkaBootstrapServers = parameterTool.getRequired("kafkaBootstrapServers"); String browseTopic = parameterTool.getRequired("browseTopic"); String browseTopicGroupID = parameterTool.getRequired("browseTopicGroupID");
EnvironmentSettings settings = EnvironmentSettings.newInstance().inStreamingMode().useBlinkPlanner().build(); StreamExecutionEnvironment streamEnv = StreamExecutionEnvironment.getExecutionEnvironment(); StreamTableEnvironment tableEnv = StreamTableEnvironment.create(streamEnv, settings); streamEnv.setParallelism(1);
Properties browseProperties = new Properties(); browseProperties.put("bootstrap.servers",kafkaBootstrapServers); browseProperties.put("group.id",browseTopicGroupID); DataStream<UserBrowseLog> browseStream=streamEnv .addSource(new FlinkKafkaConsumer010<>(browseTopic, new SimpleStringSchema(), browseProperties)) .process(new BrowseKafkaProcessFunction()); tableEnv.registerDataStream("source_kafka_browse_log",browseStream,"userID,eventTime,eventType,productID,productPrice,eventTimeTimestamp");
String[] sinkFieldNames={"userID","eventTime","eventType","productID","productPrice","eventTimeTimestamp"}; DataType[] sinkFieldTypes={DataTypes.STRING(),DataTypes.STRING(),DataTypes.STRING(),DataTypes.STRING(),DataTypes.INT(),DataTypes.BIGINT()}; TableSink<Row> myAppendStreamTableSink = new MyAppendStreamTableSink(sinkFieldNames,sinkFieldTypes); tableEnv.registerTableSink("sink_stdout",myAppendStreamTableSink);
String sql="insert into sink_stdout select userID,eventTime,eventType,productID,productPrice,eventTimeTimestamp from source_kafka_browse_log where userID='user_1'"; tableEnv.sqlUpdate(sql);
tableEnv.execute(Test.class.getSimpleName());
}
private static class BrowseKafkaProcessFunction extends ProcessFunction<String, UserBrowseLog> { @Override public void processElement(String value, Context ctx, Collector<UserBrowseLog> out) throws Exception { try {
UserBrowseLog log = JSON.parseObject(value, UserBrowseLog.class);
java.time.format.DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); OffsetDateTime eventTime = LocalDateTime.parse(log.getEventTime(), format).atOffset(ZoneOffset.of("+08:00")); long eventTimeTimestamp = eventTime.toInstant().toEpochMilli(); log.setEventTimeTimestamp(eventTimeTimestamp);
out.collect(log); }catch (Exception ex){ log.error("解析Kafka数据异常...",ex); } } }
private static class MyAppendStreamTableSink implements AppendStreamTableSink<Row> {
private TableSchema tableSchema;
public MyAppendStreamTableSink(String[] fieldNames, DataType[] fieldTypes) { this.tableSchema = TableSchema.builder().fields(fieldNames,fieldTypes).build(); }
@Override public TableSchema getTableSchema() { return tableSchema; }
@Override public DataType getConsumedDataType() { return tableSchema.toRowDataType(); }
@Override public TableSink<Row> configure(String[] fieldNames, TypeInformation<?>[] fieldTypes) { return null; }
@Override public void emitDataStream(DataStream<Row> dataStream) {}
@Override public DataStreamSink<Row> consumeDataStream(DataStream<Row> dataStream) { return dataStream.addSink(new SinkFunction()); }
private static class SinkFunction extends RichSinkFunction<Row> { public SinkFunction() { }
@Override public void invoke(Row value, Context context) throws Exception { System.out.println(value); } } }
}
|