目录
数据源类型:
1.从文件中读取数据
2.从集合中读取数据
3.从已有定义对象获取数据
4.从socket获取数据
5.从kafka获取数据
代码如下:
package com.atguigu.chapter05;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import java.util.ArrayList;
import java.util.Properties;
/**
* @author psl
* @create 2022/5/2 17:48
* @desc
*/
public class SourceTest {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(1);
//从文件中读取数据
DataStreamSource<String> stream1 = env.readTextFile("input/clicks.txt");
//从集合中读取数据
ArrayList<Integer> nums = new ArrayList<>();
nums.add(1);
nums.add(2);
DataStreamSource<Integer> stream2 = env.fromCollection(nums);
//从已有定义对象获取数据
ArrayList<Event> events = new ArrayList<>();
events.add(new Event("Mary","./home",1000L));
events.add(new Event("Bob","./cart",2000L));
events.add(new Event("Tom","./prod",3000L));
DataStreamSource<Event> stream3 = env.fromCollection(events);
//从socket获取数据
DataStreamSource<String> stream4 = env.socketTextStream("10.22.82.120", 7777);
stream1.print();
stream2.print();
stream3.print();
stream4.print();
//从kafka获取数据
Properties properties = new Properties();
properties.setProperty("bootstrap.servers","node04:9092");
properties.setProperty("group.id","consumer-group");
properties.setProperty("key.deserializer","oeg.apache.kafka.common.serialization.StringDeserializer");
properties.setProperty("value.deserializer","oeg.apache.kafka.common.serialization.StringDeserializer");
properties.setProperty("auto.offset.reset","latest");
DataStreamSource<String> kafkastream = env.addSource(new FlinkKafkaConsumer<String>("clicks", new SimpleStringSchema(), properties));
env.execute();
}
}
注意:3.从已有定义对象获取数据,需要定义一个类:
package com.atguigu.chapter05;
import java.sql.Timestamp;
/**
* @author psl
* @create 2022/5/2 17:47
* @desc
*/
public class Event {
public String user;
public String url;
public Long timestamp;
public Event() {
}
public Event(String user, String url, Long timestamp) {
this.user = user;
this.url = url;
this.timestamp = timestamp;
}
@Override
public String toString() {
return "Event{" +
"user='" + user + '\'' +
", url='" + url + '\'' +
", timestamp=" + new Timestamp(timestamp) +
'}';
}
}