全部產品
Search
文件中心

:PyODPS參數傳遞

更新時間:Jun 19, 2024

本文為您介紹如何在DataWorks中進行PyODPS參數的傳遞。

前提條件

請提前完成如下操作:

操作步驟

  1. 準備測試資料。

    1. 建立表並上傳資料。操作方法請參見建表並上傳資料

      表結構以及來源資料資訊如下。

      • 分區表user_detail建表語句如下。

        create table if not exists user_detail
        (
        userid    BIGINT comment '使用者id',
        job       STRING comment '工作類型',
        education STRING comment '教育程度'
        ) comment '使用者資訊表'
        partitioned by (dt STRING comment '日期',region STRING comment '地區');
      • 來源資料表user_detail_ods建表語句如下。

        create table if not exists user_detail_ods
        (
          userid    BIGINT comment '使用者id',
          job       STRING comment '工作類型',
          education STRING comment '教育程度',
          dt STRING comment '日期',
          region STRING comment '地區'
        );
      • 測試資料儲存為user_detail.txt檔案。將此檔案上傳至表user_detail_ods中。

        0001,互連網,本科,20190715,beijing
        0002,教育,大專,20190716,beijing
        0003,金融,碩士,20190715,shandong
        0004,互連網,碩士,20190715,beijing
    2. 按右鍵商務程序,選擇建立節點 > MaxCompute > ODPS SQL

    3. 輸入節點名稱,並單擊確認

    4. 在ODPS SQL節點中輸入如下代碼。

      insert overwrite table user_detail partition (dt,region)
      select userid,job,education,dt,region from user_detail_ods;
    5. 單擊運行,將資料插入到分區表user_detail中。

  2. 使用PyODPS傳遞參數。

    1. 登入DataWorks控制台

    2. 在左側導覽列,單擊工作空間列表

    3. 選擇操作列中的快速進入 > 資料開發

    4. 資料開發頁面,按右鍵已經建立的商務程序,選擇建立節點 > MaxCompute > PyODPS 2

    5. 輸入節點名稱,單擊確認

    6. 在PyODPS 2節點中輸入如下代碼實現參數傳遞。

      import sys
      reload(sys)
      print('dt=' + args['dt'])
      #修改系統預設編碼。
      sys.setdefaultencoding('utf8')
      #擷取表。
      t = o.get_table('user_detail')
      #接受傳入的分區參數。
      with t.open_reader(partition='dt=' + args['dt'] + ',region=beijing') as reader1:
          count = reader1.count
      print("查詢分區表資料:")
      for record in reader1:
          print record[0],record[1],record[2]
    7. 單擊進階運行(帶參數運行)

    8. 參數對話方塊填寫配置參數,單擊確定

      配置參數說明如下:

      • 調度資源群組:選擇預設資源群組

      • dt:設定為dt=20190715

    9. 作業記錄中查看運行結果。作業記錄