我是德鲁伊的新手。我想从我的Java应用程序查询远程Druid集群。我在德鲁伊用户google组中阅读到可以使用io.druid.client.DirectDruidClient的信息。有人可以帮我吗,还是可以提供带有示例的资源?

最佳答案

这是一个简单的 Spring Boot Java应用程序,它使用 Avatica JDBC驱动程序查询 Druid 数据,并打印查询的第一行。

假设Druid在本地运行,并且您已经在表名称“ druid_table ”中包含了数据,该名称具有一列 sourceIP


@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());
        }


    }

}


<?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>

关于druid - 从Java应用程序查询Druid,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40465425/

10-10 22:58