flink sink HDFS

StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
Properties debeziumProperties = new Properties();
debeziumProperties.put("snapshot.locking.mode", "none");
// 使用flinkCDC从MySQL读取增量数据
DebeziumSourceFunction mySQLSource = MySqlSource.<String>builder()
        .serverTimeZone("UTC")
        .hostname("114.115.150.**")
        .port(3306)
        .databaseList("DBname")
        .startupOptions(StartupOptions.initial())
        .username("user")
        .password("ps")
        .deserializer(new StringDebeziumDeserializationSchema())
        .build();
DataStreamSource dataStreamSource = env.addSource(mySQLSource);
// 写入HDFS
StreamingFileSink<String> sinkBuild = StreamingFileSink
        .forRowFormat(new Path("hdfs://loclhost//user//flink//test1//"), new SimpleStringEncoder<String>("UTF-8"))
        .withRollingPolicy(
                DefaultRollingPolicy.builder()
                        .withRolloverInterval(TimeUnit.MINUTES.toMinutes(5))
                        .withInactivityInterval(TimeUnit.MINUTES.toMinutes(1))
                        .withMaxPartSize(1024 * 1024 * 1024)
                        .build()
        )
        .withBucketAssigner(new MyFlinkSink())
        .build();

dataStreamSource.addSink(sinkBuild).setParallelism(1);

dataStreamSource.print();
env.execute();
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好,您的问题是关于Flink中将数据写入HDFS的问题。对于这个问题,您可以使用FlinkHDFS Sink来实现将数据写入HDFS。具体步骤如下: 1. 在pom.xml中添加以下依赖: ``` <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-hadoop-compatibility_2.12</artifactId> <version>${flink.version}</version> </dependency> ``` 2. 在Flink代码中,使用以下代码创建HDFS Sink: ``` // 创建Hadoop Configuration对象 Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://localhost:9000"); // 创建HDFS Sink Path outputPath = new Path("hdfs://localhost:9000/path/to/output"); TextOutputFormat<String> outputFormat = new TextOutputFormat<>(outputPath); outputFormat.setWriteMode(WriteMode.OVERWRITE); outputFormat.setOutputDirectoryMode(FileOutputFormat.OutputDirectoryMode.PARONLY); HadoopFileSystem hdfs = new HadoopFileSystem(outputPath.toUri(), conf); HadoopOutputFormat<Text, String> hadoopOutputFormat = new HadoopOutputFormat<>(outputFormat, conf); hadoopOutputFormat.setExternalFileSystem(hdfs); DataStream<String> dataStream = ... // 输入数据流 dataStream.writeUsingOutputFormat(hadoopOutputFormat); ``` 其中,`outputPath`是输出路径,`TextOutputFormat`是输出格式,`WriteMode.OVERWRITE`表示覆盖已有文件,`FileOutputFormat.OutputDirectoryMode.PARONLY`表示只输出到HDFS的partition目录,`HadoopFileSystem`是Hadoop的FileSystem对象,`HadoopOutputFormat`是Hadoop的OutputFormat对象,`dataStream`是输入数据流。 这样,您就可以通过Flink将数据写入HDFS了。希望对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值