重新整理結構化和非結構化資料

本頁面說明如何重新整理結構化非結構化資料。

重新整理結構化資料

只要使用的結構定義與資料儲存區中的結構定義相同或向後相容,即可重新整理結構化資料儲存區中的資料。舉例來說,如果只在現有結構定義中新增欄位,就具有回溯相容性。

您可以在 Google Cloud 控制台或使用 API 重新整理結構化資料。

控制台

如要使用 Google Cloud 控制台,從資料存放區的分支重新整理結構化資料,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Gemini Enterprise」頁面。

    Gemini Enterprise

  2. 點按導覽選單中的「Data Stores」(資料儲存庫)

  3. 在「名稱」欄中,按一下要編輯的資料儲存庫。

  4. 在「文件」分頁中,按一下 「匯入資料」

  5. 如要從 Cloud Storage 重新整理:

    1. 在「Select a data source」(選取資料來源) 窗格中,選取「Cloud Storage」
    2. 在「Import data from Cloud Storage」(從 Cloud Storage 匯入資料) 窗格中,按一下「Browse」(瀏覽),選取包含更新資料的值區,然後按一下「Select」(選取)。或者,您也可以直接在「gs://」欄位中輸入 bucket 位置。
    3. 在「資料匯入選項」下方,選取匯入選項。
    4. 按一下「匯入」
  6. 如要重新整理 BigQuery 資料,請按照下列步驟操作:

    1. 在「選取資料來源」窗格中,選取「BigQuery」
    2. 在「從 BigQuery 匯入資料」窗格中,按一下「瀏覽」,選取含有重新整理資料的資料表,然後按一下「選取」。或者,直接在「BigQuery 路徑」欄位中輸入資料表位置。
    3. 在「資料匯入選項」下方,選取匯入選項。
    4. 按一下「匯入」

REST

使用 documents.import 方法重新整理資料,並指定適當的 reconciliationMode 值。

如要使用指令列從 BigQuery 或 Cloud Storage 重新整理結構化資料,請按照下列步驟操作:

  1. 找出資料儲存庫 ID。如果已有資料商店 ID,請跳到下一個步驟。

    1. 前往 Google Cloud 控制台的「Gemini Enterprise」頁面,然後點按導覽選單中的「Data Stores」(資料儲存庫)

      前往「Data Stores」頁面

    2. 點按資料儲存庫的名稱。

    3. 在資料儲存庫的「資料」頁面中,取得資料儲存庫 ID。

  2. 如要從 BigQuery 匯入結構化資料,請呼叫下列方法。您可以從 BigQuery 或 Cloud Storage 匯入。如要從 Cloud Storage 匯入,請跳至下一個步驟。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1beta/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/branches/0/documents:import" \
    -d '{
      "bigquerySource": {
        "projectId": "PROJECT_ID",
        "datasetId":"DATASET_ID",
        "tableId": "TABLE_ID",
        "dataSchema": "DATA_SCHEMA_BQ",
      },
      "reconciliationMode": "RECONCILIATION_MODE",
      "autoGenerateIds": AUTO_GENERATE_IDS,
      "idField": "ID_FIELD",
      "errorConfig": {
        "gcsPrefix": "ERROR_DIRECTORY"
      }
    }'
    

    更改下列內容:

    • PROJECT_ID:專案 ID。
    • DATA_STORE_ID:資料儲存庫的 ID。
    • DATASET_ID:BigQuery 資料集的名稱。
    • TABLE_ID:BigQuery 資料表的名稱。
    • DATA_SCHEMA_BQ:選用欄位,用於指定從 BigQuery 來源剖析資料時要使用的結構定義。可能具有下列值:
      • document:預設值。您使用的 BigQuery 資料表必須符合下列預設的 BigQuery 結構定義。您可以自行定義每份文件的 ID,同時將整個資料包裝在 json_data 字串中。
      • custom:系統接受任何 BigQuery 資料表結構定義,且 Gemini Enterprise 會自動為匯入的每份文件產生 ID。
    • ERROR_DIRECTORY:選填欄位,可指定 Cloud Storage 目錄,存放匯入作業的錯誤資訊,例如 gs://<your-gcs-bucket>/directory/import_errors。Google 建議將這個欄位留空,讓 Gemini Enterprise 自動建立暫時目錄。
    • RECONCILIATION_MODE:選用欄位,用於指定如何將匯入的文件與目標資料儲存庫中的現有文件進行比對。可能具有下列值:
      • INCREMENTAL:預設值。從 BigQuery 增量重新整理資料至資料儲存庫。這會執行 upsert 作業,新增文件並以 ID 相同的更新文件取代現有文件。
      • FULL:導致資料存放區中的文件完全重新基準化。 因此,系統會將新文件和更新的文件新增至資料存放區,並從資料存放區中移除不在 BigQuery 中的文件。如果您想自動刪除不再需要的檔案,建議使用FULL模式。
    • AUTO_GENERATE_IDS:選填欄位,可指定是否要自動產生文件 ID。如果設為 true,系統會根據酬載的雜湊值產生文件 ID。請注意,多次匯入時,產生的文件 ID 可能不一致。如果您在多次匯入時自動產生 ID,Google 強烈建議將 reconciliationMode 設為 FULL,以維持文件 ID 的一致性。

      只有在 bigquerySource.dataSchema 設為 custom 時,才指定 autoGenerateIds。否則,系統會傳回 INVALID_ARGUMENT 錯誤。如未指定 autoGenerateIds 或將其設為 false,則必須指定 idField。否則文件無法匯入。

    • ID_FIELD:選用欄位,用來指定哪些欄位是文件 ID。如果是 BigQuery 來源檔案,idField 表示 BigQuery 資料表中包含文件 ID 的資料欄名稱。

      只有在滿足下列兩個條件時,才能指定 idField,否則系統會傳回 INVALID_ARGUMENT 錯誤:

      • bigquerySource.dataSchema」已設為「custom
      • auto_generate_ids 設為 false 或未指定。

      此外,BigQuery 資料欄名稱的值必須為字串類型,長度介於 1 到 63 個字元之間,且符合 RFC-1034 規範。否則文件無法匯入。

    預設的 BigQuery 結構定義如下。設定 dataSchemadocument 時,BigQuery 資料表必須符合這個結構定義。

    [
     {
       "name": "id",
       "mode": "REQUIRED",
       "type": "STRING",
       "fields": []
     },
     {
       "name": "jsonData",
       "mode": "NULLABLE",
       "type": "STRING",
       "fields": []
     }
    ]
    
  3. 如要從 Cloud Storage 匯入結構化資料,請呼叫下列方法。您可以從 BigQuery 或 Cloud Storage 匯入資料。如要從 BigQuery 匯入,請返回上一步。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1beta/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/branches/0/documents:import" \
    -d '{
      "gcsSource": {
        "inputUris": ["GCS_PATHS"],
        "dataSchema": "DATA_SCHEMA_GCS",
      },
      "reconciliationMode": "RECONCILIATION_MODE",
      "idField": "ID_FIELD",
      "errorConfig": {
        "gcsPrefix": "ERROR_DIRECTORY"
      }
    }'
    

    更改下列內容:

    • PROJECT_ID:專案 ID。
    • DATA_STORE_ID:資料儲存庫的 ID。
    • GCS_PATHS:以半形逗號分隔的 URI 清單,指向要匯入資料的 Cloud Storage 位置。每個 URI 最多可包含 2,000 個半形字元。URI 可以比對儲存空間物件的完整路徑,也可以比對一或多個物件的模式。舉例來說,gs://bucket/directory/*.json 是有效路徑。
    • DATA_SCHEMA_GCS:選用欄位,用於指定從 BigQuery 來源剖析資料時要使用的結構定義。可能具有下列值:
      • document:預設值。您使用的 BigQuery 資料表必須符合下列預設的 BigQuery 結構定義。您可以自行定義每份文件的 ID,同時將整個資料包裝在 json_data 字串中。
      • custom:系統接受任何 BigQuery 資料表結構定義,且 Gemini Enterprise 會自動為匯入的每份文件產生 ID。
    • ERROR_DIRECTORY:選填欄位,可指定 Cloud Storage 目錄,存放匯入作業的錯誤資訊,例如 gs://<your-gcs-bucket>/directory/import_errors。Google 建議將這個欄位留空,讓 Gemini Enterprise 自動建立暫時目錄。
    • RECONCILIATION_MODE:選用欄位,用於指定如何將匯入的文件與目標資料儲存庫中的現有文件進行比對。可能具有下列值:
      • INCREMENTAL:預設值。從 BigQuery 增量重新整理資料至資料儲存庫。這會執行 upsert 作業,新增文件並以 ID 相同的更新文件取代現有文件。
      • FULL:導致資料存放區中的文件完全重新基準化。 因此,系統會將新文件和更新的文件新增至資料存放區,並從資料存放區中移除不在 BigQuery 中的文件。如果您想自動刪除不再需要的檔案,可以選擇 FULL 模式。

Python

在試用這個範例之前,請先按照「使用用戶端程式庫的 Gemini Enterprise 快速入門導覽課程」中的 Python 設定操作說明進行操作。詳情請參閱 Gemini Enterprise Python API 參考文件

如要向 Gemini Enterprise 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。


from google.api_core.client_options import ClientOptions
from google.cloud import discoveryengine

# TODO(developer): Uncomment these variables before running the sample.
# project_id = "YOUR_PROJECT_ID"
# location = "YOUR_LOCATION" # Values: "global"
# data_store_id = "YOUR_DATA_STORE_ID"
# bigquery_dataset = "YOUR_BIGQUERY_DATASET"
# bigquery_table = "YOUR_BIGQUERY_TABLE"

#  For more information, refer to:
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
client_options = (
    ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
    if location != "global"
    else None
)

# Create a client
client = discoveryengine.DocumentServiceClient(client_options=client_options)

# The full resource name of the search engine branch.
# e.g. projects/{project}/locations/{location}/dataStores/{data_store_id}/branches/{branch}
parent = client.branch_path(
    project=project_id,
    location=location,
    data_store=data_store_id,
    branch="default_branch",
)

request = discoveryengine.ImportDocumentsRequest(
    parent=parent,
    bigquery_source=discoveryengine.BigQuerySource(
        project_id=project_id,
        dataset_id=bigquery_dataset,
        table_id=bigquery_table,
        data_schema="custom",
    ),
    # Options: `FULL`, `INCREMENTAL`
    reconciliation_mode=discoveryengine.ImportDocumentsRequest.ReconciliationMode.INCREMENTAL,
)

# Make the request
operation = client.import_documents(request=request)

print(f"Waiting for operation to complete: {operation.operation.name}")
response = operation.result()

# After the operation is complete,
# get information from operation metadata
metadata = discoveryengine.ImportDocumentsMetadata(operation.metadata)

# Handle the response
print(response)
print(metadata)

重新整理非結構化資料

您可以在 Google Cloud 控制台中或使用 API 重新整理非結構化資料。

控制台

如要使用 Google Cloud 控制台,從資料存放區的分支機構重新整理非結構化資料,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Gemini Enterprise」頁面。

    Gemini Enterprise

  2. 點按導覽選單中的「Data Stores」(資料儲存庫)

  3. 在「名稱」欄中,按一下要編輯的資料儲存庫。

  4. 在「文件」分頁中,按一下 「匯入資料」

  5. 如要從 Cloud Storage 值區擷取資料 (含或不含中繼資料):

    1. 在「Select a data source」(選取資料來源) 窗格中,選取「Cloud Storage」
    2. 在「Import data from Cloud Storage」(從 Cloud Storage 匯入資料) 窗格中,按一下「Browse」(瀏覽),選取包含更新資料的值區,然後按一下「Select」(選取)。或者,也可以直接在 gs:// 欄位中輸入 bucket 位置。
    3. 在「資料匯入選項」下方,選取匯入選項。
    4. 按一下「匯入」
  6. 如要從 BigQuery 擷取資料,請按照下列步驟操作:

    1. 在「選取資料來源」窗格中,選取「BigQuery」
    2. 在「從 BigQuery 匯入資料」窗格中,按一下「瀏覽」,選取包含重新整理資料的資料表,然後按一下「選取」。或者,直接在「BigQuery 路徑」欄位中輸入資料表位置。
    3. 在「資料匯入選項」下方,選取匯入選項。
    4. 按一下「匯入」

REST

如要使用 API 重新整理非結構化資料,請使用 documents.import 方法重新匯入資料,並指定適當的 reconciliationMode 值。如要進一步瞭解如何匯入非結構化資料,請參閱非結構化資料

Python

在試用這個範例之前,請先按照「使用用戶端程式庫的 Gemini Enterprise 快速入門導覽課程」中的 Python 設定操作說明進行操作。詳情請參閱 Gemini Enterprise Python API 參考文件

如要向 Gemini Enterprise 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

from google.api_core.client_options import ClientOptions
from google.cloud import discoveryengine

# TODO(developer): Uncomment these variables before running the sample.
# project_id = "YOUR_PROJECT_ID"
# location = "YOUR_LOCATION" # Values: "global"
# data_store_id = "YOUR_DATA_STORE_ID"

# Examples:
# - Unstructured documents
#   - `gs://bucket/directory/file.pdf`
#   - `gs://bucket/directory/*.pdf`
# - Unstructured documents with JSONL Metadata
#   - `gs://bucket/directory/file.json`
# - Unstructured documents with CSV Metadata
#   - `gs://bucket/directory/file.csv`
# gcs_uri = "YOUR_GCS_PATH"

#  For more information, refer to:
# https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
client_options = (
    ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
    if location != "global"
    else None
)

# Create a client
client = discoveryengine.DocumentServiceClient(client_options=client_options)

# The full resource name of the search engine branch.
# e.g. projects/{project}/locations/{location}/dataStores/{data_store_id}/branches/{branch}
parent = client.branch_path(
    project=project_id,
    location=location,
    data_store=data_store_id,
    branch="default_branch",
)

request = discoveryengine.ImportDocumentsRequest(
    parent=parent,
    gcs_source=discoveryengine.GcsSource(
        # Multiple URIs are supported
        input_uris=[gcs_uri],
        # Options:
        # - `content` - Unstructured documents (PDF, HTML, DOC, TXT, PPTX)
        # - `custom` - Unstructured documents with custom JSONL metadata
        # - `document` - Structured documents in the discoveryengine.Document format.
        # - `csv` - Unstructured documents with CSV metadata
        data_schema="content",
    ),
    # Options: `FULL`, `INCREMENTAL`
    reconciliation_mode=discoveryengine.ImportDocumentsRequest.ReconciliationMode.INCREMENTAL,
)

# Make the request
operation = client.import_documents(request=request)

print(f"Waiting for operation to complete: {operation.operation.name}")
response = operation.result()

# After the operation is complete,
# get information from operation metadata
metadata = discoveryengine.ImportDocumentsMetadata(operation.metadata)

# Handle the response
print(response)
print(metadata)