天天看點

物聯網資料分析服務之資料開發API使用說明簡介使用場景架構圖API服務JAVA調用示例

簡介

物聯網資料分析,又稱Link Analytics,是阿裡雲為物聯網開發者提供的裝置智能分析服務,全鍊路覆寫了裝置資料生成、管理(存儲)、清洗、分析及可視化等環節。有效降低資料分析門檻,助力物聯網開發工作。

資料開發提供雲上互動式查詢服務,無需資料預處理過程,直接使用标準的SQL語句對裝置進行資料分析。

可以對以下三個資料來源中的裝置資料進行分析。

  1. 物聯網平台的系統資料
  2. 基于産品能力定義的裝置資料
  3. 使用者授權的業務資料

使用場景

無需實時處理資料的場景均可使用。但是如果對實時性有要求,請使用資料分析服務的實時資料分析功能,因為資料從裝置上報到物聯網平台,再完成SQL資料分析,有大約5分鐘左右的延遲。

  1. 裝置監控
  2. 報表統計
  3. 裝置行為分析

架構圖

物聯網資料分析服務之資料開發API使用說明簡介使用場景架構圖API服務JAVA調用示例

API服務

前提:已建立好物聯網平台上的産品及裝置

一、添加新的API

物聯網資料分析服務之資料開發API使用說明簡介使用場景架構圖API服務JAVA調用示例

二、編寫SQL語句

本文中的示例為統計産品表中去重裝置數

物聯網資料分析服務之資料開發API使用說明簡介使用場景架構圖API服務JAVA調用示例

三、測試與釋出

物聯網資料分析服務之資料開發API使用說明簡介使用場景架構圖API服務JAVA調用示例
物聯網資料分析服務之資料開發API使用說明簡介使用場景架構圖API服務JAVA調用示例
物聯網資料分析服務之資料開發API使用說明簡介使用場景架構圖API服務JAVA調用示例
物聯網資料分析服務之資料開發API使用說明簡介使用場景架構圖API服務JAVA調用示例

JAVA調用示例

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>資料開發API-Demo</groupId>
    <artifactId>資料開發API-Demo</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-iot -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-iot</artifactId>
            <version>6.10.0</version>
        </dependency>

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.3.5</version>
        </dependency>
    </dependencies>

</project>           

Demo.java

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.iot.model.v20180120.InvokeDataAPIServiceRequest;
import com.aliyuncs.iot.model.v20180120.InvokeDataAPIServiceResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Demo {

    public static void main(String args[]) throws ClientException {
        String accessKey = "*****";
        String accessSecret = "*****";
        DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Iot", "iot.cn-shanghai.aliyuncs.com");
        IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret);
        DefaultAcsClient client = new DefaultAcsClient(profile); //初始化SDK用戶端

        //在控制台的屬性參數設定裡截取API Path的一部分
        String apiSrn = "acs:iot:*:{uid}:serveapi/devicecount";

        //如果有請求參數,則需要設定
        InvokeDataAPIServiceRequest.Param param = new InvokeDataAPIServiceRequest.Param();
        // 請求參數名稱
        //param.setParamName("$device_name");
        // 線上狀态
        //param.setParamValue("***");

        InvokeDataAPIServiceRequest request = new InvokeDataAPIServiceRequest();
        request.setApiSrn(apiSrn);
        //request.setParams(Arrays.asList(param));
        // 當param為空時用請求方式用GET,如果不為空是用POST
        request.setSysMethod(MethodType.GET);

        try {
            InvokeDataAPIServiceResponse response = client.getAcsResponse(request);

            System.out.println(response.getSuccess());
            System.out.println(response.getErrorMessage());

            // 服務API指定的SQL查詢結果
            List<Map<Object, Object>> result = response.getData().getResultList();
            System.out.println(result);

        } catch (ClientException ce) {
            ce.printStackTrace();
        }
    }
}           

運作結果

物聯網資料分析服務之資料開發API使用說明簡介使用場景架構圖API服務JAVA調用示例