DynamoDB 適用於 SAP ABAP 的 SDK 範例 - AWS SDK 程式碼範例

AWS 文件 AWS SDK 範例 GitHub 存放庫中提供了更多 SDK 範例

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

DynamoDB 適用於 SAP ABAP 的 SDK 範例

下列程式碼範例說明如何透過使用適用於 SAP ABAP 的 AWS SDK 搭配 DynamoDB 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境和跨服務範例中查看內容中的動作。

Scenarios (案例) 是向您展示如何呼叫相同服務中的多個函數來完成特定任務的程式碼範例。

每個範例都包含一個連結 GitHub,您可以在其中找到如何在內容中設定和執行程式碼的指示。

動作

下列程式碼範例會示範如何使用CreateTable

適用於 SAP ABAP 的開發套件
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

TRY. DATA(lt_keyschema) = VALUE /aws1/cl_dynkeyschemaelement=>tt_keyschema( ( NEW /aws1/cl_dynkeyschemaelement( iv_attributename = 'year' iv_keytype = 'HASH' ) ) ( NEW /aws1/cl_dynkeyschemaelement( iv_attributename = 'title' iv_keytype = 'RANGE' ) ) ). DATA(lt_attributedefinitions) = VALUE /aws1/cl_dynattributedefn=>tt_attributedefinitions( ( NEW /aws1/cl_dynattributedefn( iv_attributename = 'year' iv_attributetype = 'N' ) ) ( NEW /aws1/cl_dynattributedefn( iv_attributename = 'title' iv_attributetype = 'S' ) ) ). " Adjust read/write capacities as desired. DATA(lo_dynprovthroughput) = NEW /aws1/cl_dynprovthroughput( iv_readcapacityunits = 5 iv_writecapacityunits = 5 ). oo_result = lo_dyn->createtable( it_keyschema = lt_keyschema iv_tablename = iv_table_name it_attributedefinitions = lt_attributedefinitions io_provisionedthroughput = lo_dynprovthroughput ). " Table creation can take some time. Wait till table exists before returning. lo_dyn->get_waiter( )->tableexists( iv_max_wait_time = 200 iv_tablename = iv_table_name ). MESSAGE 'DynamoDB Table' && iv_table_name && 'created.' TYPE 'I'. " This exception can happen if the table already exists. CATCH /aws1/cx_dynresourceinuseex INTO DATA(lo_resourceinuseex). DATA(lv_error) = |"{ lo_resourceinuseex->av_err_code }" - { lo_resourceinuseex->av_err_msg }|. MESSAGE lv_error TYPE 'E'. ENDTRY.
  • 如需 API 詳細資訊,請參閱 AWS SDK CreateTable中的 SAP ABAP API 參考資料。

下列程式碼範例會示範如何使用DeleteItem

適用於 SAP ABAP 的開發套件
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

TRY. DATA(lo_resp) = lo_dyn->deleteitem( iv_tablename = iv_table_name it_key = it_key_input ). MESSAGE 'Deleted one item.' TYPE 'I'. CATCH /aws1/cx_dyncondalcheckfaile00. MESSAGE 'A condition specified in the operation could not be evaluated.' TYPE 'E'. CATCH /aws1/cx_dynresourcenotfoundex. MESSAGE 'The table or index does not exist' TYPE 'E'. CATCH /aws1/cx_dyntransactconflictex. MESSAGE 'Another transaction is using the item' TYPE 'E'. ENDTRY.
  • 如需 API 詳細資訊,請參閱 AWS SDK DeleteItem中的 SAP ABAP API 參考資料。

下列程式碼範例會示範如何使用DeleteTable

適用於 SAP ABAP 的開發套件
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

TRY. lo_dyn->deletetable( iv_tablename = iv_table_name ). " Wait till the table is actually deleted. lo_dyn->get_waiter( )->tablenotexists( iv_max_wait_time = 200 iv_tablename = iv_table_name ). MESSAGE 'Table ' && iv_table_name && ' deleted.' TYPE 'I'. CATCH /aws1/cx_dynresourcenotfoundex. MESSAGE 'The table ' && iv_table_name && ' does not exist' TYPE 'E'. CATCH /aws1/cx_dynresourceinuseex. MESSAGE 'The table cannot be deleted since it is in use' TYPE 'E'. ENDTRY.
  • 如需 API 詳細資訊,請參閱 AWS SDK DeleteTable中的 SAP ABAP API 參考資料。

下列程式碼範例會示範如何使用DescribeTable

適用於 SAP ABAP 的開發套件
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

TRY. oo_result = lo_dyn->describetable( iv_tablename = iv_table_name ). DATA(lv_tablename) = oo_result->get_table( )->ask_tablename( ). DATA(lv_tablearn) = oo_result->get_table( )->ask_tablearn( ). DATA(lv_tablestatus) = oo_result->get_table( )->ask_tablestatus( ). DATA(lv_itemcount) = oo_result->get_table( )->ask_itemcount( ). MESSAGE 'The table name is ' && lv_tablename && '. The table ARN is ' && lv_tablearn && '. The tablestatus is ' && lv_tablestatus && '. Item count is ' && lv_itemcount TYPE 'I'. CATCH /aws1/cx_dynresourcenotfoundex. MESSAGE 'The table ' && lv_tablename && ' does not exist' TYPE 'E'. ENDTRY.
  • 如需 API 詳細資訊,請參閱 AWS SDK DescribeTable中的 SAP ABAP API 參考資料。

下列程式碼範例會示範如何使用GetItem

適用於 SAP ABAP 的開發套件
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

TRY. oo_item = lo_dyn->getitem( iv_tablename = iv_table_name it_key = it_key ). DATA(lt_attr) = oo_item->get_item( ). DATA(lo_title) = lt_attr[ key = 'title' ]-value. DATA(lo_year) = lt_attr[ key = 'year' ]-value. DATA(lo_rating) = lt_attr[ key = 'rating' ]-value. MESSAGE 'Movie name is: ' && lo_title->get_s( ) && 'Movie year is: ' && lo_year->get_n( ) && 'Moving rating is: ' && lo_rating->get_n( ) TYPE 'I'. CATCH /aws1/cx_dynresourcenotfoundex. MESSAGE 'The table or index does not exist' TYPE 'E'. ENDTRY.
  • 如需 API 詳細資訊,請參閱 AWS SDK GetItem中的 SAP ABAP API 參考資料。

下列程式碼範例會示範如何使用ListTables

適用於 SAP ABAP 的開發套件
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

TRY. oo_result = lo_dyn->listtables( ). " You can loop over the oo_result to get table properties like this. LOOP AT oo_result->get_tablenames( ) INTO DATA(lo_table_name). DATA(lv_tablename) = lo_table_name->get_value( ). ENDLOOP. DATA(lv_tablecount) = lines( oo_result->get_tablenames( ) ). MESSAGE 'Found ' && lv_tablecount && ' tables' TYPE 'I'. CATCH /aws1/cx_rt_service_generic INTO DATA(lo_exception). DATA(lv_error) = |"{ lo_exception->av_err_code }" - { lo_exception->av_err_msg }|. MESSAGE lv_error TYPE 'E'. ENDTRY.
  • 如需 API 詳細資訊,請參閱 AWS SDK ListTables中的 SAP ABAP API 參考資料。

下列程式碼範例會示範如何使用PutItem

適用於 SAP ABAP 的開發套件
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

TRY. DATA(lo_resp) = lo_dyn->putitem( iv_tablename = iv_table_name it_item = it_item ). MESSAGE '1 row inserted into DynamoDB Table' && iv_table_name TYPE 'I'. CATCH /aws1/cx_dyncondalcheckfaile00. MESSAGE 'A condition specified in the operation could not be evaluated.' TYPE 'E'. CATCH /aws1/cx_dynresourcenotfoundex. MESSAGE 'The table or index does not exist' TYPE 'E'. CATCH /aws1/cx_dyntransactconflictex. MESSAGE 'Another transaction is using the item' TYPE 'E'. ENDTRY.
  • 如需 API 詳細資訊,請參閱 AWS SDK PutItem中的 SAP ABAP API 參考資料。

下列程式碼範例會示範如何使用Query

適用於 SAP ABAP 的開發套件
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

TRY. " Query movies for a given year . DATA(lt_attributelist) = VALUE /aws1/cl_dynattributevalue=>tt_attributevaluelist( ( NEW /aws1/cl_dynattributevalue( iv_n = |{ iv_year }| ) ) ). DATA(lt_key_conditions) = VALUE /aws1/cl_dyncondition=>tt_keyconditions( ( VALUE /aws1/cl_dyncondition=>ts_keyconditions_maprow( key = 'year' value = NEW /aws1/cl_dyncondition( it_attributevaluelist = lt_attributelist iv_comparisonoperator = |EQ| ) ) ) ). oo_result = lo_dyn->query( iv_tablename = iv_table_name it_keyconditions = lt_key_conditions ). DATA(lt_items) = oo_result->get_items( ). "You can loop over the results to get item attributes. LOOP AT lt_items INTO DATA(lt_item). DATA(lo_title) = lt_item[ key = 'title' ]-value. DATA(lo_year) = lt_item[ key = 'year' ]-value. ENDLOOP. DATA(lv_count) = oo_result->get_count( ). MESSAGE 'Item count is: ' && lv_count TYPE 'I'. CATCH /aws1/cx_dynresourcenotfoundex. MESSAGE 'The table or index does not exist' TYPE 'E'. ENDTRY.
  • 如需 API 詳細資訊,請參閱《適用於 SAP ABAP 的AWS SDK API 參考》中的 Query

下列程式碼範例會示範如何使用Scan

適用於 SAP ABAP 的開發套件
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

TRY. " Scan movies for rating greater than or equal to the rating specified DATA(lt_attributelist) = VALUE /aws1/cl_dynattributevalue=>tt_attributevaluelist( ( NEW /aws1/cl_dynattributevalue( iv_n = |{ iv_rating }| ) ) ). DATA(lt_filter_conditions) = VALUE /aws1/cl_dyncondition=>tt_filterconditionmap( ( VALUE /aws1/cl_dyncondition=>ts_filterconditionmap_maprow( key = 'rating' value = NEW /aws1/cl_dyncondition( it_attributevaluelist = lt_attributelist iv_comparisonoperator = |GE| ) ) ) ). oo_scan_result = lo_dyn->scan( iv_tablename = iv_table_name it_scanfilter = lt_filter_conditions ). DATA(lt_items) = oo_scan_result->get_items( ). LOOP AT lt_items INTO DATA(lo_item). " You can loop over to get individual attributes. DATA(lo_title) = lo_item[ key = 'title' ]-value. DATA(lo_year) = lo_item[ key = 'year' ]-value. ENDLOOP. DATA(lv_count) = oo_scan_result->get_count( ). MESSAGE 'Found ' && lv_count && ' items' TYPE 'I'. CATCH /aws1/cx_dynresourcenotfoundex. MESSAGE 'The table or index does not exist' TYPE 'E'. ENDTRY.
  • 如需 API 詳細資訊,請參閱《適用於 SAP ABAP 的AWS SDK API 參考》中的 Scan

下列程式碼範例會示範如何使用UpdateItem

適用於 SAP ABAP 的開發套件
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

TRY. oo_output = lo_dyn->updateitem( iv_tablename = iv_table_name it_key = it_item_key it_attributeupdates = it_attribute_updates ). MESSAGE '1 item updated in DynamoDB Table' && iv_table_name TYPE 'I'. CATCH /aws1/cx_dyncondalcheckfaile00. MESSAGE 'A condition specified in the operation could not be evaluated.' TYPE 'E'. CATCH /aws1/cx_dynresourcenotfoundex. MESSAGE 'The table or index does not exist' TYPE 'E'. CATCH /aws1/cx_dyntransactconflictex. MESSAGE 'Another transaction is using the item' TYPE 'E'. ENDTRY.
  • 如需 API 詳細資訊,請參閱 AWS SDK UpdateItem中的 SAP ABAP API 參考資料。

案例

以下程式碼範例顯示做法:

  • 建立可存放電影資料的資料表。

  • 放入、取得和更新資料表中的單個電影。

  • 將影片資料從範例 JSON 檔案寫入資料表。

  • 查詢特定年份發表的電影。

  • 掃描某個年份範圍內發表的電影。

  • 從資料表刪除電影,然後刪除資料表。

適用於 SAP ABAP 的開發套件
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

" Create an Amazon Dynamo DB table. TRY. DATA(lo_session) = /aws1/cl_rt_session_aws=>create( cv_pfl ). DATA(lo_dyn) = /aws1/cl_dyn_factory=>create( lo_session ). DATA(lt_keyschema) = VALUE /aws1/cl_dynkeyschemaelement=>tt_keyschema( ( NEW /aws1/cl_dynkeyschemaelement( iv_attributename = 'year' iv_keytype = 'HASH' ) ) ( NEW /aws1/cl_dynkeyschemaelement( iv_attributename = 'title' iv_keytype = 'RANGE' ) ) ). DATA(lt_attributedefinitions) = VALUE /aws1/cl_dynattributedefn=>tt_attributedefinitions( ( NEW /aws1/cl_dynattributedefn( iv_attributename = 'year' iv_attributetype = 'N' ) ) ( NEW /aws1/cl_dynattributedefn( iv_attributename = 'title' iv_attributetype = 'S' ) ) ). " Adjust read/write capacities as desired. DATA(lo_dynprovthroughput) = NEW /aws1/cl_dynprovthroughput( iv_readcapacityunits = 5 iv_writecapacityunits = 5 ). DATA(oo_result) = lo_dyn->createtable( it_keyschema = lt_keyschema iv_tablename = iv_table_name it_attributedefinitions = lt_attributedefinitions io_provisionedthroughput = lo_dynprovthroughput ). " Table creation can take some time. Wait till table exists before returning. lo_dyn->get_waiter( )->tableexists( iv_max_wait_time = 200 iv_tablename = iv_table_name ). MESSAGE 'DynamoDB Table' && iv_table_name && 'created.' TYPE 'I'. " It throws exception if the table already exists. CATCH /aws1/cx_dynresourceinuseex INTO DATA(lo_resourceinuseex). DATA(lv_error) = |"{ lo_resourceinuseex->av_err_code }" - { lo_resourceinuseex->av_err_msg }|. MESSAGE lv_error TYPE 'E'. ENDTRY. " Describe table TRY. DATA(lo_table) = lo_dyn->describetable( iv_tablename = iv_table_name ). DATA(lv_tablename) = lo_table->get_table( )->ask_tablename( ). MESSAGE 'The table name is ' && lv_tablename TYPE 'I'. CATCH /aws1/cx_dynresourcenotfoundex. MESSAGE 'The table does not exist' TYPE 'E'. ENDTRY. " Put items into the table. TRY. DATA(lo_resp_putitem) = lo_dyn->putitem( iv_tablename = iv_table_name it_item = VALUE /aws1/cl_dynattributevalue=>tt_putiteminputattributemap( ( VALUE /aws1/cl_dynattributevalue=>ts_putiteminputattrmap_maprow( key = 'title' value = NEW /aws1/cl_dynattributevalue( iv_s = 'Jaws' ) ) ) ( VALUE /aws1/cl_dynattributevalue=>ts_putiteminputattrmap_maprow( key = 'year' value = NEW /aws1/cl_dynattributevalue( iv_n = |{ '1975' }| ) ) ) ( VALUE /aws1/cl_dynattributevalue=>ts_putiteminputattrmap_maprow( key = 'rating' value = NEW /aws1/cl_dynattributevalue( iv_n = |{ '7.5' }| ) ) ) ) ). lo_resp_putitem = lo_dyn->putitem( iv_tablename = iv_table_name it_item = VALUE /aws1/cl_dynattributevalue=>tt_putiteminputattributemap( ( VALUE /aws1/cl_dynattributevalue=>ts_putiteminputattrmap_maprow( key = 'title' value = NEW /aws1/cl_dynattributevalue( iv_s = 'Star Wars' ) ) ) ( VALUE /aws1/cl_dynattributevalue=>ts_putiteminputattrmap_maprow( key = 'year' value = NEW /aws1/cl_dynattributevalue( iv_n = |{ '1978' }| ) ) ) ( VALUE /aws1/cl_dynattributevalue=>ts_putiteminputattrmap_maprow( key = 'rating' value = NEW /aws1/cl_dynattributevalue( iv_n = |{ '8.1' }| ) ) ) ) ). lo_resp_putitem = lo_dyn->putitem( iv_tablename = iv_table_name it_item = VALUE /aws1/cl_dynattributevalue=>tt_putiteminputattributemap( ( VALUE /aws1/cl_dynattributevalue=>ts_putiteminputattrmap_maprow( key = 'title' value = NEW /aws1/cl_dynattributevalue( iv_s = 'Speed' ) ) ) ( VALUE /aws1/cl_dynattributevalue=>ts_putiteminputattrmap_maprow( key = 'year' value = NEW /aws1/cl_dynattributevalue( iv_n = |{ '1994' }| ) ) ) ( VALUE /aws1/cl_dynattributevalue=>ts_putiteminputattrmap_maprow( key = 'rating' value = NEW /aws1/cl_dynattributevalue( iv_n = |{ '7.9' }| ) ) ) ) ). " TYPE REF TO ZCL_AWS1_dyn_PUT_ITEM_OUTPUT MESSAGE '3 rows inserted into DynamoDB Table' && iv_table_name TYPE 'I'. CATCH /aws1/cx_dyncondalcheckfaile00. MESSAGE 'A condition specified in the operation could not be evaluated.' TYPE 'E'. CATCH /aws1/cx_dynresourcenotfoundex. MESSAGE 'The table or index does not exist' TYPE 'E'. CATCH /aws1/cx_dyntransactconflictex. MESSAGE 'Another transaction is using the item' TYPE 'E'. ENDTRY. " Get item from table. TRY. DATA(lo_resp_getitem) = lo_dyn->getitem( iv_tablename = iv_table_name it_key = VALUE /aws1/cl_dynattributevalue=>tt_key( ( VALUE /aws1/cl_dynattributevalue=>ts_key_maprow( key = 'title' value = NEW /aws1/cl_dynattributevalue( iv_s = 'Jaws' ) ) ) ( VALUE /aws1/cl_dynattributevalue=>ts_key_maprow( key = 'year' value = NEW /aws1/cl_dynattributevalue( iv_n = '1975' ) ) ) ) ). DATA(lt_attr) = lo_resp_getitem->get_item( ). DATA(lo_title) = lt_attr[ key = 'title' ]-value. DATA(lo_year) = lt_attr[ key = 'year' ]-value. DATA(lo_rating) = lt_attr[ key = 'year' ]-value. MESSAGE 'Movie name is: ' && lo_title->get_s( ) TYPE 'I'. MESSAGE 'Movie year is: ' && lo_year->get_n( ) TYPE 'I'. MESSAGE 'Movie rating is: ' && lo_rating->get_n( ) TYPE 'I'. CATCH /aws1/cx_dynresourcenotfoundex. MESSAGE 'The table or index does not exist' TYPE 'E'. ENDTRY. " Query item from table. TRY. DATA(lt_attributelist) = VALUE /aws1/cl_dynattributevalue=>tt_attributevaluelist( ( NEW /aws1/cl_dynattributevalue( iv_n = '1975' ) ) ). DATA(lt_keyconditions) = VALUE /aws1/cl_dyncondition=>tt_keyconditions( ( VALUE /aws1/cl_dyncondition=>ts_keyconditions_maprow( key = 'year' value = NEW /aws1/cl_dyncondition( it_attributevaluelist = lt_attributelist iv_comparisonoperator = |EQ| ) ) ) ). DATA(lo_query_result) = lo_dyn->query( iv_tablename = iv_table_name it_keyconditions = lt_keyconditions ). DATA(lt_items) = lo_query_result->get_items( ). READ TABLE lo_query_result->get_items( ) INTO DATA(lt_item) INDEX 1. lo_title = lt_item[ key = 'title' ]-value. lo_year = lt_item[ key = 'year' ]-value. lo_rating = lt_item[ key = 'rating' ]-value. MESSAGE 'Movie name is: ' && lo_title->get_s( ) TYPE 'I'. MESSAGE 'Movie year is: ' && lo_year->get_n( ) TYPE 'I'. MESSAGE 'Movie rating is: ' && lo_rating->get_n( ) TYPE 'I'. CATCH /aws1/cx_dynresourcenotfoundex. MESSAGE 'The table or index does not exist' TYPE 'E'. ENDTRY. " Scan items from table. TRY. DATA(lo_scan_result) = lo_dyn->scan( iv_tablename = iv_table_name ). lt_items = lo_scan_result->get_items( ). " Read the first item and display the attributes. READ TABLE lo_query_result->get_items( ) INTO lt_item INDEX 1. lo_title = lt_item[ key = 'title' ]-value. lo_year = lt_item[ key = 'year' ]-value. lo_rating = lt_item[ key = 'rating' ]-value. MESSAGE 'Movie name is: ' && lo_title->get_s( ) TYPE 'I'. MESSAGE 'Movie year is: ' && lo_year->get_n( ) TYPE 'I'. MESSAGE 'Movie rating is: ' && lo_rating->get_n( ) TYPE 'I'. CATCH /aws1/cx_dynresourcenotfoundex. MESSAGE 'The table or index does not exist' TYPE 'E'. ENDTRY. " Update items from table. TRY. DATA(lt_attributeupdates) = VALUE /aws1/cl_dynattrvalueupdate=>tt_attributeupdates( ( VALUE /aws1/cl_dynattrvalueupdate=>ts_attributeupdates_maprow( key = 'rating' value = NEW /aws1/cl_dynattrvalueupdate( io_value = NEW /aws1/cl_dynattributevalue( iv_n = '7.6' ) iv_action = |PUT| ) ) ) ). DATA(lt_key) = VALUE /aws1/cl_dynattributevalue=>tt_key( ( VALUE /aws1/cl_dynattributevalue=>ts_key_maprow( key = 'year' value = NEW /aws1/cl_dynattributevalue( iv_n = '1975' ) ) ) ( VALUE /aws1/cl_dynattributevalue=>ts_key_maprow( key = 'title' value = NEW /aws1/cl_dynattributevalue( iv_s = '1980' ) ) ) ). DATA(lo_resp) = lo_dyn->updateitem( iv_tablename = iv_table_name it_key = lt_key it_attributeupdates = lt_attributeupdates ). MESSAGE '1 item updated in DynamoDB Table' && iv_table_name TYPE 'I'. CATCH /aws1/cx_dyncondalcheckfaile00. MESSAGE 'A condition specified in the operation could not be evaluated.' TYPE 'E'. CATCH /aws1/cx_dynresourcenotfoundex. MESSAGE 'The table or index does not exist' TYPE 'E'. CATCH /aws1/cx_dyntransactconflictex. MESSAGE 'Another transaction is using the item' TYPE 'E'. ENDTRY. " Delete table. TRY. lo_dyn->deletetable( iv_tablename = iv_table_name ). lo_dyn->get_waiter( )->tablenotexists( iv_max_wait_time = 200 iv_tablename = iv_table_name ). MESSAGE 'DynamoDB Table deleted.' TYPE 'I'. CATCH /aws1/cx_dynresourcenotfoundex. MESSAGE 'The table or index does not exist' TYPE 'E'. CATCH /aws1/cx_dynresourceinuseex. MESSAGE 'The table cannot be deleted as it is in use' TYPE 'E'. ENDTRY.