This topic explains how to query a level-1 partition by using PyODPS.
Prerequisites
The following are required:
-
MaxCompute is activated.
-
DataWorks is activated.
-
A workflow is created in DataWorks. For more information, see Create a workflow.
Procedure
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.
-
Prepare test data.
-
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
-
-
Write data from the source data table
user_detail_odsto the partitioned tableuser_detail.-
Log on to the DataWorks console.
-
In the left-side navigation pane, click Workspace.
-
Find the target workspace and in the Actions column, click .
-
Right-click the workflow and choose .
-
Enter a node name and click OK.
-
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; -
Click Run to write the data.
-
-
Query the level-1 partition by using PyODPS.
-
Log on to the DataWorks console.
-
In the left-side navigation pane, click Workspace.
-
Find the target workspace and in the Actions column, click .
-
On the Data Development page, right-click the workflow that you created and choose .
-
Enter a node name and click OK.
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)Click Run.
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
-