

# 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 작업 스크립트에서 상태 관리 라이브러리를 가져오고 사용하세요.

## 델타 토큰 기반 증분 전송 예제
<a name="sap-odata-delta-token-incremental-transfer"></a>

다음은 델타 토큰 기반 증분 전송에 상태 관리 스크립트를 사용하는 방법에 대한 예제입니다.

```
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>

다음은 델타 토큰 기반 증분 전송에 상태 관리 스크립트를 사용하는 방법에 대한 예제입니다.

```
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())
```

두 예제 모두에서 상태 관리 스크립트는 작업 실행 간에 상태(델타 토큰 또는 타임스탬프)를 저장하는 복잡성을 처리합니다. 커넥터 옵션을 가져올 때 마지막 알려진 상태를 자동으로 검색하고 처리 후 상태를 업데이트하므로 각 작업 실행이 새 데이터 또는 변경된 데이터만 처리할 수 있습니다.