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 分钟前
泛型(2)
java
南宫生13 分钟前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
转码的小石21 分钟前
12/21java基础
java
李小白6629 分钟前
Spring MVC(上)
java·spring·mvc
GoodStudyAndDayDayUp42 分钟前
IDEA能够从mapper跳转到xml的插件
xml·java·intellij-idea
装不满的克莱因瓶1 小时前
【Redis经典面试题六】Redis的持久化机制是怎样的?
java·数据库·redis·持久化·aof·rdb
n北斗1 小时前
常用类晨考day15
java
骇客野人1 小时前
【JAVA】JAVA接口公共返回体ResponseData封装
java·开发语言
yuanbenshidiaos2 小时前
c++---------数据类型
java·jvm·c++
向宇it3 小时前
【从零开始入门unity游戏开发之——C#篇25】C#面向对象动态多态——virtual、override 和 base 关键字、抽象类和抽象方法
java·开发语言·unity·c#·游戏引擎