1 回答

TA貢獻2003條經驗 獲得超2個贊
目前,用于 Java 的 Azure 管理庫并未涵蓋 Azure 門戶中的所有服務。不幸的是,我們現在不能用它來管理 IOT hub。
我做了一些測試,發現了 2 個可選的解決方法:
使用 Azure REST API創建 IOT 中心資源
使用 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();
? ? }
}?
添加回答
舉報