import com.paratera.console.datasync.constans.DataSyncConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.sql.;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.;
/**
- clickhouse 工具类
*/
@Slf4j
@Component
public class ClickHouseUtils {
@Value("${clickhouse.url}")
private String url;
@Resource(name = "clickhouseConnection")
private Connection connection;
/**
* 执行无返回sql
*
* @param sql
*/
public void execSQL(String sql) {
log.debug("clickhouse 执行sql:" + sql);
try (Statement statement = connection.createStatement()) {
statement.executeQuery(sql);
} catch (SQLException e) {
sendMessage2WxRobot(e);
}
}
/**
* 执行有返回结果sql
*
* @param sql
* @return
*/
public List<Map> exeSql(String sql) {
log.info("clickhouse 执行sql:" + sql);
try (Statement statement = connection.createStatement();
ResultSet results = statement.executeQuery(sql)) {
ResultSetMetaData rsmd = results.getMetaData();
List<Map> list = new ArrayList();
while (results.next()) {
Map row = new HashMap();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
row.put(rsmd.getColumnName(i), results.getString(rsmd.getColumnName(i)));
}
list.add(row);
}
return list;
} catch (SQLException e) {
sendMessage2WxRobot(e);
}
return Collections.emptyList();
}
}