Lisez les données d'une table partitionnée à l'aide de PyODPS dans un espace de travail DataWorks.
Prérequis
Assurez-vous de respecter les conditions suivantes :
MaxCompute est activé.
DataWorks est activé.
Vous disposez d'un flux de travail dans DataWorks. Pour plus d'informations, consultez la rubrique Créer un flux de travail.
Procédure
Cet exemple utilise un espace de travail DataWorks en mode standard. Lors de la création de l'espace de travail, ne sélectionnez pas Join Public Preview of DataStudio . Les espaces de travail en aperçu public ne sont pas compatibles avec cet exemple.
-
Préparez les données de test.
-
Créez une table et chargez les données. Pour plus d'informations, consultez la rubrique Créer une table et charger des données.
Voici les schémas de table et les données source.
-
L'instruction de création de la table partitionnée user_detail est la suivante.
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'); -
L'instruction de création de la table source user_detail_ods est la suivante.
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' ); -
Enregistrez les données de test dans le fichier user_detail.txt. Chargez ce fichier dans la table user_detail_ods.
0001,Internet,Bachelor,20190715,beijing 0002,Education,Associate Degree,20190716,beijing 0003,Finance,Master,20190715,shandong 0004,Internet,Master,20190715,beijing
-
-
Écrivez les données de la table source
user_detail_odsvers la table partitionnéeuser_detail.Connectez-vous à la console DataWorks.
Dans le volet de navigation de gauche, cliquez sur Workspace.
Localisez l'espace de travail cible et, dans la colonne Actions, cliquez sur .
Faites un clic droit sur le flux de travail et choisissez .
Saisissez un nom de nœud et cliquez sur OK.
-
Dans le nœud ODPS SQL, saisissez le code suivant.
INSERT OVERWRITE TABLE user_detail PARTITION (dt, region) SELECT userid, job, education, dt, region FROM user_detail_ods; Cliquez sur Run pour écrire les données.
-
-
Utilisez PyODPS pour lire les données de la table partitionnée.
Connectez-vous à la console DataWorks.
Dans le volet de navigation de gauche, cliquez sur Workspace.
Localisez l'espace de travail cible et, dans la colonne Actions, cliquez sur .
Sur la page Data Development, faites un clic droit sur le flux de travail que vous avez créé et choisissez .
Saisissez un nom de nœud et cliquez sur OK.
-
Dans le nœud PyODPS 2, saisissez le code suivant.
import sys from odps import ODPS reload(sys) print('dt=' + args['dt']) # Set the default system encoding to UTF-8. sys.setdefaultencoding('utf8') # Get the table object. t = o.get_table('user_detail') # Check whether a specific partition exists. print t.exist_partition('dt=20190715,region=beijing') # List all partitions in the table. for partition in t.partitions: print partition.name # Query data by using one of the following three methods. # Method 1: Use open_reader() as a context manager. # The reader is automatically closed when the 'with' block is exited, which ensures proper resource cleanup. with t.open_reader(partition='dt=20190715,region=beijing') as reader1: count = reader1.count print("Query data from the partitioned table by using Method 1:") for record in reader1: print record[0],record[1],record[2] # Method 2: Use open_reader() without a context manager. # This method allows you to access records by column name. print("Query data from the partitioned table by using Method 2:") reader2 = t.open_reader(partition='dt=20190715,region=beijing') for record in reader2: print record["userid"],record["job"],record["education"] # Method 3: Use read_table() on the ODPS object. # This is the most concise option for simple read operations. print("Query data from the partitioned table by using Method 3:") for record in o.read_table('user_detail', partition='dt=20190715,region=beijing'): print record["userid"],record["job"],record["education"] Cliquez sur Run with Parameters.
-
Dans la boîte de dialogue Parameters, configurez les paramètres et cliquez sur Run.
Configurez les paramètres suivants :
Resource Group Name : sélectionnez Shared Resource Group.
dt : définissez la valeur sur dt=20190715.
-
Consultez les résultats d'exécution dans l'onglet Runtime Log.
Executing user script with PyODPS 0.8.0 dt=20190715 True dt='20190715',region='beijing' dt='20190715',region='shandong' dt='20190716',region='beijing' Query data from the partitioned table by using Method 1: 4 Internet master 1 Internet bachelor Query data from the partitioned table by using Method 2: 4 Internet master 1 Internet bachelor Query data from the partitioned table by using Method 3: 4 Internet master 1 Internet bachelor