Voci di ricerca

Utilizza la ricerca per recuperare le voci corrispondenti alla query.

Esempio di codice

Java

Prima di provare questo esempio, segui le istruzioni di configurazione di Java riportate nella guida rapida di Dataplex che utilizza le librerie client. Per ulteriori informazioni, consulta la documentazione di riferimento dell'API Dataplex Java.

Per autenticarti a Dataplex, configura le credenziali predefinite dell'applicazione. Per ulteriori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.

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

Prima di provare questo esempio, segui le istruzioni di configurazione di Python riportate nella guida rapida di Dataplex che utilizza le librerie client. Per ulteriori informazioni, consulta la documentazione di riferimento dell'API Dataplex Python.

Per autenticarti a Dataplex, configura le credenziali predefinite dell'applicazione. Per ulteriori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.

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}")

Passaggi successivi

Per cercare e filtrare i sample di codice per altri Google Cloud prodotti, consulta il Google Cloud browser di sample.