Java通过calcite实时读取kafka中的数据

引入maven依赖

<dependency>

<groupId>org.apache.calcite</groupId>

<artifactId>calcite-kafka</artifactId>

<version>1.28.0</version>

</dependency>

测试代码

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.util.Properties;

public class CalciteDemo {

public static void main(String[] args) throws SQLException {

String model = "inline:" +

"{\n" +

" \"version\": \"1.0\",\n" +

" \"defaultSchema\": \"KAFKA\",\n" +

" \"schemas\": [\n" +

" {\n" +

" \"name\": \"KAFKA\",\n" +

" \"tables\": [\n" +

" {\n" +

" \"name\": \"TEST_TABLE\",\n" +

" \"factory\": \"org.apache.calcite.adapter.kafka.KafkaTableFactory\",\n" +

" \"stream\": { \"stream\": true },\n" +

" \"operand\": {\n" +

" \"bootstrap.servers\": \"192.168.x.xx:9092\",\n" +

" \"topic.name\": \"my-cloud-events\",\n" +

" \"consumer.params\": {\n" +

" \"group.id\": \"calcite-ut-consumer\",\n" +

" \"key.deserializer\": \"org.apache.kafka.common.serialization.ByteArrayDeserializer\",\n" +

" \"value.deserializer\": \"org.apache.kafka.common.serialization.ByteArrayDeserializer\"\n" +

" }\n" +

" }\n" +

" }\n" +

" ]\n" +

" }\n" +

" ]\n" +

"}";

Properties info = new Properties();

info.put("model", model);

Connection connection = DriverManager.getConnection("jdbc:calcite:", info);

final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);

final String sql7 = "SELECT STREAM * FROM \"KAFKA\".\"TEST_TABLE\"";

print(calciteConnection,sql7);

connection.close();

calciteConnection.close();

}

public static void print(CalciteConnection calciteConnection, String sql7) throws SQLException {

final PreparedStatement statement = calciteConnection.prepareStatement(sql7);

final ResultSet resultSet = statement.executeQuery();

ResultSetMetaData metadata = resultSet.getMetaData();

while (resultSet.next()) {

for (int i = 1; i <= metadata.getColumnCount(); i++) {

System.out.print(metadata.getColumnLabel(i) + "=" + resultSet.getString(i) + ",");

}

System.out.println();

}

}

}

发送测试数据

运行结果

相关推荐
空の鱼4 小时前
java开发,IDEA转战VSCODE配置(mac)
java·vscode
P7进阶路5 小时前
Tomcat异常日志中文乱码怎么解决
java·tomcat·firefox
小丁爱养花5 小时前
Spring MVC:HTTP 请求的参数传递2.0
java·后端·spring
CodeClimb5 小时前
【华为OD-E卷 - 第k个排列 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
等一场春雨6 小时前
Java设计模式 九 桥接模式 (Bridge Pattern)
java·设计模式·桥接模式
带刺的坐椅6 小时前
[Java] Solon 框架的三大核心组件之一插件扩展体系
java·ioc·solon·plugin·aop·handler
不惑_7 小时前
深度学习 · 手撕 DeepLearning4J ,用Java实现手写数字识别 (附UI效果展示)
java·深度学习·ui
费曼乐园7 小时前
Kafka中bin目录下面kafka-run-class.sh脚本中的JAVA_HOME
java·kafka
Shinobi_Jack7 小时前
c#使用Confluent.Kafka实现生产者发送消息至kafka(远程连接kafka发送消息超时的解决 Local:Message timed out)
分布式·kafka
feilieren7 小时前
SpringBoot 搭建 SSE
java·spring boot·spring