简体   繁体   English

Hive - 带有行数据的 Concat 字符串

[英]Hive - Concat String with row data

Am trying to concat a string with data row in a table using Hive.我正在尝试使用 Hive 将字符串与表中的数据行连接起来。

which looks like this in SQL在 SQL 中看起来像这样

 SELECT 'Select * from ' + [Column] + '; '
                 FROM table_name
                 ORDER BY [table_name]

Result Should be - SELECT * FROM abc;结果应该是- SELECT * FROM abc; SELECT * FROM asd;从 asd 中选择 *; SELECT * FROM xyz..选择 * 从 xyz..

Unable to write or find something related to this in Hive command.无法在 Hive 命令中写入或找到与此相关的内容。

Use concat() function:使用 concat() 函数:

'SELECT concat('Select * from ', [table_name],  '; ') 
                 FROM your_table
                 ORDER BY [table_name]'

if a [table_name] is a column containing table_name如果 [table_name] 是包含 table_name 的列

Concate the string data which is in row.连接行中的字符串数据。 Say x , y to be the column names.假设x , y是列名。

insert into orders(1,"a");
insert into orders(2,"b");

Here suppose we want to concate "a" , "b" as one entity.这里假设我们想将"a""b"连接为一个实体。 So we do as follow所以我们做如下

select concat_ws(',',collect_list(a,b)) from orders;

You can use concat to accomplish your task.您可以使用 concat 来完成您的任务。 But if you directly put ";"但是如果你直接把“;” inside concat function it may show "can not recognize input error" to avoid this use \ before;在 concat 函数中,它可能会显示“无法识别输入错误”以避免之前使用 \;

below is the tested and working example of the same下面是相同的测试和工作示例

select concat("select * from ", col , " \;") from table

output:输出:

select * from A;从 A 中选择 *;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM