4.1、Flink任务怎样读取集合中的数据

1、API说明

非并行数据源:

def fromElementsT: TypeInformation(data: T*): DataStreamT

def fromCollectionT: TypeInformation(data: SeqT): DataStreamT

def fromCollectionT: TypeInformation (data: IteratorT): DataStreamT

并行数据源:

def fromParallelCollectionT: TypeInformation (data: SplittableIteratorT)

使用场景:

常用来调试代码使用


2、这是一个完整的入门案例

开发语言:Java1.8

Flink版本:flink1.17.0

java 复制代码
package com.baidu.datastream.source;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.NumberSequenceIterator;

import java.util.Arrays;
import java.util.List;

// --------------------------------------------------------------------------------------------
//  TODO 从集合中读取数据
// --------------------------------------------------------------------------------------------

/*
 *  TODO 通过`读取Java集合中数据`来创建 DataStreamSource
 *
 *  方法1:fromCollection
 *        Collection、Iterator -> DataStreamSource
 *  方法2:fromElements
 *        OUT... data -> DataStreamSource
 *  方法3:fromParallelCollection
 *        SplittableIterator -> DataStreamSource
 *  重要提示:
 *       fromCollection、fromElements 创建的是非并行source算子(并行度只能为1)
 *       fromParallelCollection 创建的是并行算子(并行度>=1)
 * */

public class ReadCollection {
    public static void main(String[] args) throws Exception {
        fromCollection();
        //fromElements();
        //fromParallelCollection();
    }

    public static void fromCollection() throws Exception {
        // 1.获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(3);

        // 2.读取Java集合数据
        List<String> list = Arrays.asList("刘备", "张飞", "关羽", "赵云", "马超", "黄忠");
        env.fromCollection(list).print();

        // 3.触发程序执行
        env.execute();
    }

    public static void fromElements() throws Exception {
        // 1.获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(3);

        // 2.读取给定的对象序列
        env.fromElements("刘备", "张飞", "关羽", "赵云", "马超", "黄忠").print();

        // 3.触发程序执行
        env.execute();
    }

    public static void fromParallelCollection() throws Exception {
        // 1.获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(3);

        // 2.读取给定的对象序列
        NumberSequenceIterator numberSequenceIterator = new NumberSequenceIterator(1, 10);

        env.fromParallelCollection(numberSequenceIterator, Long.class).print();
        /*
         * 注意: fromParallelCollection生成的source为并行算子
         *       集合中的数据会被平均分配到并行子任务中去
         * */

        // 3.触发程序执行
        env.execute();
    }
}
相关推荐
泛普软件1 小时前
工程合约管理软件具备哪些核心功能,解决合同结算各类纠纷难题
大数据·人工智能·区块链
Elastic 中国社区官方博客3 小时前
Elastic 在 Everest Group 企业搜索产品 PEAK Matrix® 评估 2026 中被评为领导者
大数据·运维·人工智能·elasticsearch·搜索引擎·ai·全文检索
重庆传粉科技5 小时前
传粉科技助力美国房产经纪企业Homequest开拓中国市场
大数据·人工智能
Java小白笔记5 小时前
Codex Skills 分类整理
大数据·人工智能·elasticsearch·搜索引擎·ai·ai编程·ai写作
杨逢昌工厂6S管理5 小时前
26-杨逢昌:制造企业6S改善提案落地低效解决方案——四级分级激励标准化体系,成效验证:车间现场熵增隐患下降65%。
大数据
一只专注api接口开发的技术猿6 小时前
电商评论自动化监控与情感数据分析完整落地教程(附可直接运行 Python 代码)
大数据·数据库·python·数据分析·自动化
AIGS0016 小时前
企业AI落地:Agent OS 治理框架实践
java·大数据·人工智能·机器学习·人工智能ai大模型应用
风途科技~7 小时前
多参数水质在线监测设备—水环境实时管控解决方案
大数据·人工智能
Elastic 中国社区官方博客7 小时前
谁来评判评判者?在 Elasticsearch Workflows 中使用 LLM-as-a-Judge
大数据·运维·人工智能·elasticsearch·搜索引擎·ai·全文检索
牛企老板俱乐部8 小时前
2026 年合肥专业 GEO 优化服务商深度测评|分行业精准选型指南
大数据·人工智能