如何从java查询德鲁伊数据?

How to query druid data from java?

我知道德鲁伊有 scala/php 客户端,但我正在使用 java 并且我不想直接通过 HTTP 查询德鲁伊数据库,是任何 java 德鲁伊-有客户吗?

最简单的大概就是Druid SQL over JDBC.

请结帐DruidDry。这是一个基于 java 的查询德鲁伊的实用程序库。

Druid dry 支持写入查询 json 和使用 Java API 查询代理节点。目前它不支持一些复杂的 JSON,也不支持摄取规范。但是它确实支持最常见的操作,例如 select、扫描、分组等

您的项目需要 运行 Java 8 或更高版本才能与 druid dry client 一起工作。

您还可以使用基于 druidry、Java 的实用程序库

这是一个简单的Spring启动Java应用程序,它使用[=20=查询Druid数据]Avatica JDBC Driver 并打印查询的第一行。

假设德鲁伊在本地运行并且你已经在table名称中有数据“druid_table " 其中有一列 sourceIP

FlinkDruidApplication.java

@SpringBootApplication
public class FlinkDruidApplication {

    public static void main(String[] args) {
        SpringApplication.run(FlinkDruidApplication.class, args);
        Logger log = LoggerFactory.getLogger("FlinkDruidApplication");
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        DataSet<Row> dbData =
                env.createInput(
                        JDBCInputFormat
                                .buildJDBCInputFormat()
                                .setDrivername("org.apache.calcite.avatica.remote.Driver")
                                .setDBUrl("jdbc:avatica:remote:url=http://localhost:8082/druid/v2/sql/avatica/")
                                .setUsername("null")
                                .setPassword("null")
                                .setQuery(
                                        "SELECT sourceIP FROM druid_table"
                                )
                                .setRowTypeInfo((RowTypeInfo) Types.ROW(Types.STRING))
                                .finish()
                );

        try {

            log.info("Printing first IP :: {} " + dbData.collect().iterator().next());
        } catch (Exception e) {
            log.error(e.getMessage());
        }


    }

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.shashank</groupId>
    <artifactId>FlinkDruid</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>FlinkDruid</name>
    <description>Flink Druid Connection</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.flink/flink-core -->
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-core</artifactId>
            <version>1.9.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.flink/flink-streaming-java -->
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-streaming-java_2.12</artifactId>
            <version>1.9.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.flink/flink-java -->
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-java</artifactId>
            <version>1.9.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.flink/flink-clients -->
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-clients_2.12</artifactId>
            <version>1.9.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.flink/flink-jdbc -->
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-jdbc_2.12</artifactId>
            <version>1.8.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.calcite.avatica/avatica-core -->
        <dependency>
            <groupId>org.apache.calcite.avatica</groupId>
            <artifactId>avatica-core</artifactId>
            <version>1.15.0</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>