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();
}
}
添加回答
举报