【问题标题】:Add some lines at the top of hive table在 hive 表的顶部添加一些行
【发布时间】:2019-10-27 01:51:03
【问题描述】:

我在 hive(之前)中有一个这种形式的表:

AB_dimp|SF_0060H00000nhSrmQAE|EBA Order 1127735|Execute|New From
AB_dimp|SF_0060H00000nhSwkQAE|EBA Order 1127725|Execute|New From
AB_Dimp|SF_0060H00000nhSyDQAU|EBA Order 1127728|Execute|New From

我想将这 3 行以这种形式(之后)显示在 hive 中该表的顶部:

[Yellow]
Cat ID|AN_Net|
[network]
AB_dimp|SF_0060H00000nhSkPQAU|EBA Order 1127708|Execute|New From
AB_DIMP|SF_0060H00000nhSl8QAE|EBA Order 1127709|Execute|New From
AB_DIMP|SF_0060H00000nhSrmQAE|EBA Order 1127735|Execute|New From

请问如何在 Hive 中实现这一点?

【问题讨论】:

  • 不清楚你在问什么
  • 嗨@leftjoin,我基本上是说我希望它出现在我上面的蜂巢表之前。 [hier] 机会 ID|SF_AllOpportunities|如何将这些值硬编码以显示在上表的顶部? [关系]
  • 但是“上表”是什么意思?这个“ [hier] Opportunity ID|SF_AllOpportunities| ”在您的示例中
  • 您是否要添加一些额外的行?您能否编辑问题并说明清楚。现在好像有点乱了
  • 好吧,也许我说的不对,所以我再试一次。假设我有一个表,并且我想选择该表的特定列(例如上表)。但在表格的最顶部,我想在表格顶部硬编码值“团队”“法律”“倡导者”。

标签: hadoop hive mapreduce bigdata hiveql


【解决方案1】:

a.) 首先,创建另一个表(假设是 NewTable)并插入这 3 条记录

b.) 现在,将现有数据插入到另一个表中

insert overwrite table NewTable select * from ExisitngTable;

c.) 删除 ExisitngTable

d.) 现在将数据从 NewTable 插入 ExisitngTable

insert overwrite table ExisitngTable select * from NewTable name;

【讨论】:

  • 谢谢。但只是我试图从表中进行选择并将结果保存在文本文件中。我想硬编码表格顶部的这 3 条记录。我不是要插入另一个表,而只是想进行选择并将其保存在文本文件中。
【解决方案2】:

全部使用联合:

select '[Yellow]' as col_name union all
select 'ID|AN_Net|'           union all
select '[network]'            union all
select col_name from your_table;

如果你想在表中添加这些行,不仅选择它们,你不需要中间表来实现:

insert overwrite your_table 
select * from 
(
    select '[Yellow]' as col_name union all
    select 'ID|AN_Net|'           union all
    select '[network]'            union all
    select col_name from your_table
)s;

但请记住,表中的行没有排序。当您选择没有order by 的表时,会在许多映射器上并行执行选择。底层文件被拆分,映射器读取每个自己的拆分。它们并行执行,彼此完全隔离,并且也独立返回结果。哪个更快它的结果将返回更快,你看,只有 order by 保证返回的行的顺序。这意味着下次当您以一定的概率选择此表时,您可能会返回这些额外的行而不是第一行。只有 ORDER BY 可以保证行的顺序。并且您需要有一些列可用于对行进行排序,例如 id,或者您的列可以按顺序使用。 如果表很小,则有可能在单个映射器上读取它,并且行将按原始顺序返回,就像在基础文件中一样。

要保留文件中的行顺序,您可以添加 row_order 列并在 ORDER BY 的上层查询中使用它:

select  DRM_Pln_Parent, opportunityid, opportunity_name
   from
   (
   SELECT 1 as row_order, '[hier]' as DRM_Pln_Parent, '' as opportunityid, '' as opportunity_name
UNION ALL
   SELECT 2 as row_order, 'Opportunity ID|SF_AllOpportunities|' as DRM_Pln_Parent, '' as opportunityid, '' as opportunity_name
UNION ALL
   SELECT 3 as row_order, '[relation]' as DRM_Pln_Parent, '' as opportunityid, '' as opportunity_name
UNION ALL 
   SELECT DISTINCT 4 as row_order, 'SF_AllOpportunities' AS DRM_Pln_Parent, 
CONCAT('SF_',opportunityid) as opportunityid, 
opportunity_name, 
from ...

   )s
order by row_order  

为了更好地理解,另请参阅此答案:https://stackoverflow.com/a/43368113/2700344

【讨论】:

  • 非常感谢您的贡献。但是我要添加的行不是来自任何表。我只是想按原样对它们进行硬编码,以便它们可以出现在表格的最顶部。我还可以使用 union all 来实现吗?
  • 嗨@leftjoin,我使用了UNION ALL。我得到的两个常见错误是:1)抽象语法树为空(2)联合双方的架构应该匹配......这是我的查询...... SELECT '[hier]' AS Hier UNION ALL SELECT 'Opportunity ID |SF_AllOpportunities|' UNION ALL SELECT '[relation]' UNION ALL 选择 DISTINCT 'AB_Allangles' AS CAD_Lsa_Altitude, CONCAT('AB_',opportunityid) 作为机会 ID,机会名称,舞台名称,机会类型,预测类别,default_opp_weighting 作为权重,iswon,来自 sf_opportunity 的关闭日期 WHERE... .
  • @Oye 当然,架构和列数应该匹配。您不能在单个数据集中的不同行中选择不同数量的列。可能的解决方案是还添加其他列:例如 null 作为 col2,null 作为 col3。
  • 我仍然收到此错误@leftjoin“联合双方的架构应该匹配”与此查询。 SELECT '[hier]' AS NULL UNION ALL SELECT '机会 ID|SF_AllOpportunities|' AS NULL UNION ALL SELECT '[relation]' AS NULL UNION ALL............也许我做错了什么。
  • 嗨@leftjoin,我设法使用union all 在我的select 语句中编写了行,但它们都出现在同一行而不是不同的行,如上图所示。此外,它出现在表格的底部而不是顶部。任何想法如何处理这两者?
猜你喜欢
  • 2010-11-24
  • 1970-01-01
  • 2020-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-30
  • 2015-09-18
相关资源
最近更新 更多