问题描述
记阿里云记一次Data Works处理需求,因为我们Sql处理后,数据产生大量行数据,导出为excel数据不完整的问题
原因分析:
我们每天都在这里 Maxcompute 平台上提交 select query,用于查询特定数据。但熟悉平台的同学都知道,从平台获取 sql 查询结果是一个 Restful 请求可能会遇到以下两个问题:
1 超时获取数据。
若数据分布在多个存储小文件上,平台需要花费大量时间来收集和合并这些数据。然而,在这个漫长的合并过程中,获取数据 Restful 请求可能已经超时。此时 Maxcompute Console 以下警告:Warning: ODPS request failed, requestID:xxxx, retryCount:1, will retry in xxx seconds.
2 数据获取量有限。
由于一次 Restful 要求的返回数据有限,当一次性获取所有数据到本地时,内存可能会爆炸,Maxcompute SQL 查询结果条数有限,具体值为 project 上的配置项 READ_TABLE_MAX_ROW (
默认为 10000
)。这将显示我们查询结果 2W 条,终于在了 Maxcompute Console 或者 Logview 上只看到 1W条 奇怪的情况
。
解决方案:
Instanc Tunnel功能
Instance Tunnel 提供使用 Tunnel 来下载 SQL 查询结果的功能,不仅能摆脱上述两类问题,可直接获取查询结果;还丰富了 Maxcompute Tunnel 下载通道不再局限于表数据。换句话说,我们以前可以用它 Tunnel 来下载 Maxcompute 如今,我们也可以使用表数据 Tunnel 来下载 Maxcompute Instance 的数据。
Instance Tunnel 使用
1. 用 Maxcompute Console 来获取 Instance 数据
1.1 使用 Tunnel download 命令将特定 Instance 将执行结果下载到本地文件中
命令:
tunnel download instance://<[project_name/]instance_id> <path>
参数:
project_name: instance 项目名称; instance_id: 下载数据 instance id 举例:
// 执行一条 select 查询: odps@ odps_test_project>select * from wc_in; ID = 20170724071705393ge3csfb8 ... ... // 使用 Instance Tunnel Download 命令下载执行结果到本地文件 odps@ odps_test_project>tunnel download instance://20170724071705393ge3csfb8 result; 2017-07-24 15:18:47 - new session: 2017072415184785b6516400090ca8 total lines: 8 2017-07-24 15:18:47 - file [0]: [0, 8), result downloading 8 records into 1 file 2017-07-24 15:18:47 - file [0] start
2017-07-24 15:18:48 - file [0] OK. total: 44 bytes
download OK
// 查看结果
cat result
slkdfj
hellp
apple
tea
peach
apple
tea
teaa
1.2 通过配置参数使 SQL 查询默认采用 Instance Tunnel 方式输出执行结果
在 Maxcompute Console 中打开 use_instance_tunnel
选项之后,执行的 select query 就会默认使用 Instance tunnel 来下载结果了,再也不会出现文章开头所描述的两种问题了。 打开该配置有两种方法:
- 如果已经下载最新的 Console,odps_config.ini 里面已经默认打开此选项,并
默认将 instance_tunnel_max_record 设置成了10000
。 如下所示:
# download sql results by instance tunnel
use_instance_tunnel=true
# the max records when download sql results by instance tunnel
instance_tunnel_max_record=10000
其中 instance_tunnel_max_record 表示使用 Instance tunnel 下载 sql 查询结果的条数。
若不设置,下载条数不受限。
- 使用 set console.sql.result.instancetunnel=true; 开启此功能。
// 打开 Instance tunnel 选项
odps@ odps_test_tunnel_project>set console.sql.result.instancetunnel=true;
OK
// 运行 select query
odps@ odps_test_tunnel_project>select * from wc_in;
ID = 20170724081946458g14csfb8
Log view:
http://logview/xxxxx.....
+------------+
| key |
+------------+
| slkdfj |
| hellp |
| apple |
| tea |
| peach |
| apple |
| tea |
| teaa |
+------------+
A total of 8 records fetched by instance tunnel.
可以看到,如果使用 Instance tunnel 的方式来输出 select 查询结果,会在最后打印一条提示。比如上面例子中的提示告诉我们这个 instance 的执行结果一共有 8 条数据。同样也可以 set console.sql.result.instancetunnel=false;
来关闭此功能。