Hay más ejemplos de AWS SDK disponibles en el GitHub repositorio de ejemplos de AWS Doc SDK.
Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.
Ejemplos de código para SageMaker usar los AWS SDK
Los siguientes ejemplos de código muestran cómo utilizar Amazon SageMaker con un kit de desarrollo de AWS software (SDK).
Las acciones son extractos de código de programas más grandes y deben ejecutarse en contexto. Mientras las acciones muestran cómo llamar a las funciones de servicio individuales, es posible ver las acciones en contexto en los escenarios relacionados y en los ejemplos entre servicios.
Los escenarios son ejemplos de código que muestran cómo llevar a cabo una tarea específica llamando a varias funciones dentro del mismo servicio.
Introducción
Los siguientes ejemplos de código muestran cómo empezar a usarlo 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 para 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 para 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 para 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}")
}
}
}