Rechercher des entrées

Utilisez la recherche pour récupérer les entrées correspondant à la requête.

Exemple de code

Java

Avant d'essayer cet exemple, suivez les instructions de configuration pour Java du guide de démarrage rapide de Dataplex avec les bibliothèques clientes. Pour en savoir plus, consultez la documentation de référence de l'API Dataplex Java.

Pour vous authentifier auprès de Dataplex, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

import com.google.cloud.dataplex.v1.CatalogServiceClient;
import com.google.cloud.dataplex.v1.Entry;
import com.google.cloud.dataplex.v1.SearchEntriesRequest;
import com.google.cloud.dataplex.v1.SearchEntriesResult;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

public class SearchEntries {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "MY_PROJECT_ID";
    // How to write query for search: https://cloud.google.com/dataplex/docs/search-syntax
    String query = "MY_QUERY";

    List<Entry> entries = searchEntries(projectId, query);
    entries.forEach(entry -> System.out.println("Entry name found in search: " + entry.getName()));
  }

  // Method to search Entries located in projectId and matching query
  public static List<Entry> searchEntries(String projectId, String query) 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.
    try (CatalogServiceClient client = CatalogServiceClient.create()) {
      SearchEntriesRequest searchEntriesRequest =
          SearchEntriesRequest.newBuilder()
              .setPageSize(100)
              // Required field, will by default limit search scope to organization under which the
              // project is located
              .setName(String.format("projects/%s/locations/global", projectId))
              // Optional field, will further limit search scope only to specified project
              .setScope(String.format("projects/%s", projectId))
              .setQuery(query)
              .build();

      CatalogServiceClient.SearchEntriesPagedResponse searchEntriesResponse =
          client.searchEntries(searchEntriesRequest);
      return searchEntriesResponse.getPage().getResponse().getResultsList().stream()
          // Extract Entries nested inside search results
          .map(SearchEntriesResult::getDataplexEntry)
          .collect(Collectors.toList());
    }
  }
}

Python

Avant d'essayer cet exemple, suivez les instructions de configuration pour Python du guide de démarrage rapide de Dataplex avec les bibliothèques clientes. Pour en savoir plus, consultez la documentation de référence de l'API Dataplex Python.

Pour vous authentifier auprès de Dataplex, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

from typing import List

from google.cloud import dataplex_v1
from google.cloud.dataplex_v1 import Entry


def search_entries(project_id: str, query: str) -> List[Entry]:
    """Method to search Entries located in project_id and matching query"""

    # Initialize client that will be used to send requests across threads. This
    # client only needs to be created once, and can be reused for multiple requests.
    # After completing all of your requests, call the "__exit__()" method to safely
    # clean up any remaining background resources. Alternatively, use the client as
    # a context manager.
    with dataplex_v1.CatalogServiceClient() as client:
        search_entries_request = dataplex_v1.SearchEntriesRequest(
            page_size=100,
            # Required field, will by default limit search scope to organization under which the project is located
            name=f"projects/{project_id}/locations/global",
            # Optional field, will further limit search scope only to specified project
            scope=f"projects/{project_id}",
            query=query,
        )

        search_entries_response = client.search_entries(search_entries_request)
        return [
            result.dataplex_entry
            for result in search_entries_response._response.results
        ]


if __name__ == "__main__":
    # TODO(developer): Replace these variables before running the sample.
    project_id = "MY_PROJECT_ID"
    # How to write query for search: https://cloud.google.com/dataplex/docs/search-syntax
    query = "MY_QUERY"

    entries = search_entries(project_id, query)
    for entry in entries:
        print(f"Entry name found in search: {entry.name}")

Étape suivante

Pour rechercher et filtrer des exemples de code pour d'autres Google Cloud produits, consultez l'explorateur d'exemples Google Cloud .