Search entries

Use search to retrieve entries matching the query.

Code sample

Java

Before trying this sample, follow the Java setup instructions in the Dataplex quickstart using client libraries. For more information, see the Dataplex Java API reference documentation.

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

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

Before trying this sample, follow the Python setup instructions in the Dataplex quickstart using client libraries. For more information, see the Dataplex Python API reference documentation.

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

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

What's next

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