1、定义通用处理
public interface ClickHouseBaseService extends IService<DataRow> {
/**
* 根据sql查询数据库,返回集合对象
*
* @param sql 执行sql
* @return {@link DataSet} 数据集
*/
DataSet querys(String sql);
/**
* 根据sql查询数据库,返回单个对象
*
* @param sql 执行sql
* @return {@link DataRow}
*/
DataRow query(String sql);
}
1.2实现类
@Service
@AllArgsConstructor
@Slf4j
public class ClickHouseBaseServiceImpl extends ServiceImpl<ClickHouseBaseMapper, DataRow> implements ClickHouseBaseService {
public DataSet querys(String sql) {
log.info("clickhouse查询开始");
List<DataRow> datas = baseMapper.querys(sql);
log.info("clickhouse查询结束,size:{}", datas.size());
if (CollUtil.isNotEmpty(datas)) {
return DataSet.build(datas);
}
return new DataSet();
}
public DataRow query(String sql) {
log.info("clickhouse查询开始");
DataRow query = baseMapper.query(sql);
log.info("clickhouse查询结");
if (Objects.nonNull(query)) {
return query;
} else {
return new DataRow();
}
}
}
2、查询
//根据sql查询数据
DataSet dataRows = new DataSet();
String sql="select * from table;"
dataRows = clickHouseBaseService.querys(sql);
if (dataRows.size() > 0) {
for (DataRow dataRow : dataRows) {
}
}