使用限制
本文操作僅適用於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用戶端和資料之間的串連。
String url = "jdbc:lindorm:table:url=http://ld-bp17j28j2y7pm****-proxy-lindorm-pub.lindorm.rds.aliyuncs.com:30060";
Properties properties = new Properties();
properties.put("user", "root");
properties.put("password", "test");
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地址。如何擷取,請參見查看串連地址。 |
user | root | 如果您忘記使用者密碼,可以通過Lindorm寬表引擎的叢集管理系統修改密碼。 |
password | test |
database | default | 需要串連的資料庫名稱。預設串連default資料庫。 |
建立串連後,使用Lindorm寬表SQL Java API訪問Lindorm寬表。範例程式碼如下:
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();
}
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();
}
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寬表引擎的程式碼範例: