AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用的代码示 SageMaker 例 AWS SDKs
以下代码示例向您展示了如何将 Amazon SageMaker 与 AWS 软件开发套件 (SDK) 一起使用。
基础知识是向您展示如何在服务中执行基本操作的代码示例。
操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。
场景是向您展示如何通过在一个服务中调用多个函数或与其他 AWS 服务结合来完成特定任务的代码示例。
开始使用
以下代码示例展示了如何开始使用 SageMaker。
- .NET
-
- AWS SDK for .NET
-
using Amazon.SageMaker;
using Amazon.SageMaker.Model;
namespace SageMakerActions;
public static class HelloSageMaker
{
static async Task Main(string[] args)
{
var sageMakerClient = new AmazonSageMakerClient();
Console.WriteLine($"Hello Amazon SageMaker! Let's list some of your notebook instances:");
Console.WriteLine();
// You can use await and any of the async methods to get a response.
// Let's get the first five notebook instances.
var response = await sageMakerClient.ListNotebookInstancesAsync(
new ListNotebookInstancesRequest()
{
MaxResults = 5
});
if (!response.NotebookInstances.Any())
{
Console.WriteLine($"No notebook instances found.");
Console.WriteLine("See https://docs.aws.amazon.com/sagemaker/latest/dg/howitworks-create-ws.html to create one.");
}
foreach (var notebookInstance in response.NotebookInstances)
{
Console.WriteLine($"\tInstance: {notebookInstance.NotebookInstanceName}");
Console.WriteLine($"\tArn: {notebookInstance.NotebookInstanceArn}");
Console.WriteLine($"\tCreation Date: {notebookInstance.CreationTime.ToShortDateString()}");
Console.WriteLine();
}
}
}
- Java
-
- SDK适用于 Java 2.x
-
/**
* Before running this Java V2 code example, set up your development
* environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class HelloSageMaker {
public static void main(String[] args) {
Region region = Region.US_WEST_2;
SageMakerClient sageMakerClient = SageMakerClient.builder()
.region(region)
.build();
listBooks(sageMakerClient);
sageMakerClient.close();
}
public static void listBooks(SageMakerClient sageMakerClient) {
try {
ListNotebookInstancesResponse notebookInstancesResponse = sageMakerClient.listNotebookInstances();
List<NotebookInstanceSummary> items = notebookInstancesResponse.notebookInstances();
for (NotebookInstanceSummary item : items) {
System.out.println("The notebook name is: " + item.notebookInstanceName());
}
} catch (SageMakerException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
}
- JavaScript
-
- SDK对于 JavaScript (v3)
-
import {
SageMakerClient,
ListNotebookInstancesCommand,
} from "@aws-sdk/client-sagemaker";
const client = new SageMakerClient({
region: "us-west-2",
});
export const helloSagemaker = async () => {
const command = new ListNotebookInstancesCommand({ MaxResults: 5 });
const response = await client.send(command);
console.log(
"Hello Amazon SageMaker! Let's list some of your notebook instances:",
);
const instances = response.NotebookInstances || [];
if (instances.length === 0) {
console.log(
"• No notebook instances found. Try creating one in the AWS Management Console or with the CreateNotebookInstanceCommand.",
);
} else {
console.log(
instances
.map(
(i) =>
`• Instance: ${i.NotebookInstanceName}\n Arn:${
i.NotebookInstanceArn
} \n Creation Date: ${i.CreationTime.toISOString()}`,
)
.join("\n"),
);
}
return response;
};
- Kotlin
-
- SDK对于 Kotlin 来说
-
suspend fun listBooks() {
SageMakerClient { region = "us-west-2" }.use { sageMakerClient ->
val response = sageMakerClient.listNotebookInstances(ListNotebookInstancesRequest {})
response.notebookInstances?.forEach { item ->
println("The notebook name is: ${item.notebookInstanceName}")
}
}
}