Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query druid from java application

Tags:

druid

I'm new to druid. I want to query a remote druid cluster from my java application. I read in the druid-user google group that we can use io.druid.client.DirectDruidClient . Can someone please help me or point out a resource with an example for the same?

like image 775
Priyanka Avatar asked Nov 20 '22 12:11

Priyanka


1 Answers

Here is a simple Spring Boot Java Application which queries Druid data using Avatica JDBC Driver and prints the first row from the query.

Assuming that Druid is running in local and you already have data in a table name "druid_table" which has a column 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>
like image 99
ShashankNiranjan Avatar answered Dec 23 '22 00:12

ShashankNiranjan