Tüm Ürünler
Search
Belge Merkezi

MaxCompute:PyODPS ile bölümlemeli tablo verilerini okuma

Son Güncelleme:Oct 04, 2026

DataWorks çalışma alanında PyODPS kullanarak bir bölümlemeli tablodan veri okuyun.

Önkoşullar

Aşağıdaki gereksinimleri karşılayın:

Yordam

Not

Bu örnekte standart modda bir DataWorks çalışma alanı kullanılmaktadır. Çalışma alanını oluştururken Join Public Preview of DataStudio seçeneğini işaretlemeyin. Genel önizleme modundaki çalışma alanları bu örnekle uyumlu değildir.

  1. Test verilerini hazırlayın.

    1. Bir tablo oluşturun ve veri yükleyin. Daha fazla bilgi için bkz. Tablo oluşturma ve veri yükleme.

      Aşağıda tablo şemaları ve kaynak veriler gösterilmiştir.

      • Bölümlemeli tablo user_detail için tablo oluşturma ifadesi aşağıdaki gibidir.

        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');
      • Kaynak tablo user_detail_ods için oluşturma ifadesi aşağıdaki gibidir.

        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'
        );
      • Test verilerini user_detail.txt dosyasına kaydedin. Bu dosyayı user_detail_ods tablosuna yükleyin.

        0001,Internet,Bachelor,20190715,beijing
        0002,Education,Associate Degree,20190716,beijing
        0003,Finance,Master,20190715,shandong
        0004,Internet,Master,20190715,beijing
    2. Kaynak veri tablosundan user_detail_ods bölümlemeli tabloya user_detail veri yazın.

      1. DataWorks konsolu üzerinde oturum açın.

      2. Sol gezinme bölmesinde Workspace öğesine tıklayın.

      3. Hedef çalışma alanını bulun ve Actions sütununda Shortcuts > Data Development yolunu izleyin.

      4. İş akışına sağ tıklayın ve Create Node > ODPS SQL seçeneğini belirleyin.

      5. Bir düğüm adı girin ve OK düğmesine tıklayın.

      6. ODPS SQL düğümünde aşağıdaki kodu girin.

        INSERT OVERWRITE TABLE user_detail PARTITION (dt, region) 
        SELECT userid, job, education, dt, region FROM user_detail_ods;
      7. Verileri yazmak için Run düğmesine tıklayın.

  2. PyODPS kullanarak bölümlemeli tablodan veri okuyun.

    1. DataWorks konsolu üzerinde oturum açın.

    2. Sol gezinme bölmesinde Workspace öğesine tıklayın.

    3. Hedef çalışma alanını bulun ve Actions sütununda Shortcuts > Data Development yolunu izleyin.

    4. Data Development sayfasında, oluşturduğunuz iş akışına sağ tıklayın ve Create Node > PyODPS 2 seçeneğini belirleyin.

    5. Bir düğüm adı girin ve OK düğmesine tıklayın.

    6. PyODPS 2 düğümünde aşağıdaki kodu girin.

      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"]
    7. Run with Parameters düğmesine tıklayın.

    8. Parameters iletişim kutusunda parametreleri yapılandırın ve Run düğmesine tıklayın.

      Aşağıdaki parametreleri yapılandırın:

      • Resource Group Name: Shared Resource Group seçeneğini belirleyin.

      • dt: Değerini dt=20190715 olarak ayarlayın.

    9. Runtime Log sekmesinde çalıştırma sonuçlarını görüntüleyin.

      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