【问题标题】:Selecting some part of a string from a column in hive and getting a count从 hive 中的列中选择字符串的某些部分并获取计数
【发布时间】:2014-05-06 19:17:57
【问题描述】:

我有一列“平台”,其行如下。

name3:"string1/string2/string3/s:1.2.1/ABCD/XYZ".

我还有另一个名为“name2”的列名。我的桌子是这样的

' id       |    name2     |   name3
-----------+--------------+---------------------
 1         |      x1      | string1/string2/string3/s:1.2.1/ABCD/XYZ
 2         |      x1      | string1/string2/string3/S:2.2.1/ABCD/XYZ
 3         |      x2      | string5/string4/string3/s:1.1/ABCD/XYZ
 4         |      x3      | string1/string6/string7/m:0.2.2/ABCD/XYZ
 5         |      x2      | string1/string2/string3/S:2.2.0/ABCD/XYZ'

我想根据平台的子字符串获取事件计数。喜欢

'name3     | X1    |   X2 |    X3    |

string4        |       |   1  |          |
string6        |       |      |   1      |'

或者如果我想仅基于“android”或“iOS”来获取计数,我该怎么做?

'name3     | X1    |   X2 |    X3    |

 string4          |       |   1  |          |
 string1      |   2   |   1  |   1      |'

我用于计数的查询如下。它可以很好地获取事件计数,但无法弄清楚如何根据子字符串获取计数。

'select name2,
    count(1) AS total
from table1 where name2='x1' OR name2='x2' OR name2='x3'
group by name2;'

有什么建议吗?

【问题讨论】:

  • 在我看来,这种数据库设计是完全错误的。我会设置一个带有平台(名称)列的表,然后做一个 1-N 关系(1 个事件-(一个或多个平台))。那么就很容易做你想做的事情了。
  • 表格是我提到的表格,我无法更改。我们可以在不改变设计的情况下做到这一点吗?像我们可以使用任何正则表达式吗?
  • 你提到什么形式?
  • 第一个包含 id、event 和 platform 列。

标签: hive hiveql


【解决方案1】:

首先,我会将该字符串拆分为具有实际列的视图。比如:

create view my_view as select
id,
event,
regexp_extract(platform, '^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$', 1) as os,
regexp_extract(platform, '^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$', 2) as brand,
regexp_extract(platform, '^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$', 3) as model,
regexp_extract(platform, '^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$', 4) as lte,
regexp_extract(platform, '^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$', 5) as abcd,
regexp_extract(platform, '^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$', 6) as user,
regexp_extract(platform, '^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$', 7) as xyz
from my_table;

然后查询该视图就更容易了。您也可以将其作为内部查询。这种在不同列中需要不同计数的“count-where”查询是一种非常常见的用途。我所知道的最好的方法是使用模式:

sum(if( [condition] , 1, 0))

因此,对于您的示例,它将是:

select os,
sum(if(event = 'x1', 1, 0)) as x1,
sum(if(event = 'x2', 1, 0)) as x2,
sum(if(event = 'x3', 1, 0)) as x3
from my_view
group by os;

或者:

select brand,
sum(if(event = 'x1', 1, 0)) as x1,
sum(if(event = 'x2', 1, 0)) as x2,
sum(if(event = 'x3', 1, 0)) as x3
from my_view
group by brand;

这是上面的查询,但使用该视图作为内部查询而不是实际视图:

select brand,
sum(if(event = 'x1', 1, 0)) as x1,
sum(if(event = 'x2', 1, 0)) as x2,
sum(if(event = 'x3', 1, 0)) as x3
from (
  select
  id,
  event,
  regexp_extract(platform, '^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$', 1) as os,
  regexp_extract(platform, '^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$', 2) as brand,
  regexp_extract(platform, '^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$', 3) as model,
  regexp_extract(platform, '^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$', 4) as lte,
  regexp_extract(platform, '^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$', 5) as abcd,
  regexp_extract(platform, '^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$', 6) as user,
  regexp_extract(platform, '^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$', 7) as xyz
  from my_table
) t
group by brand;

【讨论】:

  • 嗨,我认为你是对的。但我只是对字符串做了一个小改动。我添加了一个子字符串,例如“sfour:4.2.2”。你能看看上面的完整字符串吗?让我知道正则表达式是如何变化的。非常感谢
  • 好吧,同一个正则表达式现在只会将整个字符串“sfour:4.2.2”捕获为一个字段。如果要将其拆分为两个字段,例如“sfour”和“4.2.2”,它是:'^(.*)/(.*)/(.*)/(.*):(.*)/(.*)/(.*)/(.*)$',所以你需要选择八个字段(因为有八个捕获组)而不是七个。
  • 嗨乔,关于快速问题。对于上述问题,我有两个列名,用于上述相同的字符串格式。一列名称是平台和其他平台类型。这两列中的数据相似,具有相同的字符串格式('string1/string2/string3/s:1.2.1/ABCD/XYZ')。有没有办法将这两个列包含在同一个查询中?提前致谢
【解决方案2】:

希望这会有所帮助...

查询:

 select a.platforms, a.event, count(1) as count from 
 (select regexp_extract(platform, '^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$',1) 
 as platforms, event from table1) a group by a.platforms, a.event;

输出:

platforms       event   count
android         x1      2
android         x2      1    
android         x3      1
ios             x2      1

【讨论】:

    猜你喜欢
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 2022-11-13
    • 2021-09-07
    相关资源
    最近更新 更多