Leia dados de uma tabela particionada usando PyODPS em um workspace do DataWorks.
Pré-requisitos
Atenda aos seguintes requisitos:
MaxCompute está ativado.
DataWorks está ativado.
Você possui um fluxo de trabalho no DataWorks. Para mais informações, consulte Crie um fluxo de trabalho.
Procedimento
Este exemplo utiliza um workspace do DataWorks no modo padrão. Ao criar o workspace, não selecione Join Public Preview of DataStudio . Workspaces em pré-visualização pública não são compatíveis com este exemplo.
-
Prepare os dados de teste.
-
Crie uma tabela e carregue os dados. Para mais informações, consulte Crie uma tabela e carregue dados.
Veja a seguir os esquemas das tabelas e os dados de source.
-
A instrução de criação da tabela particionada user_detail é a seguinte.
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'); -
A instrução para criar a tabela de source user_detail_ods é a seguinte.
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' ); -
Salve os dados de teste no arquivo user_detail.txt e carregue-o na tabela user_detail_ods.
0001,Internet,Bachelor,20190715,beijing 0002,Education,Associate Degree,20190716,beijing 0003,Finance,Master,20190715,shandong 0004,Internet,Master,20190715,beijing
-
-
Grave os dados da tabela de source
user_detail_odsna tabela particionadauser_detail.Faça login no console do DataWorks.
No painel de navegação à esquerda, clique em Workspace.
Localize o workspace desejado e, na coluna Actions, clique em .
Clique com o botão direito no fluxo de trabalho e escolha .
Insira um nome para o nó e clique em OK.
-
No nó ODPS SQL, insira o código a seguir.
INSERT OVERWRITE TABLE user_detail PARTITION (dt, region) SELECT userid, job, education, dt, region FROM user_detail_ods; Clique em Run para gravar os dados.
-
-
Use PyODPS para ler dados da tabela particionada.
Faça login no console do DataWorks.
No painel de navegação à esquerda, clique em Workspace.
Localize o workspace desejado e, na coluna Actions, clique em .
Na página Data Development, clique com o botão direito no fluxo de trabalho criado e escolha .
Insira um nome para o nó e clique em OK.
-
No nó PyODPS 2, insira o código a seguir.
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"] Clique em Run with Parameters.
-
Na caixa de diálogo Parameters, configure os parâmetros e clique em Run.
Configure os seguintes parâmetros:
Resource Group Name: Selecione Shared Resource Group.
dt: Defina como dt=20190715.
-
Visualize os resultados da execução na aba 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