fromCollection 可直接从集合中读取数据作为流,由于集合是有界的,所以该数据流有界,读完之后程序会自动退出。
是非并行的DataSource,主要用于测试。
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import java.util.Arrays;
public class FromCollectionReview {
public static void main(String[] args) throws Exception{
// 1.获取flink流计算的运行环境
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
// 2.从collection读取数据
// DataStreamSource<Integer> nums = env.fromElements(1,2,3,4,5,6,7,8,9);;
DataStreamSource<Integer> nums = env.fromCollection(Arrays.asList(1,2,3,4,5,6,7,8,9));
//3.调用Sink
nums.print();
//4.启动流计算
env.execute("FromCollectionReview");
}
运行结果如下: