flink Data Source数据源

flink

Data Source数据源

Source

  • 并行度

    • 非并行:并行度只能为1

    • 并行

  • 基于集合的Source

    • fromElements

      • package com.pxj.sx.flink;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.RestOptions;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class FromElementDemo {
   
   
    public static void main(String[] args) throws Exception {
   
   
        Configuration configuration = new Configuration();
        configuration.setInteger(RestOptions.PORT, 8081);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(configuration);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> daat = env.fromElements("flink", "spark", "hive");
        daat.print();


        Thread.sleep(2000000);
    }
}
	- fromElements(T ...) 方法是一个非并行的Source,可以将一到多个数据作为可变参数传入到该方法中,返回DataStreamSource。该方法返回的DataStream是一个有限数据流,数据读完后,程序退出,通常用于开发测试。

-  fromCollection

	- fromCollection可以从一个结合读取数据,返回DataStream,该方法返回的DataStream是一个有限数据流,数据读完后,程序退出,通常用于开发测试。

		- package com.pxj.sx.flink;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

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

public class FromCollectionDemo {
   
   
    public static void main(String[] args) throws Exception {
   
   
         StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
         List<String> wordList = Arrays.asList("flink", "spark", "hadoop", "flink");
         DataStreamSource<String> source = env.fromCollection(wordList);
         source.print(
Flink是一个流式处理引擎,它支持从各种数据源(例如Kafka、Kinesis、HDFS等)读取数据,并将数据处理并输出到各种目标系统(例如HDFS、Elasticsearch等)。Flink提供了一组内置的Source,如KafkaSource和SocketTextStreamSource,可以轻松地与常见的数据源进行交互。除此之外,Flink还提供了一种自定义Source的方式,让用户可以轻松地从自己的数据源中读取数据。 自定义Source的步骤如下: 1.实现SourceFunction接口 在Flink中,自定义Source需要实现SourceFunction接口。SourceFunction是所有自定义Source的基类,它定义了两个方法:run和cancel。run方法中包含了执行自定义Source的逻辑,cancel方法用于取消任务。 ```java public interface SourceFunction<T> extends Function, Serializable { void run(SourceContext<T> ctx) throws Exception; void cancel(); } ``` 2.实现run方法 在run方法中,应该包含从自定义数据源中读取数据的逻辑。Flink提供了SourceContext接口,可以使用它将数据发送到下游算子中。 ```java public interface SourceContext<T> { void collect(T element); void collectWithTimestamp(T element, long timestamp); void emitWatermark(Watermark mark); Object getCheckpointLock(); void close(); } ``` 例如,以下示例代码从自定义数据源中读取整数,并将它们发送到下游算子中: ```java public class CustomSource implements SourceFunction<Integer> { private volatile boolean isRunning = true; @Override public void run(SourceContext<Integer> ctx) throws Exception { while (isRunning) { int number = // 从自定义数据源读取数据 ctx.collect(number); } } @Override public void cancel() { isRunning = false; } } ``` 3.添加自定义SourceFlink程序中 一旦自定义Source已经实现,就可以将它添加到Flink程序中。以下示例代码展示了如何将自定义Source添加到Flink程序中: ```java StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); DataStream<Integer> dataStream = env.addSource(new CustomSource()); ``` 在这个例子中,我们使用DataStream API将自定义Source添加到Flink程序中,并将其转换为DataStream对象。 4.配置自定义Source 用户可以通过调用DataStream API中的各种方法来配置自定义Source。例如,可以使用setParallelism方法设置并行度,使用setUid方法设置唯一标识符等。 例如,以下示例代码展示了如何设置自定义Source的并行度: ```java StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); DataStream<Integer> dataStream = env.addSource(new CustomSource()).setParallelism(2); ``` 在这个例子中,我们将自定义Source的并行度设置为2。 5.启动Flink程序 配置完成后,就可以启动Flink程序了。Flink程序将从自定义Source中读取数据,并将其发送到下游算子中进行处理。 参考文献: - https://ci.apache.org/projects/flink/flink-docs-release-1.13/dev/datastream_api.html#sources - https://ci.apache.org/projects/flink/flink-docs-release-1.13/dev/datastream_api.html#transformations-on-datastreams
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值