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 分钟前
easyExcel - 导出合并单元格
java
XMJ200237 分钟前
flutter插件:录制系统播放的声音
java·flutter
编程修仙1 小时前
Vector和ArrayList的比较
java·开发语言
fat house cat_1 小时前
MySQL是怎么解决幻读和不可重复读的?
java·数据库·mysql
程序猿麦小七2 小时前
springboot006基于SpringBoot的网上订餐系统(源码+包运行+LW+技术指导)
java·spring boot·后端·网上订餐
Daniel521-Spark2 小时前
三、谷粒商城- Spring Cloud Alibaba(3)
java·分布式·后端·spring cloud
《源码好优多》2 小时前
基于Java Springboot图书馆管理系统
java·开发语言·spring boot
查理不安生2 小时前
【Mac】未能完成该操作 Unable to locate a Java Runtime
java·开发语言·macos
蓝天星空2 小时前
基于springboot的用户管理系统
java·spring
乐鑫科技 Espressif2 小时前
“乐鑫组件注册表”简介
java·前端·microsoft·iot·乐鑫科技