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

}

}

}

发送测试数据

运行结果

相关推荐
叫致寒吧14 小时前
Tomcat详解
java·tomcat
S***267518 小时前
基于SpringBoot和Leaflet的行政区划地图掩膜效果实战
java·spring boot·后端
马剑威(威哥爱编程)19 小时前
鸿蒙6开发视频播放器的屏幕方向适配问题
java·音视频·harmonyos
JIngJaneIL19 小时前
社区互助|社区交易|基于springboot+vue的社区互助交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·社区互助
V***u45319 小时前
MS SQL Server partition by 函数实战二 编排考场人员
java·服务器·开发语言
这是程序猿20 小时前
基于java的ssm框架旅游在线平台
java·开发语言·spring boot·spring·旅游·旅游在线平台
i***t91920 小时前
基于SpringBoot和PostGIS的云南与缅甸的千里边境线实战
java·spring boot·spring
k***082920 小时前
【监控】spring actuator源码速读
java·spring boot·spring
麦麦鸡腿堡20 小时前
Java_网络编程_InetAddress类与Socket类
java·服务器·网络
vx_dmxq21121 小时前
【PHP考研互助系统】(免费领源码+演示录像)|可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案
java·spring boot·mysql·考研·微信小程序·小程序·php