日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

通過Native SDK連接并訪問時(shí)序引擎

本文檔主要介紹Lindorm 時(shí)序引擎Java Native SDK的安裝以及快速開始。

前提條件

  • 云原生多模數(shù)據(jù)庫Lindorm實(shí)例已開通時(shí)序引擎。

  • 已安裝Java環(huán)境,要求安裝JDK 1.8及以上版本。

  • 時(shí)序引擎版本為3.4.7及以上版本,查看和升級時(shí)序引擎版本的方法請參見時(shí)序引擎版本說明

  • 已將客戶端IP地址添加至Lindorm白名單,具體操作請參見設(shè)置白名單

  • 已獲取云原生多模數(shù)據(jù)庫Lindorm時(shí)序引擎的連接地址,獲取方法請參見獲取連接串

準(zhǔn)備工作

通過Java Native SDK連接Lindorm時(shí)序引擎前,需要安裝Java Native SDK。以1.0.0版本為例,您可以通過以下三種方式安裝Java Native SDK:

  • (推薦)在Maven項(xiàng)目中使用Lindorm TSDB Java SDK。在pom.xml文件的dependencies中添加以下依賴項(xiàng)。

    <dependency>
      <groupId>com.aliyun.lindorm</groupId>
      <artifactId>lindorm-tsdb-client</artifactId>
      <version>1.0.4</version>
    </dependency>
    說明

    Lindorm TSDB Java SDK提供了一個(gè)基于Maven的示例工程,您可以直接下載示例工程并在本地編譯和運(yùn)行,也可以以示例工程為基礎(chǔ)開發(fā)您的項(xiàng)目工程。

  • 在Eclipse項(xiàng)目中導(dǎo)入JAR包。

    1. 下載Java SDK開發(fā)包

    2. 解壓下載的Java SDK開發(fā)包。

    3. 將解壓后JAR包添加至Eclipse項(xiàng)目中。

      1. 在Eclipse中打開您的項(xiàng)目,右鍵單擊該項(xiàng)目,選擇Properties

      2. 在彈出的對話框中,單擊Java Build Path > Libraries > Add JARs,選擇解壓后的lindorm-tsdb-client-1.0.0.jarlib文件中的JAR包。

      3. 單擊Apply and Close

  • 在IntelliJ IDEA項(xiàng)目中導(dǎo)入JAR包。

    1. 下載Java SDK開發(fā)包

    2. 解壓下載的Java SDK開發(fā)包。

    3. 將解壓后JAR包添加至IntelliJ IDEA項(xiàng)目中。

      1. 在IntelliJ IDEA中打開您的項(xiàng)目,在菜單欄單擊File > Project Structure

      2. Project Structure對話框的左邊選擇Project Structure > Modules

      3. 單擊右邊添加,選擇JARs or directories

      4. 在彈出的對話框中,選擇解壓后的lindorm-tsdb-client-1.0.0.jarlib文件中的JAR包,并單擊OK

      5. 單擊Apply

      6. 單擊OK

說明
  • Lindorm時(shí)序引擎的Java Native SDK各版本可以通過Maven中央倉庫獲取,更多信息請參見Maven Repository

  • Lindorm時(shí)序引擎的Java Native SDK各版本說明請參見版本說明

操作步驟

  1. 創(chuàng)建數(shù)據(jù)庫實(shí)例。新建LindormTSDBClient時(shí),需要指定Lindorm時(shí)序引擎的連接地址,獲取方法請參見獲取連接串

    String url = "http://ld-bp17j28j2y7pm****-proxy-tsdb-pub.lindorm.rds.aliyuncs.com:8242";
    // LindormTSDBClient線程安全,可以重復(fù)使用,無需頻繁創(chuàng)建和銷毀
    ClientOptions options = ClientOptions.newBuilder(url).build();
    LindormTSDBClient lindormTSDBClient = LindormTSDBFactory.connect(options);
  2. 創(chuàng)建數(shù)據(jù)庫demo和時(shí)序表sensor。關(guān)于創(chuàng)建數(shù)據(jù)庫和時(shí)序表的SQL語句說明,請參見CREATE DATABASECREATE TABLE

    lindormTSDBClient.execute("CREATE DATABASE demo");
    lindormTSDBClient.execute("demo","CREATE TABLE sensor (device_id VARCHAR TAG,region VARCHAR TAG,time BIGINT,temperature DOUBLE,humidity DOUBLE,PRIMARY KEY(device_id))");
  3. 在表中寫入數(shù)據(jù)。

    說明

    默認(rèn)情況下,為了提高寫入數(shù)據(jù)的性能,LindormTSDBClient通過異步攢批的方式進(jìn)行數(shù)據(jù)寫入。如果需要通過同步的方式進(jìn)行數(shù)據(jù)寫入,可以調(diào)用write()方法返回的CompletableFuture<WriteResult>join()方法。

    int numRecords = 10;
    List<Record> records = new ArrayList<>(numRecords);
    long currentTime = System.currentTimeMillis();
    for (int i = 0; i < numRecords; i++) {
        Record record = Record
            .table("sensor")
            .time(currentTime + i * 1000)
            .tag("device_id", "F07A1260")
            .tag("region", "north-cn")
            .addField("temperature", 12.1 + i)
            .addField("humidity", 45.0 + i)
            .build();
        records.add(record);
    }
    
    CompletableFuture<WriteResult> future = lindormTSDBClient.write("demo", records);
    // 處理異步寫入結(jié)果
    future.whenComplete((r, ex) -> {
        // 處理寫入失敗
        if (ex != null) {
            System.out.println("Failed to write.");
            if (ex instanceof LindormTSDBException) {
                LindormTSDBException e = (LindormTSDBException) ex;
                System.out.println("Caught an LindormTSDBException, which means your request made it to Lindorm TSDB, "
                                   + "but was rejected with an error response for some reason.");
                System.out.println("Error Code: " + e.getCode());
                System.out.println("SQL State:  " + e.getSqlstate());
                System.out.println("Error Message: " + e.getMessage());
            }  else {
                ex.printStackTrace();
            }
        } else  {
            System.out.println("Write successfully.");
        }
    });
    // 這里作為示例, 簡單同步處理寫入結(jié)果
    System.out.println(future.join());
  4. 查詢時(shí)序表的數(shù)據(jù)。關(guān)于查詢操作的SQL語句說明,請參見基本查詢

    String sql = "select * from sensor limit 10";
    ResultSet resultSet = lindormTSDBClient.query("demo", sql);
    
    try {
        // 處理查詢結(jié)果
        QueryResult result = null;
        // 查詢結(jié)果使用分批的方式返回,默認(rèn)每批1000行
        // 當(dāng)resultSet的next()方法返回為null,表示已經(jīng)讀取完所有的查詢結(jié)果
        while ((result = resultSet.next()) != null) {
            List<String> columns = result.getColumns();
            System.out.println("columns: " + columns);
            List<String> metadata = result.getMetadata();
            System.out.println("metadata: " + metadata);
            List<List<Object>> rows = result.getRows();
            for (int i = 0, size = rows.size(); i < size; i++) {
                List<Object> row = rows.get(i);
                System.out.println("row #" + i + " : " + row);
            }
        }
    } finally {
        // 查詢結(jié)束后,需確保調(diào)用ResultSet的close方法,以釋放IO資源
        resultSet.close();
    }

完整的代碼示例

import com.aliyun.lindorm.tsdb.client.ClientOptions;
import com.aliyun.lindorm.tsdb.client.LindormTSDBClient;
import com.aliyun.lindorm.tsdb.client.LindormTSDBFactory;
import com.aliyun.lindorm.tsdb.client.exception.LindormTSDBException;
import com.aliyun.lindorm.tsdb.client.model.QueryResult;
import com.aliyun.lindorm.tsdb.client.model.Record;
import com.aliyun.lindorm.tsdb.client.model.ResultSet;
import com.aliyun.lindorm.tsdb.client.model.WriteResult;
import com.aliyun.lindorm.tsdb.client.utils.ExceptionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class QuickStart {

    public static void main(String[] args) {

        // 1.創(chuàng)建客戶端實(shí)例
        String url = "http://ld-xxxx-proxy-tsdb-pub.lindorm.rds.aliyuncs.com:8242";
        // LindormTSDBClient線程安全,可以重復(fù)使用,無需頻繁創(chuàng)建和銷毀
        ClientOptions options = ClientOptions.newBuilder(url).build();
        LindormTSDBClient lindormTSDBClient = LindormTSDBFactory.connect(options);

        // 2.創(chuàng)建數(shù)據(jù)庫demo和表sensor
        lindormTSDBClient.execute("CREATE DATABASE demo");
        lindormTSDBClient.execute("demo","CREATE TABLE sensor (device_id VARCHAR TAG,region VARCHAR TAG,time BIGINT,temperature DOUBLE,humidity DOUBLE,PRIMARY KEY(device_id))");

        // 3.寫入數(shù)據(jù)
        int numRecords = 10;
        List<Record> records = new ArrayList<>(numRecords);
        long currentTime = System.currentTimeMillis();
        for (int i = 0; i < numRecords; i++) {
            Record record = Record
                    .table("sensor")
                    .time(currentTime + i * 1000)
                    .tag("device_id", "F07A1260")
                    .tag("region", "north-cn")
                    .addField("temperature", 12.1 + i)
                    .addField("humidity", 45.0 + i)
                    .build();
            records.add(record);
        }

        CompletableFuture<WriteResult> future = lindormTSDBClient.write("demo", records);
        // 處理異步寫入結(jié)果
        future.whenComplete((r, ex) -> {
            // 處理寫入失敗
            if (ex != null) {
                System.out.println("Failed to write.");
                Throwable throwable = ExceptionUtils.getRootCause(ex);
                if (throwable instanceof LindormTSDBException) {
                    LindormTSDBException e = (LindormTSDBException) throwable;
                    System.out.println("Caught an LindormTSDBException, which means your request made it to Lindorm TSDB, "
                            + "but was rejected with an error response for some reason.");
                    System.out.println("Error Code: " + e.getCode());
                    System.out.println("SQL State:  " + e.getSqlstate());
                    System.out.println("Error Message: " + e.getMessage());
                }  else {
                    throwable.printStackTrace();
                }
            } else  {
                System.out.println("Write successfully.");
            }
        });
        // 這里作為示例, 簡單同步等待
        System.out.println(future.join());

        // 4.查詢數(shù)據(jù)
        String sql = "select * from sensor limit 10";
        ResultSet resultSet = lindormTSDBClient.query("demo", sql);

        try {
            // 處理查詢結(jié)果
            QueryResult result = null;
            // 查詢結(jié)果使用分批的方式返回,默認(rèn)每批1000行
            // 當(dāng)resultSet的next()方法返回為null,表示已經(jīng)讀取完所有的查詢結(jié)果
            while ((result = resultSet.next()) != null) {
                List<String> columns = result.getColumns();
                System.out.println("columns: " + columns);
                List<String> metadata = result.getMetadata();
                System.out.println("metadata: " + metadata);
                List<List<Object>> rows = result.getRows();
                for (int i = 0, size = rows.size(); i < size; i++) {
                    List<Object> row = rows.get(i);
                    System.out.println("row #" + i + " : " + row);
                }
            }
        } finally {
            // 查詢結(jié)束后,需確保調(diào)用ResultSet的close方法,以釋放IO資源
            resultSet.close();
        }

        lindormTSDBClient.shutdown();
    }
}