handle_ml

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


  METHOD handle_ml.

    DATA(lo_client) = bq_client( ).

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

    ls_input_query-query =
     | SELECT |  && c_nl &&
     | * |  && c_nl &&
     | FROM |  && c_nl &&
     | ML.FORECAST(MODEL `{ lo_client->gv_project_id }.{ p_dset }.{ p_model }`,  |  && c_nl &&
     |  STRUCT(60 AS horizon, 0.8 AS confidence_level));  |.


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

        IF lo_client->is_success( lv_ret_code ) <> abap_true.
          MESSAGE lv_err_text TYPE 'E'.
        ENDIF.
      CATCH /goog/cx_sdk INTO DATA(lo_ex).
        MESSAGE lo_ex->get_text( ) TYPE 'E'.
    ENDTRY.

    FIELD-SYMBOLS: <ls_field> TYPE /goog/cl_bigquery_v2=>ty_132.

    DATA(lt_prediction) = VALUE tt_prediction( ).
    LOOP AT ls_output-rows REFERENCE INTO DATA(ls_row).

      DATA(ls_prediction) = VALUE t_prediction( ).
      LOOP AT ls_row->f ASSIGNING <ls_field> .
        DATA(lv_index) = sy-tabix.

        ASSIGN COMPONENT lv_index OF STRUCTURE ls_prediction
            TO FIELD-SYMBOL(<ls_target>).
        <ls_target> = <ls_field>-v->*.
      ENDLOOP.

      APPEND ls_prediction TO lt_prediction.
    ENDLOOP.

    cl_demo_output=>display( lt_prediction ).