

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 SAP OData 狀態管理指令碼
<a name="sap-odata-state-management-script"></a>

若要在 AWS Glue 任務中使用 SAP OData 狀態管理指令碼，請遵循下列步驟：
+ 下載狀態管理指令碼：公有 Amazon S3 儲存貯體中的 `s3://aws-blogs-artifacts-public/artifacts/BDB-4789/sap_odata_state_management.zip `。
+ 將指令碼上傳至您的 AWS Glue 任務具有存取許可的 Amazon S3 儲存貯體。
+ 參考 AWS Glue 任務中的指令碼：建立或更新 AWS Glue 任務時，請傳遞參考 Amazon S3 儲存貯體中指令碼路徑`'--extra-py-files'`的選項。例如：`--extra-py-files s3://your-bucket/path/to/sap_odata_state_management.py`
+ 在 AWS Glue 任務指令碼中匯入和使用狀態管理程式庫。

## Delta-token 型增量傳輸範例
<a name="sap-odata-delta-token-incremental-transfer"></a>

以下是如何使用狀態管理指令碼進行 Delta-token 型增量傳輸的範例：

```
from sap_odata_state_management import StateManagerFactory, StateManagerType, StateType

# Initialize the state manager
state_manager = StateManagerFactory.create_manager(
    manager_type=StateManagerType.JOB_TAG,
    state_type=StateType.DELTA_TOKEN,
    options={
        "job_name": args['JOB_NAME'],
        "logger": logger
    }
)

# Get connector options (including delta token if available)
key = "SAPODataNode"
connector_options = state_manager.get_connector_options(key)

# Use the connector options in your Glue job
df = glueContext.create_dynamic_frame.from_options(
    connection_type="SAPOData",
    connection_options={
        "connectionName": "connectionName",
        "ENTITY_NAME": "entityName",
        "ENABLE_CDC": "true",
        **connector_options
    }
)

# Process your data here...

# Update the state after processing
state_manager.update_state(key, sapodata_df.toDF())
```

## 時間戳記型增量傳輸範例
<a name="sap-odata-timestamp-incremental-transfer"></a>

以下是如何使用狀態管理指令碼進行 Delta-token 型增量傳輸的範例：

```
from sap_odata_state_management import StateManagerFactory, StateManagerType, StateType

# Initialize the state manager
state_manager = StateManagerFactory.create_manager(
    manager_type=StateManagerType.JOB_TAG,
    state_type=StateType.DELTA_TOKEN,
    options={
        "job_name": args['JOB_NAME'],
        "logger": logger
    }
)

# Get connector options (including delta token if available)
key = "SAPODataNode"
connector_options = state_manager.get_connector_options(key)

# Use the connector options in your Glue job
df = glueContext.create_dynamic_frame.from_options(
    connection_type="SAPOData",
    connection_options={
        "connectionName": "connectionName",
        "ENTITY_NAME": "entityName",
        "ENABLE_CDC": "true",
        **connector_options
    }
)

# Process your data here...

# Update the state after processing
state_manager.update_state(key, sapodata_df.toDF())
```

在這兩個範例中，狀態管理指令碼會處理在任務執行之間存放狀態 (delta token 或時間戳記) 的複雜性。其會在取得連接器選項時自動擷取最後的已知狀態，並在處理後更新狀態，確保每個任務只會處理最新的或變更的資料。