Merge pull request #15629 from parthiv39731/PR-7319
BAEL-7319, Introduction to Apache Calcite
This commit is contained in:
commit
a4f8be9d51
|
@ -0,0 +1,45 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>java-calcite</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>persistence-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>9</source>
|
||||||
|
<target>9</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.calcite</groupId>
|
||||||
|
<artifactId>calcite-core</artifactId>
|
||||||
|
<version>${calcite.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.calcite</groupId>
|
||||||
|
<artifactId>calcite-file</artifactId>
|
||||||
|
<version>${calcite.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<calcite.version>1.34.0</calcite.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.baledung.calcite.model;
|
||||||
|
|
||||||
|
public class CompanySchema {
|
||||||
|
public Employee[] employees;
|
||||||
|
public Department[] departments;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.baledung.calcite.model;
|
||||||
|
|
||||||
|
public class Department {
|
||||||
|
public String deptId;
|
||||||
|
public String deptName;
|
||||||
|
|
||||||
|
public Department(String deptId, String deptName) {
|
||||||
|
this.deptId = deptId;
|
||||||
|
this.deptName = deptName;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baledung.calcite.model;
|
||||||
|
|
||||||
|
public class Employee {
|
||||||
|
public String name;
|
||||||
|
public String id;
|
||||||
|
|
||||||
|
public String deptId;
|
||||||
|
|
||||||
|
public Employee(String name, String id, String deptId) {
|
||||||
|
this.name = name;
|
||||||
|
this.id = id;
|
||||||
|
this.deptId = deptId;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
@startuml
|
||||||
|
'https://plantuml.com/class-diagram
|
||||||
|
|
||||||
|
|
||||||
|
skinparam Handwritten true
|
||||||
|
skinparam ClassBorderColor black/#63b175
|
||||||
|
skinparam BackgroundColor #fdf8f6
|
||||||
|
skinparam class {
|
||||||
|
ArrowColor black/#63b175
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
'hide empty methods
|
||||||
|
hide empty attributes
|
||||||
|
|
||||||
|
|
||||||
|
interface Schema {
|
||||||
|
+getTable()
|
||||||
|
+getType()
|
||||||
|
+getFunctions()
|
||||||
|
+more methods()..
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SchemaFactory {
|
||||||
|
+creates()
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Table {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TableFactory <T extends Table> {
|
||||||
|
+create()
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SchemaFactory {
|
||||||
|
}
|
||||||
|
interface Schema {
|
||||||
|
}
|
||||||
|
interface Table {
|
||||||
|
}
|
||||||
|
interface TableFactory {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
SchemaFactory -right-> Schema:creates
|
||||||
|
CassandraSchemaFactory -right-|> SchemaFactory:implements
|
||||||
|
CsvSchemaFactory -down-|> SchemaFactory:implements
|
||||||
|
MongoSchemaFactory -down-|> SchemaFactory:implements
|
||||||
|
FileSchemaFactory -down-|> SchemaFactory:implements
|
||||||
|
TableFactory -right-> Table:creates
|
||||||
|
Table <-up- Schema:gets
|
||||||
|
CassandraSchema -down-|> Schema:implements
|
||||||
|
CsvSchema -down-|> Schema:implements
|
||||||
|
MongoSchema -up-|> Schema:implements
|
||||||
|
SplunkSchema -up-|> Schema:implements
|
||||||
|
|
||||||
|
@enduml
|
|
@ -0,0 +1,97 @@
|
||||||
|
package com.baeldung.calcite;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.apache.calcite.adapter.java.ReflectiveSchema;
|
||||||
|
import org.apache.calcite.jdbc.CalciteConnection;
|
||||||
|
import org.apache.calcite.schema.Schema;
|
||||||
|
import org.apache.calcite.schema.SchemaPlus;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.baledung.calcite.model.CompanySchema;
|
||||||
|
import com.baledung.calcite.model.Department;
|
||||||
|
import com.baledung.calcite.model.Employee;
|
||||||
|
|
||||||
|
public class CalciteUnitTest {
|
||||||
|
Logger logger = LoggerFactory.getLogger(CalciteUnitTest.class);
|
||||||
|
static CompanySchema companySchema = new CompanySchema();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenCsvSchema_thenQuerySuccess() throws SQLException {
|
||||||
|
Properties info = new Properties();
|
||||||
|
info.put("model", getPath("model.json"));
|
||||||
|
try(Connection connection = DriverManager.getConnection("jdbc:calcite:", info)) {
|
||||||
|
Statement statement = connection.createStatement();
|
||||||
|
ResultSet resultSet = statement.executeQuery("select * from trades.trade");
|
||||||
|
|
||||||
|
assertEquals(3, resultSet.getMetaData().getColumnCount());
|
||||||
|
|
||||||
|
List<Integer> tradeIds = new ArrayList<>();
|
||||||
|
while(resultSet.next()) {
|
||||||
|
tradeIds.add(resultSet.getInt("tradeid"));
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(3, tradeIds.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getPath(String model) {
|
||||||
|
URL url = ClassLoader.getSystemClassLoader().getResource(model);
|
||||||
|
logger.info("path fetched :" + url.getPath());
|
||||||
|
return url.getPath();
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void setup() {
|
||||||
|
Department dept1 = new Department("HR", "Human Resource");
|
||||||
|
Department dept2 = new Department("MKT", "Marketing");
|
||||||
|
Department dept3 = new Department("FIN", "Finance");
|
||||||
|
|
||||||
|
Employee emp1 = new Employee("Tom", "1234", "HR");
|
||||||
|
Employee emp2 = new Employee("Harry", "39731", "FIN");
|
||||||
|
Employee emp3 = new Employee("Danny", "45632", "FIN");
|
||||||
|
Employee emp4 = new Employee("Jenny", "78654", "MKT");
|
||||||
|
|
||||||
|
companySchema.departments = new Department[]{dept1, dept2, dept3};
|
||||||
|
companySchema.employees = new Employee[]{emp1, emp2, emp3, emp4};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenQueryEmployeesObject_thenQuerySuccess() throws SQLException {
|
||||||
|
Properties info = new Properties();
|
||||||
|
info.setProperty("lex", "JAVA");
|
||||||
|
Connection connection = DriverManager.getConnection("jdbc:calcite:", info);
|
||||||
|
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
|
||||||
|
SchemaPlus rootSchema = calciteConnection.getRootSchema();
|
||||||
|
Schema schema = new ReflectiveSchema(companySchema);
|
||||||
|
rootSchema.add("company", schema);
|
||||||
|
Statement statement = calciteConnection.createStatement();
|
||||||
|
String query = "select dept.deptName, count(emp.id) "
|
||||||
|
+ "from company.employees as emp "
|
||||||
|
+ "join company.departments as dept "
|
||||||
|
+ "on (emp.deptId = dept.deptId) "
|
||||||
|
+ "group by dept.deptName";
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
|
ResultSet resultSet = statement.executeQuery(query);
|
||||||
|
while (resultSet.next()) {
|
||||||
|
logger.info("Dept Name:" + resultSet.getString(1)
|
||||||
|
+ " No. of employees:" + resultSet.getInt(2));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
{
|
||||||
|
"version": "1.0",
|
||||||
|
"defaultSchema": "TRADES",
|
||||||
|
"schemas": [
|
||||||
|
{
|
||||||
|
"name": "TRADES",
|
||||||
|
"type": "custom",
|
||||||
|
"factory": "org.apache.calcite.adapter.file.FileSchemaFactory",
|
||||||
|
"operand": {
|
||||||
|
"directory": "trades"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
tradeid:int,productid:string,qty:int
|
||||||
|
232312123,"RFTXC",100
|
||||||
|
232312124,"RFUXC",200
|
||||||
|
232312125,"RFSXC",1000
|
|
|
@ -38,6 +38,7 @@
|
||||||
<module>hibernate-queries</module>
|
<module>hibernate-queries</module>
|
||||||
<module>hibernate-enterprise</module>
|
<module>hibernate-enterprise</module>
|
||||||
<module>influxdb</module>
|
<module>influxdb</module>
|
||||||
|
<module>java-calcite</module>
|
||||||
<module>java-cassandra</module>
|
<module>java-cassandra</module>
|
||||||
<module>java-cockroachdb</module>
|
<module>java-cockroachdb</module>
|
||||||
<module>java-jdbi</module>
|
<module>java-jdbi</module>
|
||||||
|
|
Loading…
Reference in New Issue