文档

Streaming Tunnel SDK示例(Python)

更新时间:

Streaming Tunnel是MaxCompute的流式数据通道,您可以通过Streaming Tunnel向MaxCompute中上传数据,本文为您介绍使用Python版Streaming Tunnel SDK上传数据的简单示例。

注意事项

Streaming Tunnel仅支持上传数据,下文为您介绍一个简单的数据上传的Python SDK示例。

上传示例

import os
from odps import ODPS
from odps.tunnel import TableTunnel

# 确保 ALIBABA_CLOUD_ACCESS_KEY_ID 环境变量设置为用户 Access Key ID,
# ALIBABA_CLOUD_ACCESS_KEY_SECRET 环境变量设置为用户 Access Key Secret,
# 不建议直接使用 Access Key ID / Access Key Secret 字符串
odps = ODPS(
    os.getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'),
    os.getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
    project='<your-default-project>',
    endpoint='<your-end-point>',
)

tunnel = TableTunnel(odps)
table = odps.get_table('<table_name>')
upload_session = tunnel.create_stream_upload_session(table.name, partition_spec='pt=test')

with upload_session.open_record_writer() as writer:
      record = table.new_record()
      record[0] = 'test1'
      record[1] = 'id1'
      writer.write(record)

      record = table.new_record(['test2', 'id2'])
      writer.write(record)