为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 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 关注
  • 93 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信