Python读取文件并写入到MaxCompute表

使用Python,或者使用DataWorks创建PyODPS3节点,读取FTP或者远程FTP文件,然后写入到MaxCompute指定表的分区中。使用的库若未安装,可用pip install方式安装。代码如下:

from odps import ODPS
from odps.df import DataFrame
import numpy as np
import pandas as pd
from odps.df import output


o = ODPS('<odps-key>', 'odps-secret', 'project-name', endpoint='endpoint-address')
t = o.get_table('tmp_xxm_test')

my_file = '/Downloads/tmp_xxm_test.csv'

field_types = {
    '账户ID': object,
    '点击数': np.float64,  # np.float64
    '花费': object,
    '时间': object
}
# 先读取数据然后设定类型
data_src = pd.read_csv(my_file, header=0, dtype=field_types)

# 修改数据类型,可取代指定每列的类型
# data_src['my_num'] = data_src['my_num'].astype('Int32')

data_1 = pd.read_csv('/Downloads/tmp_xxm_test_1.csv', header=0, dtype=field_types)
# 两个DataFrame合并,即UNION
df = pd.concat([data_src, data_1], axis=0, join='outer', ignore_index=False)


'''
方式1:自定义方式写入,该方式可以写入到指定分区中,None表示写入null
'''
with t.open_writer(partition='ds=20231128', create_partition=True) as writer:
    records = data_src.apply(lambda x: [
        None if pd.isna(x['my_id']) else x['my_id'],
        None if pd.isna(x['my_num']) else x['my_num'],
        None if pd.isna(x['my_amt']) else x['my_amt'],
        None if pd.isna(x['my_time']) else x['my_time']
    ], axis='columns').tolist()
    print(records)
    writer.write(records)

print('sucess.')

'''
方式2:persist方式写入
1. 该方式写入时,目标表不要创建整数字段,因为如果整数字段为np.float的NaN或者pd的NA则无法转为integer。
2. 若data_src中使用'Int64'则创建DataFrame会报错无法识别Int64。
3. 原df的列名在目标表都必须存在,若df中列名缺少目标表的列则该字段填充null。
4. 输出按照列名去输出,不是按照顺序,顺序不一致没关系,但建议一致。
'''
df = DataFrame(data_src)
print(df)
'''
overwrite参数表示是否覆盖,默认True;drop_partition表示是否删除存在的分区数据,默认false。
drop_partition设为False仍然会覆盖已存在的指定分区,overwrite设为False才会Append方式写入。
'''
df.persist('tmp_xxm_test', partition='ds=20231128', drop_partition=True, create_partition=True, odps=o)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值