This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
zhanghongqing-knowledge-log/src/main/java/com/zdjizhi/utils/ck/ClickhouseSink.java

139 lines
4.8 KiB
Java
Raw Normal View History

2022-07-07 14:07:27 +08:00
package com.zdjizhi.utils.ck;
import cn.hutool.core.io.IoUtil;
2022-07-07 14:07:27 +08:00
import cn.hutool.core.util.StrUtil;
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;
2022-07-07 14:07:27 +08:00
import java.sql.Connection;
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;
2022-07-07 14:07:27 +08:00
import java.util.stream.Collectors;
import static com.zdjizhi.common.FlowWriteConfig.*;
public class ClickhouseSink extends RichSinkFunction<List<Map<String, Object>>> {
2022-07-07 14:07:27 +08:00
private static final Log log = LogFactory.get();
private Connection connection;
private PreparedStatement preparedStatement;
2022-07-07 14:07:27 +08:00
public String sink;
public ClickhouseSink(String sink) {
this.sink = sink;
}
public String getSink() {
return sink;
}
public void setSink(String sink) {
this.sink = sink;
}
@Override
public void invoke(List<Map<String, Object>> logs, Context context) throws Exception {
executeInsert(logs, getSink());
2022-07-07 14:07:27 +08:00
}
@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);
}
2022-07-07 14:07:27 +08:00
}
@Override
public void close() throws Exception {
IoUtil.close(preparedStatement);
IoUtil.close(connection);
2022-07-07 14:07:27 +08:00
}
public void executeInsert(List<Map<String, Object>> data, String tableName) {
2022-07-07 14:07:27 +08:00
try {
List<String> keys = new LinkedList<>(data.get(0).keySet());
2022-07-07 14:07:27 +08:00
connection.setAutoCommit(false);
preparedStatement = connection.prepareStatement(preparedSql(keys, tableName));
int count = 0;
for (Map<String, Object> map : data) {
List<Object> values = new LinkedList<>(map.values());
for (int i = 1; i <= values.size(); i++) {
Object val = values.get(i - 1);
if (val instanceof Long) {
preparedStatement.setLong((i), Long.valueOf(StrUtil.toString(val)));
} else if (val instanceof Integer) {
preparedStatement.setInt((i), Integer.valueOf(StrUtil.toString(val)));
} else if (val instanceof Boolean) {
preparedStatement.setBoolean((i), Boolean.valueOf(StrUtil.toString(val)));
} else {
preparedStatement.setString((i), StrUtil.toString(val));
}
2022-07-07 14:07:27 +08:00
}
preparedStatement.addBatch();
count++;
2022-07-07 14:07:27 +08:00
//1w提交一次
if (count % SINK_BATCH == 0) {
2022-07-07 14:07:27 +08:00
preparedStatement.executeBatch();
connection.commit();
preparedStatement.clearBatch();
count = 0;
2022-07-07 14:07:27 +08:00
}
}
if (count > 0) {
2022-07-07 14:07:27 +08:00
preparedStatement.executeBatch();
connection.commit();
}
2022-07-07 14:07:27 +08:00
} catch (Exception ex) {
log.error("ClickhouseSink插入报错", ex);
}
}
public static String preparedSql(List<String> fields, String tableName) {
2022-07-07 14:07:27 +08:00
String placeholders = fields.stream()
.filter(Objects::nonNull)
.map(f -> "?")
.collect(Collectors.joining(", "));
String columns = fields.stream()
.filter(Objects::nonNull)
.collect(Collectors.joining(", "));
String sql = StrUtil.concat(true, "INSERT INTO ", CK_DATABASE, ".", tableName,
"(", columns, ") VALUES (", placeholders, ")");
log.debug(sql);
2022-07-07 14:07:27 +08:00
return sql;
}
}