gpt4 book ai didi

druid - 从java应用程序查询druid

转载 作者:行者123 更新时间:2023-12-01 22:17:55 25 4
gpt4 key购买 nike

我是德鲁伊新手。我想从我的 java 应用程序查询远程 druid 集群。我在 druid-user google group 中读到我们可以使用 io.druid.client.DirectDruidClient 。有人可以帮助我或指出一个带有相同示例的资源吗?

最佳答案

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

假设 Druid 在本地运行,并且您已经在名为“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>

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com