Dimostra il recupero di un set di dati.
Per saperne di più
Per la documentazione dettagliata che include questo esempio di codice, consulta quanto segue:
Esempio di codice
Go
import (
"context"
"fmt"
"io"
automl "cloud.google.com/go/automl/apiv1"
automlpb "google.golang.org/genproto/googleapis/cloud/automl/v1"
)
// getDataset gets a dataset.
func getDataset(w io.Writer, projectID string, location string, datasetID string) error {
// projectID := "my-project-id"
// location := "us-central1"
// datasetID := "TRL123456789..."
ctx := context.Background()
client, err := automl.NewClient(ctx)
if err != nil {
return fmt.Errorf("NewClient: %v", err)
}
defer client.Close()
req := &automlpb.GetDatasetRequest{
Name: fmt.Sprintf("projects/%s/locations/%s/datasets/%s", projectID, location, datasetID),
}
dataset, err := client.GetDataset(ctx, req)
if err != nil {
return fmt.Errorf("DeleteDataset: %v", err)
}
fmt.Fprintf(w, "Dataset name: %v\n", dataset.GetName())
fmt.Fprintf(w, "Dataset display name: %v\n", dataset.GetDisplayName())
fmt.Fprintf(w, "Dataset create time:\n")
fmt.Fprintf(w, "\tseconds: %v\n", dataset.GetCreateTime().GetSeconds())
fmt.Fprintf(w, "\tnanos: %v\n", dataset.GetCreateTime().GetNanos())
// Vision object detection
if metadata := dataset.GetImageObjectDetectionDatasetMetadata(); metadata != nil {
fmt.Fprintf(w, "Image object detection dataset metadata: %v\n", metadata)
}
return nil
}
Java
import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.Dataset;
import com.google.cloud.automl.v1.DatasetName;
import java.io.IOException;
class GetDataset {
static void getDataset() throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "YOUR_PROJECT_ID";
String datasetId = "YOUR_DATASET_ID";
getDataset(projectId, datasetId);
}
// Get a dataset
static void getDataset(String projectId, String datasetId) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// Get the complete path of the dataset.
DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
Dataset dataset = client.getDataset(datasetFullId);
// Display the dataset information
System.out.format("Dataset name: %s\n", dataset.getName());
// To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are
// required for other methods.
// Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}`
String[] names = dataset.getName().split("/");
String retrievedDatasetId = names[names.length - 1];
System.out.format("Dataset id: %s\n", retrievedDatasetId);
System.out.format("Dataset display name: %s\n", dataset.getDisplayName());
System.out.println("Dataset create time:");
System.out.format("\tseconds: %s\n", dataset.getCreateTime().getSeconds());
System.out.format("\tnanos: %s\n", dataset.getCreateTime().getNanos());
System.out.format(
"Image object detection dataset metadata: %s\n",
dataset.getImageObjectDetectionDatasetMetadata());
}
}
}
Node.js
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const datasetId = 'YOUR_DATASET_ID';
// Imports the Google Cloud AutoML library
const {AutoMlClient} = require('@google-cloud/automl').v1;
// Instantiates a client
const client = new AutoMlClient();
async function getDataset() {
// Construct request
const request = {
name: client.datasetPath(projectId, location, datasetId),
};
const [response] = await client.getDataset(request);
console.log(`Dataset name: ${response.name}`);
console.log(
`Dataset id: ${
response.name.split('/')[response.name.split('/').length - 1]
}`
);
console.log(`Dataset display name: ${response.displayName}`);
console.log('Dataset create time');
console.log(`\tseconds ${response.createTime.seconds}`);
console.log(`\tnanos ${response.createTime.nanos / 1e9}`);
console.log(
`Image object detection dataset metatdata: ${response.imageObjectDetectionDatasetMetatdata}`
);
}
getDataset();
Python
from google.cloud import automl
# TODO(developer): Uncomment and set the following variables
# project_id = "YOUR_PROJECT_ID"
# dataset_id = "YOUR_DATASET_ID"
client = automl.AutoMlClient()
# Get the full path of the dataset
dataset_full_id = client.dataset_path(project_id, "us-central1", dataset_id)
dataset = client.get_dataset(name=dataset_full_id)
# Display the dataset information
print("Dataset name: {}".format(dataset.name))
print("Dataset id: {}".format(dataset.name.split("/")[-1]))
print("Dataset display name: {}".format(dataset.display_name))
print("Dataset create time: {}".format(dataset.create_time))
print(
"Image object detection dataset metadata: {}".format(
dataset.image_object_detection_dataset_metadata
)
)
Passaggi successivi
Per cercare e filtrare esempi di codice per altri prodotti Google Cloud, vedi il browser di esempio Google Cloud.