一.创建数据API
本文以产品数据为示例,自定义API可以参见:创建数据分析API
二.添加依赖
<dependency> <groupId>com.aliyun</groupId> <artifactId>tea-openapi</artifactId> <version>0.0.11</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>iot20180120</artifactId> <version>1.1.0</version> </dependency> <!--以下依赖为非必须项,只为下文示例代码中JSON序列化输出结果使用--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.61</version> </dependency>
三.java示例
condition设置参数,可以在API详情中API测试中查看,参数保持一致。
一个请求参数对应一个condition。在API详情页,查看API的请求参数,您可配置指定数量的condition。
本文示例代码中,该API有3个请求参数__instance_id__、entityId、statDate分别对应condition、condition1、condition2。
import com.alibaba.fastjson.JSON; import com.aliyun.iot20180120.Client; import com.aliyun.iot20180120.models.*; import com.aliyun.teaopenapi.models.Config; public class JavaDemo { /** * 使用AccessKey ID和AccessKey Secret初始化账号Client * @param accessKeyId * @param accessKeySecret * @return Client * @throws Exception */ public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception { Config config = new Config(); config.setAccessKeyId(accessKeyId); config.setAccessKeySecret(accessKeySecret); // 您的接入域名 config.setEndpoint("iot.cn-shanghai.aliyuncs.com"); return new Client(config); } public static void main(String[] args_) throws Exception { // 您的AccessKey ID和AccessKey Secret Client client = JavaDemo.createClient("LTAI4FyDFmKN************", "WF3onkl8cq3cTyVW8n************")); ListAnalyticsDataRequest request = new ListAnalyticsDataRequest(); // 您的API Path request.setApiPath("/iot-cn-npk1v******/system/query/hist_dev_cnt_stat"); // 您的API所在实例ID request.setIotInstanceId("iot-cn-npk1v******"); //分页参数:页号 request.setPageNum(1); //分页参数:页大小 request.setPageSize(100); List<ListAnalyticsDataRequest.ListAnalyticsDataRequestCondition> conditions = new ArrayList<>(); //您的业务相关的请求参数。Condition的配置说明,请参见下文的相关说明。 ListAnalyticsDataRequest.ListAnalyticsDataRequestCondition condition = new ListAnalyticsDataRequest .ListAnalyticsDataRequestCondition(); condition.setFieldName("__instance_id__"); condition.setOperate("="); condition.setValue("iot-public"); conditions.add(condition); ListAnalyticsDataRequest.ListAnalyticsDataRequestCondition condition1 = new ListAnalyticsDataRequest .ListAnalyticsDataRequestCondition(); condition1.setFieldName("entityId"); condition1.setOperate("="); condition1.setValue("all"); conditions.add(condition1); ListAnalyticsDataRequest.ListAnalyticsDataRequestCondition condition2 = new ListAnalyticsDataRequest .ListAnalyticsDataRequestCondition(); condition2.setFieldName("statDate"); condition2.setOperate("="); condition2.setValue("20210221"); conditions.add(condition2); request.setCondition(conditions); ListAnalyticsDataResponse listAnalyticsDataResponse = client.listAnalyticsData(request); System.out.println(JSON.toJSONString(listAnalyticsDataResponse)); } }
四.查看结果