flink sink
This commit is contained in:
		| @@ -1,7 +1,7 @@ | ||||
| package com.heibaiying; | ||||
|  | ||||
| import com.heibaiying.bean.Employee; | ||||
| import com.heibaiying.sink.FlinkToMySQL; | ||||
| import com.heibaiying.sink.FlinkToMySQLSink; | ||||
| import org.apache.flink.streaming.api.datastream.DataStreamSource; | ||||
| import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; | ||||
|  | ||||
| @@ -17,7 +17,7 @@ public class CustomSinkJob { | ||||
|                 new Employee("hei", 10, date), | ||||
|                 new Employee("bai", 20, date), | ||||
|                 new Employee("ying", 30, date)); | ||||
|         streamSource.addSink(new FlinkToMySQL()); | ||||
|         streamSource.addSink(new FlinkToMySQLSink()); | ||||
|         env.execute(); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -20,7 +20,7 @@ public class KafkaStreamingJob { | ||||
|  | ||||
|         // 1.指定Kafka的相关配置属性 | ||||
|         Properties properties = new Properties(); | ||||
|         properties.setProperty("bootstrap.servers", "192.168.200.229:9092"); | ||||
|         properties.setProperty("bootstrap.servers", "192.168.200.0:9092"); | ||||
|  | ||||
|         // 2.接收Kafka上的数据 | ||||
|         DataStream<String> stream = env | ||||
| @@ -35,7 +35,9 @@ public class KafkaStreamingJob { | ||||
|         }; | ||||
|         // 4. 定义Flink Kafka生产者 | ||||
|         FlinkKafkaProducer<String> kafkaProducer = new FlinkKafkaProducer<>("flink-stream-out-topic", | ||||
|                 kafkaSerializationSchema, properties, FlinkKafkaProducer.Semantic.AT_LEAST_ONCE, 5); | ||||
|                 kafkaSerializationSchema, | ||||
|                 properties, | ||||
|                 FlinkKafkaProducer.Semantic.AT_LEAST_ONCE, 5); | ||||
|         // 5. 将接收到输入元素*2后写出到Kafka | ||||
|         stream.map((MapFunction<String, String>) value -> value + value).addSink(kafkaProducer); | ||||
|         env.execute("Flink Streaming"); | ||||
|   | ||||
| @@ -8,7 +8,7 @@ import java.sql.Connection; | ||||
| import java.sql.DriverManager; | ||||
| import java.sql.PreparedStatement; | ||||
| 
 | ||||
| public class FlinkToMySQL extends RichSinkFunction<Employee> { | ||||
| public class FlinkToMySQLSink extends RichSinkFunction<Employee> { | ||||
| 
 | ||||
|     private PreparedStatement stmt; | ||||
|     private Connection conn; | ||||
| @@ -16,7 +16,7 @@ public class FlinkToMySQL extends RichSinkFunction<Employee> { | ||||
|     @Override | ||||
|     public void open(Configuration parameters) throws Exception { | ||||
|         Class.forName("com.mysql.cj.jdbc.Driver"); | ||||
|         conn = DriverManager.getConnection("jdbc:mysql://192.168.200.229:3306/employees?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false", "root", "123456"); | ||||
|         conn = DriverManager.getConnection("jdbc:mysql://192.168.0.229:3306/employees?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false", "root", "123456"); | ||||
|         String sql = "insert into emp(name, age, birthday) values(?, ?, ?)"; | ||||
|         stmt = conn.prepareStatement(sql); | ||||
|     } | ||||
		Reference in New Issue
	
	Block a user