亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 java 在資源組中創建 IoTHub 資源?

如何使用 java 在資源組中創建 IoTHub 資源?

開滿天機 2023-06-04 17:05:34
我能夠使用 java 庫在 azure 中創建一個資源組,但不知道如何在該組中創建 IoTHub 資源。我曾嘗試使用 genericResources,但它拋出了缺少 Sku 信息的異常。不幸的是,沒有方法可以在 genericResources 創建中設置 SKU 信息。錯誤:com.microsoft.azure.CloudException:缺少 Sku 信息。
查看完整描述

1 回答

?
湖上湖

TA貢獻2003條經驗 獲得超2個贊

目前,用于 Java 的 Azure 管理庫并未涵蓋 Azure 門戶中的所有服務。不幸的是,我們現在不能用它來管理 IOT hub。

我做了一些測試,發現了 2 個可選的解決方法:

  1. 使用 Azure REST API創建 IOT 中心資源

  2. 使用 Azure Java SDK 通過模板部署 IOT 中心資源:

模板:

{

? ? "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",

? ? "contentVersion": "1.0.0.0",

? ? "parameters": {

? ? ? ? "name": {

? ? ? ? ? ? "type": "string"

? ? ? ? },

? ? ? ? "location": {

? ? ? ? ? ? "type": "string"

? ? ? ? },

? ? ? ? "sku_name": {

? ? ? ? ? ? "type": "string"

? ? ? ? },

? ? ? ? "sku_units": {

? ? ? ? ? ? "type": "string"

? ? ? ? },

? ? ? ? "d2c_partitions": {

? ? ? ? ? ? "type": "string"

? ? ? ? },

? ? ? ? "features": {

? ? ? ? ? ? "type": "string"

? ? ? ? }

? ? },

? ? "resources": [

? ? ? ? {

? ? ? ? ? ? "apiVersion": "2019-07-01-preview",

? ? ? ? ? ? "type": "Microsoft.Devices/IotHubs",

? ? ? ? ? ? "name": "[parameters('name')]",

? ? ? ? ? ? "location": "[parameters('location')]",

? ? ? ? ? ? "properties": {

? ? ? ? ? ? ? ? "eventHubEndpoints": {

? ? ? ? ? ? ? ? ? ? "events": {

? ? ? ? ? ? ? ? ? ? ? ? "retentionTimeInDays": 1,

? ? ? ? ? ? ? ? ? ? ? ? "partitionCount": "[parameters('d2c_partitions')]"

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? "features": "[parameters('features')]"

? ? ? ? ? ? },

? ? ? ? ? ? "sku": {

? ? ? ? ? ? ? ? "name": "[parameters('sku_name')]",

? ? ? ? ? ? ? ? "capacity": "[parameters('sku_units')]"

? ? ? ? ? ? }

? ? ? ? }

? ? ]

}

Java代碼:


import com.microsoft.azure.management.Azure;

import com.microsoft.azure.management.resources.DeploymentMode;

import com.microsoft.azure.management.resources.fluentcore.arm.Region;

import org.apache.commons.io.IOUtils;

import org.json.JSONObject;


public static void DeployTest(Azure azure) {

? ? try(InputStream templatein = new BufferedInputStream(new FileInputStream( "D:\\iottemplate\\template.json"));

? ? ? ? StringWriter templateWriter = new StringWriter();

? ? ){

? ? ? ? // Read the template.json file

? ? ? ? IOUtils.copy(templatein, templateWriter);


? ? ? ? // Convert template to JSON object

? ? ? ? JSONObject templateNode = new JSONObject(templateWriter.toString());


? ? ? ? // Add default value for parameters

? ? ? ? JSONObject parameterValue = templateNode.optJSONObject("parameters");

? ? ? ? parameterValue.optJSONObject("sku_name").put("defaultValue","B1");

? ? ? ? parameterValue.optJSONObject("sku_units").put("defaultValue","1");

? ? ? ? parameterValue.optJSONObject("d2c_partitions").put("defaultValue","4");

? ? ? ? parameterValue.optJSONObject("location").put("defaultValue","southeastasia");

? ? ? ? parameterValue.optJSONObject("features").put("defaultValue","None");

? ? ? ? parameterValue.optJSONObject("name").put("defaultValue","jackiottest567");


? ? ? ? // Deploy

? ? ? ? azure.deployments().define("CreateIOTHub")

? ? ? ? ? ? ? ? .withNewResourceGroup("JackIotTest1", Region.ASIA_SOUTHEAST)

? ? ? ? ? ? ? ? .withTemplate(templateNode.toString())

? ? ? ? ? ? ? ? .withParameters("{}")

? ? ? ? ? ? ? ? .withMode(DeploymentMode.INCREMENTAL)

? ? ? ? ? ? ? ? .create();


? ? } catch (FileNotFoundException e) {

? ? ? ? e.printStackTrace();

? ? } catch (IOException e) {

? ? ? ? e.printStackTrace();

? ? }

}?


查看完整回答
反對 回復 2023-06-04
  • 1 回答
  • 0 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號