优化代码:使用windowAll做数据批量操作
This commit is contained in:
@@ -6,15 +6,17 @@ import cn.hutool.log.Log;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import org.apache.flink.configuration.Configuration;
|
||||
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
|
||||
import ru.yandex.clickhouse.BalancedClickhouseDataSource;
|
||||
import ru.yandex.clickhouse.settings.ClickHouseProperties;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.zdjizhi.common.FlowWriteConfig.*;
|
||||
@@ -23,21 +25,10 @@ public class ClickhouseSink extends RichSinkFunction<List<Map<String, Object>>>
|
||||
|
||||
private static final Log log = LogFactory.get();
|
||||
|
||||
private static Connection connection;
|
||||
private static PreparedStatement preparedStatement;
|
||||
private Connection connection;
|
||||
private PreparedStatement preparedStatement;
|
||||
public String sink;
|
||||
|
||||
static {
|
||||
try {
|
||||
Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
|
||||
connection = DriverManager.getConnection("jdbc:clickhouse://" + CK_HOSTS + "/" + CK_DATABASE, CK_USERNAME, CK_PIN);
|
||||
// BalancedClickhouseDataSource dataSource = new BalancedClickhouseDataSource("jdbc:clickhouse://node01:8123,node02:8123,node03:8123/default", props);
|
||||
// connection = dataSource.getConnection();
|
||||
log.info("get clickhouse connection success");
|
||||
} catch (ClassNotFoundException | SQLException e) {
|
||||
log.error("clickhouse connection error ,{}", e);
|
||||
}
|
||||
}
|
||||
|
||||
public ClickhouseSink(String sink) {
|
||||
this.sink = sink;
|
||||
@@ -59,16 +50,32 @@ public class ClickhouseSink extends RichSinkFunction<List<Map<String, Object>>>
|
||||
@Override
|
||||
public void open(Configuration parameters) throws Exception {
|
||||
|
||||
try {
|
||||
// Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
|
||||
// connection = DriverManager.getConnection("jdbc:clickhouse://" + CK_HOSTS + "/" + CK_DATABASE, CK_USERNAME, CK_PIN);
|
||||
ClickHouseProperties properties = new ClickHouseProperties();
|
||||
properties.setDatabase(CK_DATABASE);
|
||||
properties.setUser(CK_USERNAME);
|
||||
properties.setPassword(CK_PIN);
|
||||
// properties.setKeepAliveTimeout(5);
|
||||
properties.setConnectionTimeout(CK_CONNECTION_TIMEOUT);
|
||||
properties.setSocketTimeout(CK_SOCKET_TIMEOUT);
|
||||
|
||||
BalancedClickhouseDataSource dataSource = new BalancedClickhouseDataSource("jdbc:clickhouse://" + CK_HOSTS, properties);
|
||||
dataSource.scheduleActualization(10, TimeUnit.SECONDS);//开启检测
|
||||
connection = dataSource.getConnection();
|
||||
|
||||
log.info("get clickhouse connection success");
|
||||
} catch (SQLException e) {
|
||||
log.error("clickhouse connection error ,{}", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
if (null != connection) {
|
||||
connection.close();
|
||||
}
|
||||
if (null != preparedStatement) {
|
||||
preparedStatement.close();
|
||||
}
|
||||
IoUtil.close(preparedStatement);
|
||||
IoUtil.close(connection);
|
||||
}
|
||||
|
||||
public void executeInsert(List<Map<String, Object>> data, String tableName) {
|
||||
@@ -109,9 +116,6 @@ public class ClickhouseSink extends RichSinkFunction<List<Map<String, Object>>>
|
||||
|
||||
} catch (Exception ex) {
|
||||
log.error("ClickhouseSink插入报错", ex);
|
||||
} finally {
|
||||
IoUtil.close(preparedStatement);
|
||||
IoUtil.close(connection);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user