List datasets

Demonstrate listing datasets.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

Java

To authenticate to AutoML Tables, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import com.google.cloud.automl.v1beta1.AutoMlClient;
import com.google.cloud.automl.v1beta1.Dataset;
import com.google.cloud.automl.v1beta1.ListDatasetsRequest;
import com.google.cloud.automl.v1beta1.LocationName;
import java.io.IOException;

class ListDatasets {

  static void listDatasets() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "YOUR_PROJECT_ID";
    listDatasets(projectId);
  }

  // List the datasets
  static void listDatasets(String projectId) 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()) {
      // A resource that represents Google Cloud Platform location.
      LocationName projectLocation = LocationName.of(projectId, "us-central1");
      ListDatasetsRequest request =
          ListDatasetsRequest.newBuilder().setParent(projectLocation.toString()).build();

      // List all the datasets available in the region by applying filter.
      System.out.println("List of datasets:");
      for (Dataset dataset : client.listDatasets(request).iterateAll()) {
        // Display the dataset information
        System.out.format("%nDataset 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("Tables dataset metadata: %s%n", dataset.getTablesDatasetMetadata());

      }
    }
  }
}

Node.js

To authenticate to AutoML Tables, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

const automl = require('@google-cloud/automl');
const util = require('util');
const client = new automl.v1beta1.AutoMlClient();

/**
 * Demonstrates using the AutoML client to list all datasets.
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const projectId = '[PROJECT_ID]' e.g., "my-gcloud-project";
// const computeRegion = '[REGION_NAME]' e.g., "us-central1";
// const filter = '[FILTER_EXPRESSIONS]' e.g., "tablesDatasetMetadata:*";

// A resource that represents Google Cloud Platform location.
const projectLocation = client.locationPath(projectId, computeRegion);

// List all the datasets available in the region by applying filter.
client
  .listDatasets({parent: projectLocation, filter: filter})
  .then(responses => {
    const dataset = responses[0];

    // Display the dataset information.
    console.log('List of datasets:');
    for (let i = 0; i < dataset.length; i++) {
      const tablesDatasetMetadata = dataset[i].tablesDatasetMetadata;

      console.log(`Dataset name: ${dataset[i].name}`);
      console.log(`Dataset Id: ${dataset[i].name.split('/').pop(-1)}`);
      console.log(`Dataset display name: ${dataset[i].displayName}`);
      console.log(`Dataset example count: ${dataset[i].exampleCount}`);
      console.log('Tables dataset metadata:');
      console.log(
        `\tTarget column correlations: ${util.inspect(
          tablesDatasetMetadata.targetColumnCorrelations,
          false,
          null
        )}`
      );
      console.log(
        `\tPrimary table spec Id: ${tablesDatasetMetadata.primaryTableSpecId}`
      );
      console.log(
        `\tTarget column spec Id: ${tablesDatasetMetadata.targetColumnSpecId}`
      );
      console.log(
        `\tWeight column spec Id: ${tablesDatasetMetadata.weightColumnSpecId}`
      );
      console.log(
        `\tMl use column spec Id: ${tablesDatasetMetadata.mlUseColumnSpecId}`
      );
    }
  })
  .catch(err => {
    console.error(err);
  });

Python

To authenticate to AutoML Tables, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

# TODO(developer): Uncomment and set the following variables
# project_id = 'PROJECT_ID_HERE'
# compute_region = 'COMPUTE_REGION_HERE'
# filter = 'filter expression here'

from google.cloud import automl_v1beta1 as automl

client = automl.TablesClient(project=project_id, region=compute_region)

# List all the datasets available in the region by applying filter.
response = client.list_datasets(filter=filter)

print("List of datasets:")
for dataset in response:
    # Display the dataset information.
    print(f"Dataset name: {dataset.name}")
    print("Dataset id: {}".format(dataset.name.split("/")[-1]))
    print(f"Dataset display name: {dataset.display_name}")
    metadata = dataset.tables_dataset_metadata
    print(
        "Dataset primary table spec id: {}".format(metadata.primary_table_spec_id)
    )
    print(
        "Dataset target column spec id: {}".format(metadata.target_column_spec_id)
    )
    print(
        "Dataset target column spec id: {}".format(metadata.target_column_spec_id)
    )
    print(
        "Dataset weight column spec id: {}".format(metadata.weight_column_spec_id)
    )
    print(
        "Dataset ml use column spec id: {}".format(metadata.ml_use_column_spec_id)
    )
    print(f"Dataset example count: {dataset.example_count}")
    print(f"Dataset create time: {dataset.create_time}")
    print("\n")

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.