2022-07-07 14:07:27 +08:00
|
|
|
package com.zdjizhi.utils.ck;
|
|
|
|
|
|
2022-07-11 10:05:14 +08:00
|
|
|
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;
|
2022-07-12 19:24:53 +08:00
|
|
|
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;
|
2022-07-12 19:24:53 +08:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2022-07-07 14:07:27 +08:00
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
import static com.zdjizhi.common.FlowWriteConfig.*;
|
|
|
|
|
|
2022-07-11 10:05:14 +08:00
|
|
|
public class ClickhouseSink extends RichSinkFunction<List<Map<String, Object>>> {
|
2022-07-07 14:07:27 +08:00
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.get();
|
|
|
|
|
|
2022-07-12 19:24:53 +08:00
|
|
|
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
|
2022-07-11 10:05:14 +08:00
|
|
|
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 {
|
2022-07-11 10:05:14 +08:00
|
|
|
|
2022-07-12 19:24:53 +08:00
|
|
|
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 {
|
2022-07-12 19:24:53 +08:00
|
|
|
IoUtil.close(preparedStatement);
|
|
|
|
|
IoUtil.close(connection);
|
2022-07-07 14:07:27 +08:00
|
|
|
}
|
|
|
|
|
|
2022-07-11 10:05:14 +08:00
|
|
|
public void executeInsert(List<Map<String, Object>> data, String tableName) {
|
2022-07-07 14:07:27 +08:00
|
|
|
|
|
|
|
|
try {
|
2022-07-11 10:05:14 +08:00
|
|
|
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));
|
2022-07-11 10:05:14 +08:00
|
|
|
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
|
|
|
}
|
2022-07-11 10:05:14 +08:00
|
|
|
preparedStatement.addBatch();
|
|
|
|
|
count++;
|
2022-07-07 14:07:27 +08:00
|
|
|
//1w提交一次
|
2022-07-11 10:05:14 +08:00
|
|
|
if (count % SINK_BATCH == 0) {
|
2022-07-07 14:07:27 +08:00
|
|
|
preparedStatement.executeBatch();
|
|
|
|
|
connection.commit();
|
|
|
|
|
preparedStatement.clearBatch();
|
2022-07-11 10:05:14 +08:00
|
|
|
count = 0;
|
2022-07-07 14:07:27 +08:00
|
|
|
}
|
2022-07-11 10:05:14 +08:00
|
|
|
}
|
|
|
|
|
if (count > 0) {
|
2022-07-07 14:07:27 +08:00
|
|
|
preparedStatement.executeBatch();
|
|
|
|
|
connection.commit();
|
|
|
|
|
}
|
2022-07-11 10:05:14 +08:00
|
|
|
|
2022-07-07 14:07:27 +08:00
|
|
|
} catch (Exception ex) {
|
|
|
|
|
log.error("ClickhouseSink插入报错", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-07-11 10:05:14 +08:00
|
|
|
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, ")");
|
2022-07-11 10:05:14 +08:00
|
|
|
log.debug(sql);
|
2022-07-07 14:07:27 +08:00
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|