Table Store提供 JDBC 驅動(com.aliyun.openservices:tablestore-jdbc),支援通過標準 JDBC 介面直連Table Store執行個體執行 SQL 查詢。
前提條件
已擷取 AccessKey,RAM 使用者需具備
"Action": "ots:SQL*"許可權。已建立資料表和映射表,詳見DDL 操作。
步驟一:安裝 JDBC 驅動
可通過以下兩種方式安裝 JDBC 驅動。
添加Maven依賴
在 Maven 工程的 pom.xml 中添加Table Store JDBC 驅動依賴。以 5.17.0 版本為例,在 <dependencies> 內加入如下內容:
<dependency>
<groupId>com.aliyun.openservices</groupId>
<artifactId>tablestore-jdbc</artifactId>
<version>5.17.0</version>
</dependency>手動安裝
下載 Table Store JDBC 驅動並匯入到專案中。
步驟二:使用 JDBC 直連
使用
Class.forName()載入Table Store JDBC 驅動。Table Store JDBC 驅動名稱為
com.alicloud.openservices.tablestore.jdbc.OTSDriver。Class.forName("com.alicloud.openservices.tablestore.jdbc.OTSDriver");使用 JDBC 串連Table Store執行個體。
String url = "jdbc:ots:https://myinstance.cn-hangzhou.ots.aliyuncs.com/myinstance"; String user = "************************"; String password = "********************************"; Connection conn = DriverManager.getConnection(url, user, password);參數說明如下。
參數
說明
url
Table StoreJDBC的URL。格式為
jdbc:ots:schema://[accessKeyId:accessKeySecret@]endpoint/instanceName[?param1=value1&...¶mN=valueN]。主要欄位說明如下:schema(必選):Table StoreJDBC驅動使用的協議,一般設定為https。
accessKeyId:accessKeySecret(可選):阿里雲帳號或者RAM使用者的AccessKey ID和AccessKey Secret。
endpoint(必選):執行個體的服務地址。
instanceName(必選):執行個體名稱。
其他常用配置項的說明詳見配置項。
user
阿里雲帳號或者RAM使用者的AccessKey ID。
password
阿里雲帳號或者RAM使用者的AccessKey Secret。
可通過 URL 或 Properties 方式傳遞 AccessKey 和配置項。以下以通過公網訪問華東1(杭州)地區下 myinstance 執行個體為例。
通過URL
DriverManager.getConnection("jdbc:ots:https://************************:********************************@myinstance.cn-hangzhou.ots.aliyuncs.com/myinstance?enableRequestCompression=true");通過Properties
Properties info = new Properties(); info.setProperty("user", "************************"); info.setProperty("password", "********************************"); info.setProperty("enableRequestCompression", "true"); DriverManager.getConnection("jdbc:ots:https://myinstance.cn-hangzhou.ots.aliyuncs.com/myinstance", info);執行 SQL 陳述式。
支援 createStatement 和 prepareStatement 兩種方式。
createStatement
String sql = "SELECT pk1, col_a FROM test_table"; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { System.out.println(rs.getString("pk1") + ", " + rs.getLong("col_a")); } rs.close(); stmt.close();prepareStatement
String sql = "SELECT * FROM test_table WHERE pk = ?"; PreparedStatement stmt = conn.prepareStatement(sql); stmt.setLong(1, 1); ResultSet rs = stmt.executeQuery(); ResultSetMetaData meta = rs.getMetaData(); while (rs.next()) { for (int i = 1; i <= meta.getColumnCount(); i++) { System.out.println(meta.getColumnName(i) + " = " + rs.getString(i)); } } rs.close(); stmt.close();
完整樣本
以下樣本用於查詢Table Store執行個體中 test_table 的資料。
public class Demo {
public static void main(String[] args) throws Exception {
Class.forName("com.alicloud.openservices.tablestore.jdbc.OTSDriver");
String url = "jdbc:ots:https://myinstance.cn-hangzhou.ots.aliyuncs.com/myinstance";
String user = "************************";
String password = "********************************";
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM test_table");
ResultSetMetaData meta = rs.getMetaData();
int colCount = meta.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= colCount; i++) {
System.out.print(meta.getColumnName(i) + "=" + rs.getString(i) + "\t");
}
System.out.println();
}
rs.close();
stmt.close();
conn.close();
}
}配置項
Table Store JDBC 驅動基於 Java SDK 實現,支援通過 URL 參數或 Properties 修改配置項。
SQL 請求服務端逾時為 30 秒。如需更短的逾時時間,將 syncClientWaitFutureTimeoutInMillis 設定為小於 30000 的值,或對每個 Statement 調用 setQueryTimeout 方法。
配置項 | 預設值 | 說明 |
enableRequestCompression | false | 是否壓縮請求資料。 |
enableResponseCompression | false | 是否壓縮響應資料。 |
ioThreadCount | 2 | HttpAsyncClient 的 IOReactor 線程數。 |
maxConnections | 300 | 允許開啟的最大 HTTP 串連數。 |
socketTimeoutInMillisecond | 30000 | Socket 層傳輸資料的逾時時間。單位為毫秒。0 表示無限等待。 |
connectionTimeoutInMillisecond | 30000 | 建立串連的逾時時間。單位為毫秒。0 表示無限等待。 |
retryThreadCount | 1 | 錯誤重試線程池的線程數。 |
syncClientWaitFutureTimeoutInMillis | -1 | 非同步等待的逾時時間。單位為毫秒。 |
connectionRequestTimeoutInMillisecond | 60000 | 發送請求的逾時時間。單位為毫秒。 |
retryStrategy | default | 重試策略,取值範圍如下:
|
retryTimeout | 10 | 重試逾時時間和時間單位。時間單位的取值範圍如下:
|
retryTimeoutUnit | seconds |
資料類型轉換
Table Store支援 Integer、Double、String、Binary 和 Boolean 五種資料類型。JDBC 驅動在 Java 類型和Table Store資料類型之間自動轉換。
Java類型轉換為Table Store資料類型
使用 PreparedStatement 為 SQL 參數賦值時,支援 Byte、Short、Int、Long、BigDecimal、Float、Double、String、CharacterStream、Bytes、Boolean 類型。
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM t WHERE pk = ?");
stmt.setLong(1, 1); // 支援
stmt.setURL(1, new URL("https://aliyun.com/")); // 不支援,拋出異常Table Store資料類型轉換為Java類型
使用 ResultSet 擷取返回結果時,資料類型自動轉換規則如下。
Table Store資料類型 | 轉換原則說明 |
Integer |
|
Double | |
String |
|
Binary | |
Boolean |
|
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT count(*) FROM t");
while (rs.next()) {
rs.getLong(1); // 支援
rs.getCharacterStream(1); // 不支援,拋出異常
}Table Store資料類型和 Java 類型的轉換支援情況如下。
"✓"表示正常轉換,"~"表示可能拋出異常,"×"表示無法轉換。
類型轉換 | Integer | Double | String | Binary | Boolean |
Byte | ~ | ~ | ~ | ~ | ✓ |
Short | ~ | ~ | ~ | ~ | ✓ |
Int | ~ | ~ | ~ | ~ | ✓ |
Long | ✓ | ~ | ~ | ~ | ✓ |
BigDecimal | ✓ | ✓ | ~ | ~ | ✓ |
Float | ✓ | ✓ | ~ | ~ | ✓ |
Double | ✓ | ✓ | ~ | ~ | ✓ |
String | ✓ | ✓ | ✓ | ✓ | ✓ |
CharacterStream | × | × | ✓ | ✓ | × |
Bytes | ✓ | ✓ | ✓ | ✓ | ✓ |
Boolean | ✓ | ✓ | ✓ | ✓ | ✓ |