handle_load_train

in abap-sdk/ZGOOG_SDK_RECIPES/ZGOOG_SDK_BQML_RECIPE/zr_bigquery_ml_recipe.prog.abap [0:0]


  METHOD handle_load_train.

    DATA(lo_client) = bq_client( ).

    " TODO
    " For the purposes of this demo, The methpd get_timeseries_data
    " populates a random number along with the date into lt_data
    " For actual enterprise scenarios, you'd populate lt_data with
    " real business timeseries data such as total quanity sold per day
    DATA(lt_data) = get_timeseries_data( ).

    TRY.
        DATA(ls_input_load) = VALUE /goog/cl_bigquery_v2=>ty_133( ).

        LOOP AT lt_data ASSIGNING FIELD-SYMBOL(<ls_data>).
          APPEND INITIAL LINE TO ls_input_load-rows ASSIGNING FIELD-SYMBOL(<ls_row>).
          CREATE DATA <ls_row>-json TYPE t_timeseries.
          FIELD-SYMBOLS: <lfs_json> TYPE t_timeseries.
          ASSIGN <ls_row>-json->* TO <lfs_json> CASTING.
          <lfs_json> = <ls_data>.
        ENDLOOP.

        "Call API method: bigquery.tabledata.insertAll
        CALL METHOD lo_client->insert_all_tabledata
          EXPORTING
            iv_p_dataset_id = p_dset
            iv_p_project_id = CONV #( lo_client->gv_project_id )
            iv_p_table_id   = p_table
            is_input        = ls_input_load
          IMPORTING
            ev_ret_code     = DATA(lv_ret_code)
            ev_err_text     = DATA(lv_err_text).

        IF lo_client->is_success( lv_ret_code ) = abap_true.
          WRITE: / |Records inserted into table { p_table }|.
        ELSE.
          MESSAGE lv_err_text TYPE 'E'.
        ENDIF.



        DATA(ls_input_query) = VALUE /goog/cl_bigquery_v2=>ty_103( ).

        ls_input_query-query =
           | CREATE OR REPLACE MODEL `{ lo_client->gv_project_id }.{ p_dset }.{ p_model }` |  && c_nl &&
           |   OPTIONS |  && c_nl &&
           |     (model_type = 'ARIMA_PLUS', |  && c_nl &&
           |    time_series_timestamp_col = 'parsed_timestamp', |  && c_nl &&
           |    time_series_data_col = 'value', |  && c_nl &&
           |      auto_arima = TRUE, |  && c_nl &&
           |      data_frequency = 'AUTO_FREQUENCY', |  && c_nl &&
           |      decompose_time_series = TRUE |  && c_nl &&
           |        ) AS |  && c_nl &&
           |     SELECT |  && c_nl &&
           |       TIMESTAMP(date) AS parsed_timestamp, |  && c_nl &&
           |       value |  && c_nl &&
           |    FROM |  && c_nl &&
           |      `{ lo_client->gv_project_id }.{ p_dset }.{ p_table }` |.

        lo_client->query_jobs(
          EXPORTING
            iv_p_projects_id = CONV #( lo_client->gv_project_id )
            is_input         = ls_input_query
          IMPORTING
            ev_ret_code      = lv_ret_code
            ev_err_text      = lv_err_text ).

        IF lo_client->is_success( lv_ret_code ) = abap_true.
          WRITE: / |Model { p_model } created|.
        ELSE.
          MESSAGE lv_err_text TYPE 'E'.
        ENDIF.
      CATCH /goog/cx_sdk INTO DATA(lo_ex).
        MESSAGE lo_ex->get_text( ) TYPE 'E'.
    ENDTRY.