Flink基础Source配置
一、pom文件
https://www.cnblogs.com/robots2/p/16048648.html
二、代码demo
FlinkBaseSource.java
package net.xdclass.class4;
import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;
import java.util.Arrays;
/**
* @author wangshida@baijia.com
* @datetime 2022-03-20 上午11:20
* @CopyRight (C) 百家互联
* @desc source数据源使用
* @menu
*/
public class Flink03Source1App {
/**
* source
* transformation
* sink
* @param args
*/
public static void main(String [] args) throws Exception {
//构建执行任务环境以及任务的启动的入口, 存储全局相关的参数
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setRuntimeMode(RuntimeExecutionMode.AUTOMATIC);
//并行度
//env.setParallelism(1);
//方式1元素:相同类型元素的数据流 source
//DataStream<String> ds1 = env.fromElements("java,SpringBoot", "spring cloud,redis", "kafka,FLink");
//ds1.print("ds1:");
//方式2列表:相同类型元素的数据流 source
//DataStream<String> ds2 = env.fromCollection(Arrays.asList("java,SpringBoot", "spring cloud,redis", "kafka,小滴课堂"));
//ds2.print("ds2:");
//方式3序号:读取
// DataStream<Long> ds3 = env.fromSequence(1,10);
// ds3.print("ds3:");
//方式4:本地文件
// DataStream<String> ds4 = env.readTextFile("/Users/xdclass/Desktop/xdclass_access.log");
// ds4.print("ds4:");
//方式5:hdfs
// DataStream<String> ds5 = env.readTextFile("hdfs://xdclass_node:8010/file/log/words.txt");
// ds5.print("ds5:");
//方式6:socket,本地运行 nc -lk 8888发送内容
DataStream<String> ds6 = env.socketTextStream("127.0.0.1", 8888);
ds6.print();
//DataStream需要调用execute,可以取个名称
env.execute("source job");
}
}