更新时间:2024-10-31 GMT+08:00

查询ClickHouse数据

本章节介绍查询ClickHouse数据样例代码。

查询语句1:querySql1查询创建ClickHouse表创建的tableName表中任意10条数据;查询语句2:querySql2通过内置函数对创建ClickHouse表创建的tableName表中的日期字段取年月后进行聚合。

示例代片段参考如下:

private void queryData(String databaseName, String tableName) throws Exception {
    String querySql1 = "select * from " + databaseName + "." + tableName + "_all" + " order by age limit 10";
    String querySql2 = "select toYYYYMM(date),count(1) from " + databaseName + "." + tableName + "_all" + " group by toYYYYMM(date) order by count(1) DESC limit 10";
    ArrayList<String> sqlList = new ArrayList<String>();
    sqlList.add(querySql1);
    sqlList.add(querySql2);
    ArrayList<ArrayList<ArrayList<String>>> result = util.exeSql(sqlList);
    for (ArrayList<ArrayList<String>> singleResult : result) {
        for (ArrayList<String> strings : singleResult) {
            StringBuilder stringBuilder = new StringBuilder();
            for (String string : strings) { 
               stringBuilder.append(string).append("\t");
            }
            log.info(stringBuilder.toString());
        }
    }
}