All Products
Search
Document Center

MaxCompute:Query a level-1 partition with PyODPS

Last Updated:Jun 24, 2026

This topic explains how to query a level-1 partition by using PyODPS.

Prerequisites

The following are required:

Procedure

Note

This example uses the basic mode of DataWorks. When you create a workspace, the Participate in Public Preview of DataStudio option is deselected by default. This example does not apply to workspaces in public preview.

  1. Prepare test data.

    1. Create a table and upload data. For more information, see Create a table and upload data.

      The following are the table schemas and source data.

      • The table creation statement for the partitioned table user_detail is as follows.

        CREATE TABLE IF NOT EXISTS user_detail
        (
        userid    BIGINT COMMENT 'User ID',
        job       STRING COMMENT 'Job type',
        education STRING COMMENT 'Education'
        ) COMMENT 'User information table'
        PARTITIONED BY (dt STRING COMMENT 'Date',region STRING COMMENT 'Region');
      • The statement to create the source table user_detail_ods is as follows.

        CREATE TABLE IF NOT EXISTS user_detail_ods
        (
          userid    BIGINT COMMENT 'User ID',
          job       STRING COMMENT 'Job type',
          education STRING COMMENT 'Education',
          dt STRING COMMENT 'Date',
          region STRING COMMENT 'Region'
        );
      • Save the test data to the user_detail.txt file. Upload this file to the user_detail_ods table.

        0001,Internet,Bachelor,20190715,beijing
        0002,Education,Associate Degree,20190716,beijing
        0003,Finance,Master,20190715,shandong
        0004,Internet,Master,20190715,beijing
    2. Write data from the source data table user_detail_ods to the partitioned table user_detail.

      1. Log on to the DataWorks console.

      2. In the left-side navigation pane, click Workspace.

      3. Find the target workspace and in the Actions column, click Shortcuts > Data Development.

      4. Right-click the workflow and choose Create Node > ODPS SQL.

      5. Enter a node name and click OK.

      6. In the ODPS SQL node, enter the following code.

        INSERT OVERWRITE TABLE user_detail PARTITION (dt, region) 
        SELECT userid, job, education, dt, region FROM user_detail_ods;
      7. Click Run to write the data.

  2. Query the level-1 partition by using PyODPS.

    1. Log on to the DataWorks console.

    2. In the left-side navigation pane, click Workspace.

    3. Find the target workspace and in the Actions column, click Shortcuts > Data Development.

    4. On the Data Development page, right-click the workflow that you created and choose Create Node > PyODPS 2.

    5. Enter a node name and click OK.

    6. Enter the following code to query the level-1 partition in three ways: asynchronously, synchronously, and by using a DataFrame.

      import sys
      reload(sys)
      # Set the default system encoding to utf8.
      sys.setdefaultencoding('utf8')
      # Read the level-1 partition asynchronously.
      instance = o.run_sql('select * from user_detail WHERE dt=\'20190715\'')
      instance.wait_for_success()
      for record in instance.open_reader():
          print record["userid"],record["job"],record["education"]
      # Read the level-1 partition synchronously.
      with o.execute_sql('select * from user_detail WHERE dt=\'20190715\'').open_reader() as reader4:
          print reader4.raw
          for record in reader4:
              print record["userid"],record["job"],record["education"]
      # Use a PyODPS DataFrame to read the level-1 partition.
      pt_df = DataFrame(o.get_table('user_detail').get_partition('dt=20190715'))
      print pt_df.head(10)
    7. Click Run.

    8. View the results in the Runtime Log.

      4  Internet  master
      1  Internet  bachelor
      3  finance  master
      "userid","job","education","dt","region"
      4,"Internet","master","20190715","beijing"
      1,"Internet","bachelor","20190715","beijing"
      3,"finance","master","20190715","shandong"
      4  Internet  master
      1  Internet  bachelor
      3  finance  master
      Try to fetch data from tunnel
         userid      job education
      0       4  Internet    master
      1       1  Internet  bachelor
      2       3   finance    master