springboot 使用clickhouse实时大数据分析引擎(使用方式)
程序员文章站
2022-06-10 08:02:46
声明:因项目中使用clickhouse引擎这里springboot使用的方式是jdbc方式连接,这种方式的好处是可以使用clickhouse 自带的fetch方法批量从clickhouse中获取数据,...
声明:
因项目中使用clickhouse引擎这里springboot使用的方式是jdbc方式连接,这种方式的好处是可以使用clickhouse 自带的fetch方法批量从clickhouse中获取数据,对于大量数据的下载来说,比较好
因为如果全部拿到内存中处理,大量数据会有内存溢出的结果
如果批量多次请求数据库对于数据库查询等也不靠谱,所有直接使用clickhouse jdbc连接来满足这种情况,不使用mybatis等框架来管理,这里根据大家不同的需求酌情参考即可
使用方式:
第一步:加入clickhouse jar包依赖
<!--clickhouse--> <dependency> <groupid>ru.yandex.clickhouse</groupid> <artifactid>clickhouse-jdbc</artifactid> <version>0.1.40</version> </dependency>
第二步:配置数据库连接属性配置文件,yml方式 此处仅作为参数,不连接任何驱动
clickhouse: address: jdbc:clickhouse://172.20.xxx.xxx:8123 username: default password: xxx db: marketing sockettimeout: 600000
第三步:添加数据库连接操作util工具类
import lombok.extern.slf4j.slf4j; import org.springframework.beans.factory.annotation.value; import org.springframework.stereotype.component; import net.sf.json.jsonobject; import ru.yandex.clickhouse.clickhouseconnection; import ru.yandex.clickhouse.clickhousedatasource; import ru.yandex.clickhouse.settings.clickhouseproperties; import java.sql.*; import java.util.*; /** * @description: * @date 2018/11/12 */ @slf4j @component public class clickhouseutil { private static string clickhouseaddress; private static string clickhouseusername; private static string clickhousepassword; private static string clickhousedb; private static integer clickhousesockettimeout; @value("${clickhouse.address}") public void setclickhouseaddress(string address) { clickhouseutil.clickhouseaddress = address; } @value("${clickhouse.username}") public void setclickhouseusername(string username) { clickhouseutil.clickhouseusername = username; @value("${clickhouse.password}") public void setclickhousepassword(string password) { clickhouseutil.clickhousepassword = password; @value("${clickhouse.db}") public void setclickhousedb(string db) { clickhouseutil.clickhousedb = db; @value("${clickhouse.sockettimeout}") public void setclickhousesockettimeout(integer sockettimeout) { clickhouseutil.clickhousesockettimeout = sockettimeout; public static connection getconn() { clickhouseconnection conn = null; clickhouseproperties properties = new clickhouseproperties(); properties.setuser(clickhouseusername); properties.setpassword(clickhousepassword); properties.setdatabase(clickhousedb); properties.setsockettimeout(clickhousesockettimeout); clickhousedatasource clickhousedatasource = new clickhousedatasource(clickhouseaddress,properties); try { conn = clickhousedatasource.getconnection(); return conn; } catch (sqlexception e) { e.printstacktrace(); } return null; public static list<jsonobject> exesql(string sql){ log.info("cliockhouse 执行sql:" + sql); connection connection = getconn(); statement statement = connection.createstatement(); resultset results = statement.executequery(sql); resultsetmetadata rsmd = results.getmetadata(); list<jsonobject> list = new arraylist(); while(results.next()){ jsonobject row = new jsonobject(); for(int i = 1;i<=rsmd.getcolumncount();i++){ row.put(rsmd.getcolumnname(i),results.getstring(rsmd.getcolumnname(i))); } list.add(row); } return list; }
第四步:test简单使用执行sql查询数据
import com.renrenche.databus.common.clickhouseutil; import com.renrenche.databus.common.result; import com.renrenche.databus.domain.logdata.fem.femparam; import com.renrenche.databus.service.fem.femmainservice; import net.sf.json.jsonobject; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.test.context.springboottest; import org.springframework.test.context.junit4.springrunner; import java.util.list; /** * @auther: qixin * @date: 2018/12/11 15:05 * @description: */ @runwith(springrunner.class) @springboottest public class semtest { @test public void getfrsdatatest(){ system.out.println("******************"); string sql="select * from marketing.sem_campaign_real_time_report"; list<jsonobject> result= clickhouseutil.exesql(sql); } }
执行完毕打印结果查看即可,
fetch方法之后再补充
到此这篇关于springboot 使用clickhouse实时大数据分析引擎的方法的文章就介绍到这了,更多相关springboot clickhouse大数据分析引擎内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!