由于直接从Maxcompute导出数据有条数限制,最多只能导出一万条,如果数据量太大,则不能直接从页面上导出。可以通过以下脚本把数据导出到文本文件或者Excel.
导出到文本文件脚本如下:
python
# coding=utf-8
import datetime
from odps import ODPS
import time
odps = ODPS('access_key', 'access_secret', 'namespace',
endpoint='http://service.cn-hangzhou.maxcompute.aliyun.com/api')
sql = "SELECT DISTINCT mobile FROM xxxx.sms WHERE pt>='20250107' AND pt<='20250631';"
save_path = "/Users/xxxx/Desktop/mobiles.txt"
with open(save_path, 'w', encoding='utf-8') as f: # 使用UTF-8编码打开文件
# 写入表头(可选)
# f.write("Mobile\n")
with odps.execute_sql(sql).open_reader() as reader:
for data in reader:
# 使用制表符分隔数据,并用换行符分隔记录
line = f"{data['mobile']}\n"
f.write(line)
exit(0)
导出到Excel脚本如下:
python
# coding=utf-8
import datetime
from odps import ODPS
import time
import xlwt
import openpyxl
odps = ODPS('access_key', 'access_secret', 'namespace',
endpoint='http://service.cn-hangzhou.maxcompute.aliyun.com/api')
sql = " SELECT mobile, FROM xxx.sms WHERE pt>='20250801' AND pt<='20250804' ;"
outwb = openpyxl.Workbook() # 打开一个将写的文件
outws = outwb.create_sheet(index=0) # 在将写的文件创建sheet
i = 1
with odps.execute_sql(sql).open_reader() as reader:
for data in reader:
print(data)
outws.cell(i, 1).value = data['mobile']
i += 1
saveExcel = "/Users/xxx/Desktop/mobiles.xlsx"
outwb.save(saveExcel) # 一定要记得保存
exit(1)