gpt4 book ai didi

regex - Hive:在列名中使用 Concat

转载 作者:行者123 更新时间:2023-12-01 16:15:58 26 4
gpt4 key购买 nike

我正在尝试从列名称为:year_2016、year_2017、year_2018 等的表中获取数据。我不确定如何从此表中获取数据。数据如下:

| count_of_accidents | year_2016 | year_2017 |year_2018 | 
|--------------------|-----------|-----------|----------|
| 15 | 12 | 5 | 1 |
| 5 | 10 | 6 | 18 |

我已经尝试过“concat”功能,但这实际上不起作用。

我试过这个:

select SUM( count_of_accidents * concat('year_',year(regexp_replace('2018_1_1','_','-')))) 
from table_name;

列名(year_2017 或 year_2018 等)将作为参数传递。所以,我真的不能像这样硬编码列名-

select SUM( count_of_accidents * year_2018) from table_name;

有什么办法可以做到这一点吗?

最佳答案

您可以使用 regular expressions 来做到这一点.像这样:

--create test table
create table test_col(year_2018 string, year_2019 string);

set hive.support.quoted.identifiers=none;
set hive.cli.print.header=true;

--test select using hard-coded pattern
select year_2018, `(year_)2019` from test_col;
OK
year_2018 year_2019
Time taken: 0.862 seconds

--test pattern parameter
set hivevar:year_param=2019;

select year_2018, `(year_)${year_param}` from test_col;
OK
year_2018 year_2019
Time taken: 0.945 seconds

--two parameters
set hivevar:year_param1=2018;
set hivevar:year_param2=2019;

select `(year_)${year_param1}`, `(year_)${year_param2}` from test_col t;
OK
year_2018 year_2019
Time taken: 0.159 seconds

--parameter contains full column_name and using more strict regexp pattern
set hivevar:year_param2=year_2019;

select `^${year_param2}$` from test_col t;
OK
year_2019
Time taken: 0.053 seconds

--select all columns using single pattern year_ and four digits
select `^year_[0-9]{4}$` from test_col t;
OK
year_2018 year_2019

应计算参数并将其传递给配置单元脚本,列名中不支持 concat()、regexp_replace 等函数。

列别名也不适用于使用正则表达式提取的列:

 select t.number_of_incidents, `^${year_param}$` as year1 from test_t t;

抛出异常:

FAILED: SemanticException [Error 10004]: Line 1:30 Invalid table alias or column reference '^year_2018$': (possible column names are: number_of_incidents, year_2016, year_2017, year_2018)

我找到了一种解决方法,可以使用 union all 和空数据集来为列添加别名,请参阅此测试:

create table test_t(number_of_incidents int, year_2016 int, year_2017 int, year_2018 int);
insert into table test_t values(15, 12, 5, 1); --insert test data
insert into table test_t values(5,10,6,18);

--parameter, can be passed from outside the script from command line
set hivevar:year_param=year_2018;

--enable regex columns and print column names
set hive.support.quoted.identifiers=none;
set hive.cli.print.header=true;

--Alias column using UNION ALL with empty dataset
select sum(number_of_incidents*year1) incidents_year1
from
(--UNION ALL with empty dataset to alias columns extracted
select 0 number_of_incidents, 0 year1 where false --returns no rows because of false condition
union all
select t.number_of_incidents, `^${year_param}$` from test_t t
)s;

结果:

OK
incidents_year1
105
Time taken: 38.003 seconds, Fetched: 1 row(s)

UNION ALL 中的第一个查询不影响数据,因为它不返回任何行。但它的列名成为整个 UNION ALL 数据集的名称,可以在上层查询中使用。这个技巧有效。如果您发现使用正则表达式提取别名列的更好解决方法,请同时添加您的解决方案。

更新:

如果您可以将完整的 column_name 作为参数传递,则不需要正则表达式。 Hive 在查询执行之前按原样替换变量(不计算它们)。仅当由于某种原因无法传递完整的列名时才使用正则表达式,并且像在原始查询中一样需要一些模式连接。看这个测试:

--parameter, can be passed from outside the script from command line  
set hivevar:year_param=year_2018;

select sum(number_of_incidents*${year_param}) incidents_year1 from test_t t;

结果:

OK
incidents_year1
105
Time taken: 63.339 seconds, Fetched: 1 row(s)

关于regex - Hive:在列名中使用 Concat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55801139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com