Hive insert插入数据与with子查询

1. insert into 与 insert overwrite区别

insert into 与 insert overwrite 都可以向hive表中插入数据,但是insert into直接追加到表中数据的尾部,而insert overwrite会重写数据,既先进行删除,再写入

注意:如果存在分区的情况,insert overwrite只重写当前分区数据,不会全部重写

2. insert 与 with as 子查询一起使用

当在hive中同时使用insert into(overwrite) table xx 与with子查询时候,需要将insert放在with as子查询后面(区分:与StarRocks不同,insert放在with as子查询前面)

WITH TEMP_A AS (
		SELECT TIME,IOT_ID,NAME FROM IOT_XX_A
	),
    TEMP_B AS (
		SELECT TIME,IOT_ID,NAME,COUNT(DISTINCT IOT_ID) AS TIMES FROM TEMP_A
			GROUP BY TIME,IOT_ID,NAME
	)
 
INSERT INTO TABLE TABLE_B	
	SELECT TIME,IOT_ID,NAME,TIMES FROM TEMP_B

注意: 当使用以下语句建表时,需要将create放在with as子查询前面

DROP TABLE IF EXISTS xx ;
CREATE TABLE xx AS
WITH TEMP_A AS (
		SELECT TIME,IOT_ID,NAME FROM IOT_XX_A
	),
    TEMP_B AS (
		SELECT TIME,IOT_ID,NAME,COUNT(DISTINCT IOT_ID) AS TIMES FROM TEMP_A
			GROUP BY TIME,IOT_ID,NAME
	)
SELECT TIME,IOT_ID,NAME,TIMES FROM TEMP_B

你可能感兴趣的:(Hive,hive,hadoop,数据仓库)