Code examples for AWS IoT SiteWise using AWS SDKs - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Code examples for AWS IoT SiteWise using AWS SDKs

The following code examples show you how to use AWS IoT SiteWise with an AWS software development kit (SDK).

Basics are code examples that show you how to perform the essential operations within a service.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

More resources

Get started

The following code examples show how to get started using AWS IoT SiteWise.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public class HelloSitewise { private static final Logger logger = LoggerFactory.getLogger(HelloSitewise.class); public static void main(String[] args) { fetchAssetModels(); } /** * Fetches asset models using the provided {@link IoTSiteWiseAsyncClient}. */ public static void fetchAssetModels() { IoTSiteWiseAsyncClient siteWiseAsyncClient = IoTSiteWiseAsyncClient.create(); ListAssetModelsRequest assetModelsRequest = ListAssetModelsRequest.builder() .assetModelTypes(AssetModelType.ASSET_MODEL) .build(); // Asynchronous paginator - process paginated results. ListAssetModelsPublisher listModelsPaginator = siteWiseAsyncClient.listAssetModelsPaginator(assetModelsRequest); CompletableFuture<Void> future = listModelsPaginator.subscribe(response -> { response.assetModelSummaries().forEach(assetSummary -> logger.info("Asset Model Name: {} ", assetSummary.name()) ); }); // Wait for the asynchronous operation to complete future.join(); } }
  • For API details, see ListAssetModels in AWS SDK for Java 2.x API Reference.

Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import boto3 def hello_iot_sitewise(iot_sitewise_client): """ Use the AWS SDK for Python (Boto3) to create an AWS IoT SiteWise client and list the asset models in your account. This example uses the default settings specified in your shared credentials and config files. :param iot_sitewise_client: A Boto3 AWS IoT SiteWise Client object. This object wraps the low-level AWS IoT SiteWise service API. """ print("Hello, AWS IoT SiteWise! Let's list some of your asset models:\n") paginator = iot_sitewise_client.get_paginator("list_asset_models") page_iterator = paginator.paginate(PaginationConfig={"MaxItems": 10}) asset_model_names: [str] = [] for page in page_iterator: for asset_model in page["assetModelSummaries"]: asset_model_names.append(asset_model["name"]) print(f"{len(asset_model_names)} asset model(s) retrieved.") for asset_model_name in asset_model_names: print(f"\t{asset_model_name}") if __name__ == "__main__": hello_iot_sitewise(boto3.client("iotsitewise"))
  • For API details, see ListAssetModels in AWS SDK for Python (Boto3) API Reference.