一、hive创建临时表并插入csv

1、建表

drop table if exists tmp.dwd_df;
create table tmp.dwd_df as
select * from  
dwd.dwd_df
where etl_date = 20230112 and 1=2
;

CREATE TABLE tmp.dm_df(
froms string,
tolist string,
createdatetime string)
row format delimited fields terminated by ',' lines terminated by '\n' stored as textfile;

2、指定分隔符,同时使用GBK防止中文乱码

ALTER TABLE tmp.dm_df SET SERDEPROPERTIES ('field.delim' = ',', 'serialization.format'=',','serialization.encoding'='GBK');

3、导入数据(执行前对hdfs文件增加读写权限)

LOAD DATA  INPATH '/user/2-1-1-dm_df.csv' overwrite  INTO TABLE tmp.dm_df;

二、hive 查找函数并查看函数使用方法

1.查看month 相关的函数

show functions like '*month*'

输出如下:
add_months
dayofmonth
month
months_between
2.查看 add_months 函数的用法

desc function add_months;

add_months(start_date, num_months) - Returns the date that is num_months after start_date.
3.查看 add_months 函数的详细说明并举例

desc function extended add_months;

SELECT add_months(‘2009-08-31’, 1) FROM src LIMIT 1;
‘2009-09-30’;

三、hive 通过application 查看任务

yarn application -list    --查看运行的任务
yarn application -status <application_id>  --查看指定任务状态
yarn application -kill <application_id>  -- kill任务

四、hive按照指定格式输出字符串或数值

语法和功能与C语言的 printf 类似,常用于格式化查询结果(如日期、数字保留小数位等)

select printf('%d1%s2%s3%s4%s至%s',1,'2','3','4','5','6') --112233445至6
printf(format_string, arg1, arg2, ...)

format_string:格式控制符,用于定义输出格式(如%d表示整数、%.2f表示保留2位小数的浮点数)。
arg1, arg2…;:待格式化的参数,数量和类型需与format_string中的控制符匹配。

更多推荐