Skip to content

Commit 49ea4a7

Browse files
samples(discoveryengine): Add AlloyDB Import Sample (GoogleCloudPlatform#12032)
--------- Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com>
1 parent 1fd5392 commit 49ea4a7

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

discoveryengine/documents_sample_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,21 @@ def test_import_documents_bigtable():
126126
assert "operations/import-documents" in operation_name
127127

128128

129+
def test_import_documents_alloy_db():
130+
operation_name = import_documents_sample.import_documents_alloy_db_sample(
131+
project_id=project_id,
132+
location=location,
133+
data_store_id=data_store_id,
134+
alloy_db_project_id=project_id,
135+
alloy_db_location_id="us-central1",
136+
alloy_db_cluster_id="vais-tests",
137+
alloy_db_database_id="postgres",
138+
alloy_db_table_id="public.vais",
139+
)
140+
141+
assert "operations/import-documents" in operation_name
142+
143+
129144
@pytest.mark.skip(reason="Permissions")
130145
def test_import_documents_healthcare_fhir_sample():
131146
location = "us"

discoveryengine/import_documents_sample.py

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,81 @@ def import_documents_bigtable_sample(
455455
return operation.operation.name
456456

457457

458+
def import_documents_alloy_db_sample(
459+
project_id: str,
460+
location: str,
461+
data_store_id: str,
462+
alloy_db_project_id: str,
463+
alloy_db_location_id: str,
464+
alloy_db_cluster_id: str,
465+
alloy_db_database_id: str,
466+
alloy_db_table_id: str,
467+
) -> str:
468+
# [START genappbuilder_import_documents_alloy_db]
469+
from google.api_core.client_options import ClientOptions
470+
from google.cloud import discoveryengine_v1 as discoveryengine
471+
472+
# TODO(developer): Uncomment these variables before running the sample.
473+
# project_id = "YOUR_PROJECT_ID"
474+
# location = "YOUR_LOCATION" # Values: "global"
475+
# data_store_id = "YOUR_DATA_STORE_ID"
476+
# alloy_db_project_id = "YOUR_ALLOY_DB_PROJECT_ID"
477+
# alloy_db_location_id = "YOUR_ALLOY_DB_LOCATION_ID"
478+
# alloy_db_cluster_id = "YOUR_ALLOY_DB_CLUSTER_ID"
479+
# alloy_db_database_id = "YOUR_ALLOY_DB_DATABASE_ID"
480+
# alloy_db_table_id = "YOUR_ALLOY_DB_TABLE_ID"
481+
482+
# For more information, refer to:
483+
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
484+
client_options = (
485+
ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
486+
if location != "global"
487+
else None
488+
)
489+
490+
# Create a client
491+
client = discoveryengine.DocumentServiceClient(client_options=client_options)
492+
493+
# The full resource name of the search engine branch.
494+
# e.g. projects/{project}/locations/{location}/dataStores/{data_store_id}/branches/{branch}
495+
parent = client.branch_path(
496+
project=project_id,
497+
location=location,
498+
data_store=data_store_id,
499+
branch="default_branch",
500+
)
501+
502+
request = discoveryengine.ImportDocumentsRequest(
503+
parent=parent,
504+
alloy_db_source=discoveryengine.AlloyDbSource(
505+
project_id=alloy_db_project_id,
506+
location_id=alloy_db_location_id,
507+
cluster_id=alloy_db_cluster_id,
508+
database_id=alloy_db_database_id,
509+
table_id=alloy_db_table_id,
510+
),
511+
# Options: `FULL`, `INCREMENTAL`
512+
reconciliation_mode=discoveryengine.ImportDocumentsRequest.ReconciliationMode.INCREMENTAL,
513+
)
514+
515+
# Make the request
516+
operation = client.import_documents(request=request)
517+
518+
print(f"Waiting for operation to complete: {operation.operation.name}")
519+
response = operation.result()
520+
521+
# Once the operation is complete,
522+
# get information from operation metadata
523+
metadata = discoveryengine.ImportDocumentsMetadata(operation.metadata)
524+
525+
# Handle the response
526+
print(response)
527+
print(metadata)
528+
# [END genappbuilder_import_documents_alloy_db]
529+
530+
return operation.operation.name
531+
532+
458533
def import_documents_healthcare_fhir_sample(
459534
project_id: str,
460535
location: str,
@@ -500,7 +575,12 @@ def import_documents_healthcare_fhir_sample(
500575
request = discoveryengine.ImportDocumentsRequest(
501576
parent=parent,
502577
fhir_store_source=discoveryengine.FhirStoreSource(
503-
fhir_store=f"projects/{healthcare_project_id}/locations/{healthcare_location}/datasets/{healthcare_dataset_id}/fhirStores/{healthcare_fihr_store_id}",
578+
fhir_store=client.fhir_store_path(
579+
healthcare_project_id,
580+
healthcare_location,
581+
healthcare_dataset_id,
582+
healthcare_fihr_store_id,
583+
),
504584
),
505585
# Options: `FULL`, `INCREMENTAL`
506586
reconciliation_mode=discoveryengine.ImportDocumentsRequest.ReconciliationMode.INCREMENTAL,

discoveryengine/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
google-cloud-discoveryengine==0.11.13
1+
google-cloud-discoveryengine==0.11.14
22
google-api-core==2.19.0

0 commit comments

Comments
 (0)