您可以使用TableWriter API对MaxCompute表进行读写。

读写MaxCompute表的功能由tensorflow.python_io.TableWriter提供,可以在TensorFlow的Graph执行逻辑以外,直接对MaxCompute表进行操作。
说明 PAI-TF作业执行过程中,写入MaxCompute表的数据,必须在作业正常结束以后,才能通过TableWriter API访问。如果作业正在运行或异常退出,则无法访问。

创建Writer并打开表

初始化TableWriter将打开一个MaxCompute表并返回Writer对象。接口定义如下。
writer = tf.python_io.TableWriter(table, slice_id=0)
  • table:string类型,表示需要打开的MaxCompute表名。该参数需要与PAI命令-Doutput中的输出表名一致,否则系统报table xxx not predefined错误。
  • slice_id:int类型,表示在表的不同分区进行写入,避免写冲突。单机场景下,使用默认值0即可。分布式场景下,如果多个worker(包括PS)同时使用相同的slice_id写入数据,则会导致写入失败。
注意 每次打开一张表,相当于打开一张空表,即原表中的数据被清空。

写入记录

将数据写入已打开表的对应列,该写入数据必须在关闭表之后才能读取。接口定义如下。
writer.write(values, indices)
  • values:表示写入的单行或多行数据。
    • 如果写入单行数据,则传入一个由标量组成的Tuple、List或1D-Ndarray。其中List和1D-Ndarray表示写入的各列数据类型相同。
    • 如果写入N(N>=1)行数据,则传入一个List或1D-Ndarray,参数中的每个元素对应一个单行数据(Tuple、List或以Structure为元素的Ndarray)。
  • indices:表示数据写入的列,支持由整形index组成的Tuple、List或1D-Ndarray类型。indices中的每个数对应表的每一列(列数从0开始计算)。

关闭表

接口定义如下。
writer.close()
with语句块中,无需显示调用close()关闭表。
注意 关闭表之后,如果使用open命令重新打开表,则表中原数据被清空。

通过with语句使用TableWriter

TableWriter支持通过with语句管理上下文,示例代码如下。
with tf.python_io.TableWriter(table) as writer:
    # Prepare values for writing.
    writer.write(values, incides)
    # Table would be closed automatically outside this section,

示例

  1. 在project中新建一张含有4列元素的MaxCompute表test_write,表的列名及数据类型如下。
    ColumnName ColumnType
    uid bigint
    name string
    price double
    virtual bool
  2. 使用Python命令,并配置-Doutputsodps://project/tables/test_write,将如下表格中的数据写入test_write表。
    uid name price virtual
    25 "Apple" 5.0 False
    38 "Pear" 4.5 False
    17 "Watermelon” 2.2 False
    # table_writer_test.py文件。
    
    import tensorflow as tf
    
    # Prepare data, 准备数据。
    values = [(25, "Apple", 5.0, False),
              (38, "Pear", 4.5, False),
              (17, "Watermelon", 2.2, False)]
    
    # Open a table,打开一个表并返回writer对象。
    writer = tf.python_io.TableWriter("odps://project/tables/test")
    
    # Write records to the 0~3 columns of the table,将数据写入表的第0~3列。
    records = writer.write(values, indices=[0, 1, 2, 3])
    
    # Close the table,关闭表和Writer。
    writer.close()
  3. 提交任务至PAI-TF,并执行任务。
    $ odpscmd -e "pai -name tensorflow140 -Dscript=<absolute_path_of_script>/table_writer_test.py -Doutputs=odps://project/tables/test_write ;"