标签:
HQL DML 主要涉到对Hive表中数据操作,包含有:load、INSERT、DELETE、EXPORT and IMPORT,详细资料参见:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DML
目录:
Loading files into tables:
#创建表 CREATE TABLE web_log(viewTime INT, userid BIGINT, url STRING, referrer STRING, ip STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘\t‘; #导入文件数据 LOAD DATA LOCAL INPATH ‘/usr/zhu/table.txt‘ OVERWRITE INTO TABLE web_log;
Inserting data into Hive Tables from queries:
#创建结构相同的表 create table empDemo as employee; #插入数据 insert into table empDemo select * from employee; #覆盖插入数据 insert overwrite table empDemo select * from employee;
Writing data into the filesystem from queries:
INSERT OVERWRITE LOCAL DIRECTORY ‘./tmp/zhu‘ SELECT * FROM employee;
Inserting values into tables from SQL:
示例代码:
#单行插入 insert into table employee values(‘001‘,‘001‘,‘tgzhu‘); #多行插入 insert into table employee values(‘004‘,‘004‘,‘WangWu‘),(‘005‘,‘005‘,‘ZhaoZhao‘);
Delete:
应用Demo:
CREATE EXTERNAL TABLE if not exists Hive_CM_EvcRealTimeData( Rowkey string, RealTimeData_CarNo string, RealTimeData_Time string, RealTimeData_Speed decimal(20,8), RealTimeData_Mileage decimal(20,8), RealTimeData_HighestVoltageBatteryOrd int, RealTimeData_Latitude decimal(20,8), RealTimeData_Longitude decimal(20,8) ) STORED BY ‘org.apache.hadoop.hive.hbase.HBaseStorageHandler‘ WITH SERDEPROPERTIES(‘hbase.columns.mapping‘ = ‘:key,d:RealTimeData_CarNo, ata_Time,d:RealTimeData_Speed,d:RealTimeData_Mileage,d:RealTimeData_HighestVoltageBatteryOrd,d:RealTimeData_Latitude,d:RealTimeData_Longitude‘) TBLPROPERTIES(‘hbase.table.name‘ = ‘CM_EvcRealTimeData‘)
CREATE TABLE if not exists Hive_CM_CarDailyRpt( CarNo string, DTime string, OnLineCount int, RunCount int, Mileage decimal(20,8), MaxSpeed decimal(20,8), totalPower decimal(20,8), AverageSpeed decimal(20,8), CDI_BatteryFlag string, CDI_CoordinatorFlag string ) STORED AS TEXTFILE
set hive.execution.engine = tez; Insert overwrite table Hive_CM_CarDailyRpt select CarNo,DTime, CONVERT(int,SUM(CT)) as OnLineCount , CONVERT(int,SUM(CTSPEED)) as RunCount, CONVERT(decimal(18,2),MAX(MILE)-MIN(MILE)) as Mileage , CONVERT(decimal(18,2),MAX(SPEED)) as MaxSpeed, ((MAX(MILE)-MIN(MILE))*0.2) as totalPower, case when SUM(CTSPEED)>0 then CONVERT(decimal(18,2),((MAX(MILE)-MIN(MILE))/SUM(CTSPEED))) else 0 end as AverageSpeed, case when SUM(RealTimeData_HighestVoltageBatteryOrd)>0 then ‘0‘ else ‘1‘ end as BatteryFlag, case when (SUM(RealTimeData_Latitude) + SUM(RealTimeData_Longitude)) >0 then ‘0‘ else ‘1‘ end as LatitudeFlag, from ( SELECT REALTIMEDATA_CARNO AS CARNO, substring(RealTimeData_Time,1,8) as DTime, 1 AS CT, CASE WHEN REALTIMEDATA_SPEED>0 THEN 1 ELSE 0 END AS CTSPEED, CASE WHEN REALTIMEDATA_MILEAGE=0 THEN NULL ELSE REALTIMEDATA_MILEAGE END AS MILE, CASE WHEN REALTIMEDATA_SPEED>200 then 0 else REALTIMEDATA_SPEED end AS SPEED, RealTimeData_HighestVoltageBatteryOrd, RealTimeData_Latitude,RealTimeData_Longitude FROM CM_EvcRealTimeData ) t group by CarNo,DTime
标签:
原文地址:http://www.cnblogs.com/tgzhu/p/5773433.html