Exemplos do DynamoDB usando o SDK para SAP ABAP - AWS Exemplos de código do SDK

Há mais exemplos de AWS SDK disponíveis no repositório AWS Doc SDK Examples GitHub .

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Exemplos do DynamoDB usando o SDK para SAP ABAP

Os exemplos de código a seguir mostram como realizar ações e implementar cenários comuns usando o AWS SDK para SAP ABAP com o DynamoDB.

Ações são trechos de código de programas maiores e devem ser executadas em contexto. Embora as ações mostrem como chamar funções de serviço específicas, é possível ver as ações contextualizadas em seus devidos cenários e exemplos entre serviços.

Cenários são exemplos de código que mostram como realizar uma tarefa específica chamando várias funções dentro do mesmo serviço.

Cada exemplo inclui um link para GitHub, onde você pode encontrar instruções sobre como configurar e executar o código no contexto.

Ações

O código de exemplo a seguir mostra como usar CreateTable.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

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.
  • Para obter detalhes da API, consulte a CreateTablereferência da API AWS SDK for SAP ABAP.

O código de exemplo a seguir mostra como usar DeleteItem.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

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.
  • Para obter detalhes da API, consulte a DeleteItemreferência da API AWS SDK for SAP ABAP.

O código de exemplo a seguir mostra como usar DeleteTable.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

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.
  • Para obter detalhes da API, consulte a DeleteTablereferência da API AWS SDK for SAP ABAP.

O código de exemplo a seguir mostra como usar DescribeTable.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

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.
  • Para obter detalhes da API, consulte a DescribeTablereferência da API AWS SDK for SAP ABAP.

O código de exemplo a seguir mostra como usar GetItem.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

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.
  • Para obter detalhes da API, consulte a GetItemreferência da API AWS SDK for SAP ABAP.

O código de exemplo a seguir mostra como usar ListTables.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

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.
  • Para obter detalhes da API, consulte a ListTablesreferência da API AWS SDK for SAP ABAP.

O código de exemplo a seguir mostra como usar PutItem.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

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.
  • Para obter detalhes da API, consulte a PutItemreferência da API AWS SDK for SAP ABAP.

O código de exemplo a seguir mostra como usar Query.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

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.
  • Para obter os detalhes da API, consulte Query na Referência da API AWS SDK para SAP ABAP.

O código de exemplo a seguir mostra como usar Scan.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

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.
  • Para obter os detalhes da API, consulte Scan na Referência da API AWS SDK para SAP ABAP.

O código de exemplo a seguir mostra como usar UpdateItem.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

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.
  • Para obter detalhes da API, consulte a UpdateItemreferência da API AWS SDK for SAP ABAP.

Cenários

O exemplo de código a seguir mostra como:

  • Criar uma tabela que possa conter dados de filmes.

  • Colocar, obter e atualizar um único filme na tabela.

  • Gravar dados de filmes na tabela usando um arquivo JSON de exemplo.

  • Consultar filmes que foram lançados em determinado ano.

  • Verificar filmes que foram lançados em um intervalo de anos.

  • Excluir um filme da tabela e, depois, excluir a tabela.

SDK para SAP ABAP
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

" 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.