如何访问的最后一个数组中的元素? [英] How to access the last element in an array ?

查看:145
本文介绍了如何访问的最后一个数组中的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的蜂巢表中,会议字段是像格式的字符串:

IP的会话ID,用户ID 面积会话ID,用户ID

有相隔3个或4个领域 - ,但用户ID总是最后一个

我想选择的用户ID,但是如何访问的最后一个字段?在Python中,有这样的:

改编[-1]

但在蜂巢,如何实现这一目标?下面的SQL似乎不正确的。


选择拆分(会话,\\ - )[ - 1]从my_table的用户;

谢谢!


解决方案

 反向(分(反向(会话),' - ')[0])

虽然这可能会有点比正则表达式的解决方案更昂贵;)

In my hive table, the session field is a string in format like:

ip-sessionID-userID or area-sessionID-userID

There's 3 or 4 fields separated by "-", but userID is always the last one.

i wanna select userID, but how to access the last field? In python, there's something like: arr[-1]

but in hive, how to achieve this? The following SQL seems not correct.

select split(session,"\-")[-1] as user from my_table;

Thanks!

解决方案

reverse(split(reverse(session), '-')[0])

Although this might be a bit more expensive than the regex solution ;)

这篇关于如何访问的最后一个数组中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆