Druid.io启用SQL支持
Druid.io的SQL功能虽然在试验阶段,但是也支持了大部分的功能,而且还可以通过 Avatica JDBC查看请求的json,有助于我们理解Druid.io的语法。Druid.io有个比较坑的是,如果没有启用SQL功能,你怎么访问也是没有data返回的,但也不会提示你错误。
1、修改配置文件
为了支持Druid SQL,需要修改common.runtime.properties或者 runtime.properties的druid.sql.enable为true就行。
To enable Druid SQL, make sure you have set druid.sql.enable = true either in your common.runtime.properties or your broker's runtime.properties.
注意启动Druid时,使用的命令是官网提供的命令的话
java `cat conf-quickstart/druid/historical/jvm.config | xargs` -cp "conf-quickstart/druid/_common:conf-quickstart/druid/historical:lib/*" io.druid.cli.Main server historical
java `cat conf-quickstart/druid/broker/jvm.config | xargs` -cp "conf-quickstart/druid/_common:conf-quickstart/druid/broker:lib/*" io.druid.cli.Main server broker
java `cat conf-quickstart/druid/coordinator/jvm.config | xargs` -cp "conf-quickstart/druid/_common:conf-quickstart/druid/coordinator:lib/*" io.druid.cli.Main server coordinator
java `cat conf-quickstart/druid/overlord/jvm.config | xargs` -cp "conf-quickstart/druid/_common:conf-quickstart/druid/overlord:lib/*" io.druid.cli.Main server overlord
java `cat conf-quickstart/druid/middleManager/jvm.config | xargs` -cp "conf-quickstart/druid/_common:conf-quickstart/druid/middleManager:lib/*" io.druid.cli.Main server middleManager
那么,你应该修改conf-quickstart/druid/broker/runtime.properties或者conf-quickstart/druid/_common/common.runtime.properties,增加下面几个配置:
druid.sql.enable=true
druid.sql.avatica.enable=true
druid.sql.http.enable=true
2、测试HTTP SQL
测试前,需要导入测试的数据,这里直接使用quickstart的数据:http://druid.io/docs/0.12.0/tutorials/quickstart.html
$ cat query.json
{"query":"SELECT COUNT(*) AS TheCount FROM wikiticker"}
$ curl -XPOST -H'Content-Type: application/json' http://localhost:8082/druid/v2/sql/ -d @query.json
[{"TheCount":39244}]
使用EXPLAIN PLAN FOR
添加到任何查询的开头,可以查看通过SQL生成的原生json查询。
3、测试Jdbc
增加maven的依赖:
<dependency>
<groupId>org.apache.calcite.avatica</groupId>
<artifactId>avatica-core</artifactId>
<version>1.11.0</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.5.1</version>
</dependency>
// Connect to /druid/v2/sql/avatica/ on your broker.
String url = "jdbc:avatica:remote:url=http://localhost:8082/druid/v2/sql/avatica/";
// Set any connection context parameters you need here (see "Connection context" below).
// Or leave empty for default behavior.
Properties connectionProperties = new Properties();
try (Connection connection = DriverManager.getConnection(url, connectionProperties)) {
try (
final Statement statement = client.createStatement();
final ResultSet resultSet = statement.executeQuery(query)
) {
while (resultSet.next()) {
// Do something
}
}
}
暂时测试出不支持jdbc游标移动absolute的方法。
网上关于Druid的东西特别难找,都是阿里的那个连接池-。-
如果帮到你的话,就赞我一下呗^_^