最顶层:SQL/Table API 提供了操作关系表、执行SQL语句分析的API库,供我们方便的开发SQL相关程序
中层:流和批处理API层,提供了一系列流和批处理的API和算子供我们对数据进行处理和分析
最底层:运行时层,提供了对Flink底层关键技术的操纵,如对Event、state、time、window等进行精细化控制的操作API
DataStreamAPI 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 import org.apache.flink.api.common.functions.FlatMapFunction;import org.apache.flink.api.common.functions.MapFunction;import org.apache.flink.api.java.ExecutionEnvironment;import org.apache.flink.api.java.operators.*;import org.apache.flink.api.java.tuple.Tuple2;import org.apache.flink.util.Collector;public class WordCountBatchDataStream { public static void main (String[] args) throws Exception { ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); DataSource<String> text = env.readTextFile("D:\\Download\\wordcount.txt" ); FlatMapOperator<String, String> wordDS = text.flatMap(new FlatMapFunction <String, String>() { @Override public void flatMap (String value, Collector<String> out) throws Exception { String[] strings = value.split("," ); for (String str : strings) { out.collect(str); } } }); MapOperator<String, Tuple2<String,Integer>> mapDs = wordDS.map(new MapFunction <String, Tuple2<String,Integer>>() { @Override public Tuple2<String,Integer> map (String value) throws Exception { return Tuple2.of(value, 1 ); } }); UnsortedGrouping<Tuple2<String,Integer>> groupBy = mapDs.groupBy(0 ); AggregateOperator<Tuple2<String,Integer>> sumDs = groupBy.sum(1 ); sumDs.print(); } }
TableAPI 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 import org.apache.flink.table.api.*;import javax.swing.text.TableView;import static org.apache.flink.table.api.Expressions.$;public class WordCountBatchTable { public static void main (String[] args) { EnvironmentSettings settings = EnvironmentSettings.newInstance().inBatchMode().build(); TableEnvironment tEnv = TableEnvironment.create(settings); tEnv.createTemporaryTable("sourceTable" , TableDescriptor.forConnector("filesystem" ) .schema(Schema.newBuilder() .column("userId" , DataTypes.STRING()) .column("timestamp" ,DataTypes.BIGINT()) .column("money" , DataTypes.DOUBLE()) .column("category" , DataTypes.STRING()) .build()) .option("path" , "D:\\Download\\order(1).csv" ) .option("format" , "csv" ) .build()); Table result = tEnv.from("sourceTable" ) .groupBy($("userId" )) .select($("userId" ), $("money" ).sum().as("totalMoney" )); tEnv.createTemporaryTable("sinkTable" , TableDescriptor.forConnector("print" ) .schema(Schema.newBuilder() .column("userId" , DataTypes.STRING()) .column("totalMoney" , DataTypes.DOUBLE()) .build()) .build()); result.executeInsert("sinkTable" ); } }
报错解决 Exception in thread “main” java.lang.reflect.InaccessibleObjectException: Unable to make field private static final long java.util.LinkedHashMap.serialVersionUID accessible: module java.base does not “opens java.util” to unnamed module @424ebba3