首页 > 解决方案 > 如何使用阿里巴巴ODPS python SDK中的Schema.from_lists方法创建模式?

问题描述

我通常通过表列和可选分区进行初始化来创建模式。我知道在阿里巴巴 ODPS python SDK 中通过方法创建模式Schema.from_lists在 LOC 和性能方面要好得多。

我经常用来创建模式的代码是:

from odps.models import Schema, Column, Partition
columns = [Column(name='num', type='bigint', comment='the column')]
partitions = [Partition(name='pt', type='string', comment='the partition')]
schema = Schema(columns=columns, partitions=partitions)
print(schema.columns)

输出:

[<column num, type bigint>, <partition pt, type string>]

如何使用Schema.from_lists方法创建模式?

标签: pythonpython-3.xalibaba-cloud

解决方案


您可以通过传递四个列表来创建。

In [33]: Schema.from_lists(['num'], ['bigint'], ['pt'], ['string'])             
Out[33]: 
odps.Schema {
  num   bigint      
}
Partitions {
  pt    string      
}

缺点是通过这种方式,您不能再指定列的注释。


推荐阅读