一、StreamingFileSink
1.1、SinkUtils类
package com.hpsk.flink.sink;
import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;
import java.util.concurrent.TimeUnit;
public class SinkUtils {
public StreamingFileSink<String> GetStreamingFileSink(String path){
return StreamingFileSink
.<String>forRowFormat(new Path(path),
new SimpleStringEncoder<>("UTF-8"))
.withRollingPolicy(
DefaultRollingPolicy
.builder()
.withMaxPartSize(1024 * 1024 * 1024)
.withRolloverInterval(TimeUnit.MINUTES.toMillis(15))
.withInactivityInterval(TimeUnit.MINUTES.toMillis(5))
.build()
)
.build();
}
}
1.2 、SinkStream类
package com.hpsk.flink.stream;
import com.hpsk.flink.beans.Event;
import com.hpsk.flink.sink.SinkUtils;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
public class SinkStream {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(2);
DataStreamSource<Event> inputDS = env.fromElements(
new Event("Mary", "./home", 2000L),
new Event("Bob", "./home", 3000L),
new Event("Mary", "./home", 2000L),
new Event("Alice", "./home", 1000L),
new Event("Alice", "./home", 2000L),
new Event("Mary", "./home", 3000L),
new Event("Bob", "./home", 5000L),
new Event("Bob", "./home", 6000L)
);
inputDS
.map(Event::toString)
.addSink(new SinkUtils().GetStreamingFileSink("output/output.txt"));
env.execute();
}
}
二、KafkaSink
2.1、SinkUtils类
package com.hpsk.flink.sink;
import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;
import java.util.concurrent.TimeUnit;
public class SinkUtils {
public FlinkKafkaProducer<String> getFlinkKafkaProducer(){
return new FlinkKafkaProducer<>(
"hadoop102:9092",
"event",
new SimpleStringSchema()