Maxcompute SQL 的查询结果条数受限1W

问题描述

记一次使用阿里云的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 来下载结果了,再也不会出现文章开头所描述的两种问题了。
打开该配置有两种方法:

  1. 如果已经下载最新的 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 查询结果的条数。
若不设置,下载条数不受限。
  1. 使用 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;来关闭此功能。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值