- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
如果现有数据存在,我想根据某些条件更新它(应该更新优先级更高的数据),如果不存在则插入新数据。
我已经为此编写了一个查询,但不知何故它重复了行数。以下是对我所拥有的以及我想要实现的目标的完整解释:
我有:表 1 - 列 - id、信息、优先级
hive> select * from sample1;
OK
1 123 1.01
2 234 1.02
3 213 1.03
5 213423 1.32
Time taken: 1.217 seconds, Fetched: 4 row(s)
表 2:列 - id、信息、优先级
hive> select * from sample2;
OK
1 1234 1.05
2 23412 1.01
3 21 1.05
4 1232 1.1
2 3432423 1.6
3 34324 1.4
我想要的是最终表的每个 id 应该只有 1 行,数据根据最高优先级排列:
1 1234 1.05
2 3432423 1.6
3 34324 1.4
4 1232 1.1
5 213423 1.32
我写的查询是这样的:
insert overwrite table sample1
select a.id,
case when cast(TRIM(a.prio) as double) > cast(TRIM(b.prio) as double) then a.info else b.info end as info,
case when cast(TRIM(a.prio) as double) > cast(TRIM(b.prio) as double) then a.prio else b.prio end as prio
from sample1 a
join
sample2 b
on a.id=b.id where b.id in (select distinct(id) from sample1)
union all
select * from sample2 where id not in (select distinct(id) from sample1)
union all
select * from sample1 where id not in (select distinct(id) from sample2);
运行此查询后,我得到以下结果:
hive> select * from sample1;
OK
1 1234 1.05
2 234 1.02
3 21 1.05
2 3432423 1.6
3 34324 1.4
5 213423 1.32
4 1232 1.1
如何修改当前查询以获得正确的结果。我是否可以遵循任何其他方法/过程来实现最终结果。我正在使用 hadoop 2.5.2 和 HIVE 1.2.1 。我在一个 6 节点集群上工作,该集群有 5 个从节点和 1 个 NN。
最佳答案
使用FULL JOIN
,它将返回所有已连接的行加上来自左侧的所有未连接的行以及来自右表的所有未连接的行。sample2
表包含每个 id
的重复行,这就是连接重复行的原因,使用 row_number()
分析函数仅从中选择具有最高优先级的行示例 2
表:
insert overwrite table sample1
select
nvl(a.id, b.id) as id,
case when cast(TRIM(a.prio) as double) > cast(TRIM(b.prio) as double) then a.info else b.info end as info,
case when cast(TRIM(a.prio) as double) > cast(TRIM(b.prio) as double) then a.prio else b.prio end as prio
from ( select a.*, row_number() over (partition by id order by prio desc) rn
from sample1 a
) a
full join
( select b.*, row_number() over (partition by id order by prio desc) rn
from sample2 b
) b on a.id=b.id and b.rn=1 --join only with highest priority rows
where a.rn=1;
如果 sample1
表还包含每个 id
的多行(它不在您的示例中),请将使用 row_number 的相同技术应用于表 sample1。
另请参阅有关使用完全连接
进行合并的答案:https://stackoverflow.com/a/37744071/2700344
从 Hive 2.2 开始,您还可以使用 ACID Merge,请参阅 examples
关于hadoop - HIVE如何根据某些条件更新现有数据,如果不存在则插入新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53001909/
我可以将 CSV 或任何其他平面文件导入到 hive 中,而无需先在 hive 中创建和定义表结构吗?假设我的 csv 文件有 200 列,需要导入到 hive 表中。所以我必须首先在 hive 中创
我有以下示例数据,我试图在 hive 中爆炸它.. 我使用了 split 但我知道我错过了一些东西.. ["[[-80.742426,35.23248],[-80.740424,35.23184],[
我有一个很大的日志文件,我加载到 HDFS . HDFS将根据机架感知复制到不同的节点。 现在我将相同的文件加载到配置单元表中。命令如下: create table log_analysis (log
我正在尝试使用 UDF在 hive 中。但是当我尝试使用 userdate as 'unixtimeToDate' 创建一个临时函数时,我得到这个异常(exception) hive> create
在Mysql中,我们可以使用DO sleep(5) ;来进行暂停。但它在 Hive 中不起作用。 Hive有 sleep 功能吗? 最佳答案 你可以通过反射调用Thread让hive在处理每一行后多等
我正在将数据从 csv 文件导入 Hive。我的表包含字符串和整数。但是,在我的输入文件中,整数周围有空格,所以它看起来像这样: some string, 2 ,another stri
我可以嵌套吗select在 Hive 中具有不同的条件?例如 如果我有以下两个 Hive 查询: select percentile(x, 0.95) from t1 where y = 1; sel
hive 安装有什么特定的模式吗? 例如,Hadoop 安装有 3 种模式:独立、伪分布式和完全分布式。 同样,Hive 是否有任何特定类型的分布? Hive 可以分布式安装吗? 最佳答案 Hive
我正在使用 Hive,我有一个结构如下的表: CREATE TABLE t1 ( id INT, created TIMESTAMP, some_value BIGINT ); 我需要找到
我是 Hadoop 生态系统工具的新手。 任何人都可以帮助我了解 hive 、直线和 hive 之间的区别。 提前致谢! 最佳答案 Apache hive : 1] Apache Hive 是一个建立
如何在 Hive 中写出数组文字? SELECT PERCENTILE(my_column, [0.5, 0.25, 0.50, 0.75, 0.95]) AS quantiles FROM my_t
我正在尝试在Hive中重命名columnName。是否可以在Hive中重命名列名称。 tableA(栏1,_c1,_c2) 至 tableA(column1,column2,column3) ?? 最
减号查询似乎在 HIVE 中不起作用。 尝试过: select x from abc minus select x from bcd ; 我做错了还是没有为 HIVE 定义负查询?如果是这样,还有其他
我正在尝试使用 hive-jdbc 连接将数据插入 Hive (NON-ACID) 表。如果我在“语句”中执行单个 SQL 查询,它就可以工作。如果我尝试使用“addBatch”对 SQL 进行批处理
我知道这些, 要获取表中的列名,我们可以触发: show columns in . 要获取表的描述(包括 column_name、column_type 和许多其他详细信息): describe [f
无法找到有关 Hive 表最大字符限制的合适规范。 我正在开发一个涉及 hive 表的 ETL 过程,这些表已指定格式为 _ 的命名约定,并且提供的表名称远大于 30 字节(pl/sql 的正常限制)
在安装了Hive的集群中,metastore和namenode有什么?我了解 Metastore 拥有所有表架构、分区详细信息和元数据。现在这个元数据是什么?那么namenode有什么呢?这个元存储在
Hive 中静态分区和动态分区的主要区别是什么?使用单独的插入意味着静态,而对分区表的单个插入意味着动态。还有什么优点吗? 最佳答案 在静态分区中,我们需要在每个 LOAD 语句中指定分区列值。 假设
我是 hadoop 和 hive 的新手。如果有人研究过pivot in hive的概念,请与我分享。 例如:来自 teradata 或 oracle 的数据未转置,这些数据应在 hive 中转置。那
1)如果分区列没有数据,那么当你查询它时,你会得到什么错误? 2)如果某些行没有分区列,这些行将如何处理?会不会有数据丢失? 3)为什么需要对数字列进行分桶?我们也可以使用字符串列吗?流程是什么?您将
我是一名优秀的程序员,十分优秀!