SQL-JDBC 驱动 #
Easysearch 的 SQL jdbc 驱动程序是一个独立、直连的纯 Java 驱动程序,可将 JDBC 调用转换为 Easysearch SQL。
安装 #
JDBC 驱动 可以从官网下载:https://release.infinilabs.com/easysearch/archive/plugins/sql-jdbc-1.7.1.jar
在 gradle 项目中安装 #
需要把sql-jdbc-1.x.x.jar 集成到本地 gradle 项目的libs目录,假设项目名称叫 jdbc-test
将下载的sql-jdbc jar 包放到 jdbc-test/libs/ 下:
在 项目 build.gradle 添加依赖
implementation files(‘libs/sql-jdbc-1.0.0.jar’)
完整的build.gradle 配置:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
dependencies {
implementation files('libs/sql-jdbc-1.0.0.jar')
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}
test {
useJUnitPlatform()
}
初始化 #
String url = "jdbc:easysearch://https://localhost:9210";
Properties properties = new Properties();
properties.put("trustStoreLocation", "/Users/xxxx/instance.jks");
properties.put("trustStorePassword", "123456");
properties.put("hostnameVerification", "false");
properties.put("user", "admin");
properties.put("password", "admin");
properties.put("ssl", "true");
Connection con = DriverManager.getConnection(url, properties);
Statement st = con.createStatement();
查询示例 #
事先创建 accounts 索引并插入几条示例数据
POST accounts/_bulk
{"index":{"_id":"1"}}
{"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
{"index":{"_id":"6"}}
{"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"hattiebond@netagy.com","city":"Dante","state":"TN"}
{"index":{"_id":"13"}}
{"account_number":13,"balance":32838,"firstname":"Nanette","lastname":"Bates","age":28,"gender":"F","address":"789 Madison Street","employer":"Quility","email":"nanettebates@quility.com","city":"Nogal","state":"VA"}
{"index":{"_id":"18"}}
{"account_number":18,"balance":4180,"firstname":"Dale","lastname":"Adams","age":33,"gender":"M","address":"467 Hutchinson Court","employer":"Boink","email":"daleadams@boink.com","city":"Orick","state":"MD"}
执行查询
Connection con = DriverManager.getConnection(url, properties);
Statement st = con.createStatement();
ResultSet rs = null;
//rs = st.executeQuery("SELECT firstname, lastname FROM accounts");
rs = st.executeQuery("SELECT firstname, lastname, age FROM accounts WHERE age > 20 ORDER BY state ASC");
while (rs.next()) {
String firstname = rs.getString("firstname");
String lastname = rs.getString("lastname");
int age = rs.getInt("age");
System.out.println("firstname: " + firstname + " lastname:" + lastname + " age:" + age);
}
if (rs != null)
rs.close();
if (st != null)
st.close();
con.close();
输出
firstname: Amber lastname:Duke age:32
firstname: Dale lastname:Adams age:33
firstname: Hattie lastname:Bond age:36
firstname: Nanette lastname:Bates age:28