本文介紹通過Java語言的標準JDBC介面使用SQL開發Lindorm寬表應用的方法和樣本。
前提條件
已安裝Java環境,要求安裝JDK 1.8及以上版本。
已將用戶端IP地址添加至Lindorm白名單,具體操作請參見設定白名單。
使用限制
本文操作僅適用於Lindorm寬表模式,不支援Lindorm Serverless。
操作步驟
您可以使用操作步驟中的範例程式碼,也可以直接下載示例代码在本地編譯和運行。本文中範例程式碼在com.aliyun.lindorm.sql.demo.BasicDemo
類中。
下載Lindorm用戶端。以Maven專案為例,在pom.xml檔案的
dependencies
中添加依賴項。範例程式碼如下:<dependency> <groupId>com.aliyun.lindorm</groupId> <artifactId>lindorm-all-client</artifactId> <version>2.2.1.3</version> </dependency>
初始化Lindorm用戶端並建立Lindorm用戶端和資料之間的串連。
//Lindorm寬表SQL的串連地址。 String url = "jdbc:lindorm:table:url=http://ld-bp17j28j2y7pm****-proxy-lindorm-pub.lindorm.rds.aliyuncs.com:30060"; // 配置串連參數 Properties properties = new Properties(); //Lindorm控制台擷取的資料庫使用者名稱。 properties.put("user", "root"); //Lindorm控制台擷取的資料庫密碼。 properties.put("password", "test"); //建立串連時可指定某一database。若不指定database,預設使用default。 properties.put("database", "default"); //擷取串連 Connection connection = DriverManager.getConnection(url, properties);
重要為了提高資源使用率,在串連Lindorm用戶端空閑十分鐘後,服務端會主動中斷連線。如果此時複用該串連,會出現
com.aliyun.lindorm.client.shaded.org.apache.calcite.avatica.http.ConnectionDisconnectedException
報錯,重建立立串連即可解決。串連使用到的參數及其說明如下:
參數
樣本值
說明
url
jdbc:lindorm:table:url=http://ld-bp17j28j2y7pm****-proxy-lindorm-pub.lindorm.rds.aliyuncs.com:30060
Lindorm 寬表SQL地址。如何擷取,請參見查看串連地址。
重要如果應用部署在ECS執行個體,建議您通過專用網路訪問Lindorm執行個體,可獲得更高的安全性和更低的網路延遲。
如果應用部署在本地,在通過公網串連Lindorm執行個體前,需在控制台開通公網地址。開通方式:在控制台選擇
,在寬表引擎頁簽單擊開通公網地址。
user
root
如果您忘記使用者密碼,可以通過Lindorm寬表引擎的叢集管理系統修改密碼。
password
test
database
default
需要串連的資料庫名稱。預設串連default資料庫。
建立串連後,使用Lindorm寬表SQL Java API訪問Lindorm寬表。範例程式碼如下:
/* -------------- 基於JDBC的CRUD樣本 ----------------- */ String tableName = "sql_table_" + new Random().nextInt(1000); //建立表 try (Statement statement = connection.createStatement()) { String sql = "create table if not exists " + tableName + "(id VARCHAR, name VARCHAR, primary key(id))"; int ret = statement.executeUpdate(sql); System.out.println(ret); } //插入資料 String upsertSql = "upsert into " + tableName + "(id,name) values(?,?)"; try (PreparedStatement ps = connection.prepareStatement(upsertSql)) { int batchSize = 100; for (int i = 0; i < batchSize; i++) { ps.setString(1, "aa" + i); ps.setString(2, "bb" + i); //加入批次 ps.addBatch(); } //執行全部批次的寫入。 //出於效能和穩定性考量,添加的批次數量不宜過大。 //建議一次executeBatch()寫入的批次數最多在百層級。 int[] ret = ps.executeBatch(); System.out.println(Arrays.toString(ret)); } //查詢資料 String querySql = "select * from " + tableName + " where id=?"; try (PreparedStatement ps = connection.prepareStatement(querySql)) { ps.setString(1, "aa1"); ResultSet rs = ps.executeQuery(); while (rs.next()) { String id = rs.getString(1); String name = rs.getString(2); System.out.println("id=" + id); System.out.println("name=" + name); } } //刪除資料 String deleteSql = "delete from " + tableName + " where id=?"; try (PreparedStatement ps = connection.prepareStatement(deleteSql)) { ps.setString(1, "aa1"); ps.executeUpdate(); } // 更新資料 // Lindorm SQL Update語句僅支援單行更新,不支援批次更新 // 即where條件必須指定全部主鍵 String updateSql = "update " + tableName + " set name = ? where id=?"; try (PreparedStatement ps = connection.prepareStatement(updateSql)) { ps.setString(1, "bb2update"); ps.setString(2, "aa2"); ps.executeUpdate(); } String querySql1 = "select * from " + tableName + " where id=?"; try (PreparedStatement ps = connection.prepareStatement(querySql1)) { ps.setString(1, "aa2"); ResultSet rs = ps.executeQuery(); System.out.println("--------- update-----------"); while (rs.next()) { String id = rs.getString(1); String name = rs.getString(2); System.out.println("id=" + id); System.out.println("name=" + name); } } // 刪除表 String dropTable = "drop table " + tableName; try (Statement stmt = connection.createStatement()) { stmt.execute(dropTable); } //關閉串連,當結束操作時要確保串連被關閉,否則會造成串連泄漏。 connection.close();
常用架構訪問寬表引擎的樣本
為了滿足更多使用者的需求,下文匯總了不同Java架構訪問Lindorm寬表引擎的程式碼範例:
通過Druid架構訪問Lindorm寬表引擎,程式碼範例請參見Druid訪問樣本。
通過Spring架構訪問Lindorm寬表引擎,程式碼範例請參見Spring訪問樣本。
通過Mybatis架構訪問Lindorm寬表SQL,程式碼範例請參見Mybatis訪問樣本。
通過Hibernate架構訪問Lindorm寬表SQL,程式碼範例請參見Hibernate訪問樣本。
相關文檔
關於Lindorm寬表SQL的文法使用請參見SQL文法手冊。