mysql_kafka
package com.dl.kafka;
import com.adt.entity.ShareHolder;
import com.adt.vo.TimeStamp;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.errors.ProducerFencedException;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.scheduling.annotation.Scheduled;
import java.sql.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.UUID;
public class KafkaProducerMysql {
private static String driver;
private static String url;
private static String username;
private static String password;
private static String string;
private static String sql;
private static PreparedStatement ps;
private static ResultSet rs;
private static Connection connection;
private static String time;
private static TimeStamp timeStamp;
private static Properties properties;
private static KafkaProducer<String, String> producer;
private static ProducerRecord<String, String> record;
private static long count;
private static int n;
private static ShareHolder shareHolder;
static {
properties = new Properties();
driver = "com.mysql.jdbc.Driver";
url = "jdbc:mysql://192.168.131.168:3306/表名";
username = "root";
password = "123456";
try {
connection = DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
e.printStackTrace();
}
properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.248.136:9092");
properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
properties.put(ProducerConfig.ACKS_CONFIG,"all");
properties.put(ProducerConfig.RETRIES_CONFIG,3);
properties.put(ProducerConfig.REQUEST_TIMEOUT_MS_CONFIG,30000);
properties.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG,true);
properties.put(ProducerConfig.TRANSACTION_TIMEOUT_CONFIG,1000*60*3);
properties.put(ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION, 1);
producer = new KafkaProducer<String, String>(properties);
n = 100000;
}
@Scheduled(cron = "0/5 * * * * ?")
public static void main(String[] args) throws SQLException, ParseException {
mysqlToKafka();
}
public static void mysqlToKafka() throws SQLException, ParseException {
String content = new Date().toString();
String updateTimeSql = "update ICSL_TIME_STAMP set content ='"+content+"' where id =1;";
connection = DriverManager.getConnection(url,username,password);
ps = connection.prepareStatement(updateTimeSql);
ps.executeUpdate();
connection = DriverManager.getConnection(url,username,password);
String timeSql = "select TIME_STAMP from ICSL_TIME_STAMP where id = 1;";
ps = connection.prepareStatement(timeSql);
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()){
timeStamp = new TimeStamp(resultSet.getString("TIME_STAMP"));
}
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = sdf.parse(timeStamp.getTimeStamp());
time = sdf.format(new Date(parse.getTime() - 1200000));
sql = "select count(id) count from COMPANY_SHAREHOLDER where TIME_STAMP >= '"+time+"';";
ps = connection.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()){
count = rs.getInt("count");
}
count = count%n == 0 ? (count/n) : (count/n)+1;
try{
for (int i = 0; i <= count; i++) {
sql = "select * from COMPANY_SHAREHOLDER where TIME_STAMP >= '"+time+"' limit "+(i*n)+","+n+";";
ps = connection.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()){
shareHolder = new ShareHolder(
rs.getString("ID"),
rs.getString("COMPANY_ID"),
rs.getString("COMPANY_NAME"),
rs.getString("INVESTOR_TYPE"),
rs.getString("INVESTOR_TYPE_NAME"),
rs.getString("SHAREHOLDER_NUM"),
rs.getString("SHAREHOLDER_ID"),
rs.getString("SHAREHOLDER_NAME"),
rs.getString("CAPITAL"),
rs.getString("CAPITALACTL"),
rs.getString("AMOUNT"),
rs.getString("CERTNAME"),
rs.getString("CERTNO"),
rs.getString("STAKES_RATIO"),
rs.getString("NOTES"),
rs.getString("DESC01"),
rs.getString("DESC02"),
rs