0
点赞
收藏
分享

微信扫一扫

微服务learning

三维控件研究 04-13 22:00 阅读 0

hive语句操作

        我这个只涉及到hive的对表的操作,包括建表,建分区表,加载数据,导出数据,查询数据,删除数据,插入数据,以及对hive分区表的操作,包括查看分区,添加分区,分区名修改,分区数据修改,删除分区等。

        复杂的聚合函数以及常用的炸裂函数的应用,行转列在这是没有体现出来的,后边有时间结合业务我再来写吧。

        基本我都是在hue上对hive进行操作,仅限于查查表,改改数据,而复杂的处理逻辑基本是使用spark写算法,调sparksql,或者sparkcore来实现的。

        这个整理的文章算一个笔记索引吧,忘了再来看看

时间:20240409

hive创建表

CREATE TABLE IF NOT EXISTS my_table (
  id INT COMMENT '唯一标识符',
  name STRING COMMENT '姓名',
  age INT COMMENT '年龄',
  salary DOUBLE COMMENT '薪水',
  address STRING COMMENT '地址'
)
COMMENT '示例表'
PARTITIONED BY (year INT COMMENT '年份', month INT COMMENT '月份')
STORED AS PARQUET;

hive查看建表语句

SHOW CREATE TABLE my_table;

hive复制表结构

-- hive建表结构
-- 创建一个新表,并命名为new_table
-- 使用SELECT语句从现有表existing_table中选择所有数据,并将其作为新表的内容,
-- 由于只是想复制现有表的结构,而不复制数据,使用WHERE子句中的条件1=0,确保不复制任何记录
CREATE TABLE new_table
AS
SELECT *
FROM existing_table
WHERE 1=0;

hive创建分区表

--hive创建一级分区表
create table if not exists part1(
id int,
name string
)
partitioned by (dt string) row format delimited fields terminated by ' ';

hive创建分区别及加载数据

--建表
create table if not exists part2(
id int,
name string
)
partitioned by (year int,month int) row format delimited fields terminated by ' ';
--hive加载数据
load data local inpath '/home/hivedata/t1' overwrite into  table part2 partition(year=2019,month=9);
load data local inpath '/home/hivedata/t' overwrite into  table part2 partition(year=2019,month=10);

hive导出数据

--hive导出数据
--使用Hive的INSERT OVERWRITE语句将数据导出到HDFS目录:
INSERT OVERWRITE DIRECTORY '/path/to/output'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
SELECT *
FROM your_table;

hive查询数据

--hive查询数据的逻辑及使用顺序
SELECT columns
FROM table_name
WHERE condition
GROUP BY columns
HAVING condition
ORDER BY columns
LIMIT n;

hive向某表插入数据

--hive插入数据
-- 向employees表插入一条新员工记录
INSERT INTO TABLE employees
VALUES (1, 'John Doe', 6000);

hive更新某表某条数据

--hive更新数据
-- 将工资低于5000的员工工资增加10%
UPDATE employees
SET salary = salary * 1.1
WHERE salary < 5000;

hive删除数据

--hive删除数据
-- 删除employees表中离职员工的记录
DELETE FROM employees
WHERE status = '离职';

hive分区操作

hive查看表分区

--hive查看分区
show partitions 表名;

hive添加分区

alter table part1 add partition(dt='2019-09-10');
alter table part1 add partition(dt='2019-09-13') partition(dt='2019-09-12');
alter table part1 add partition(dt='2019-09-11') location  '/user/hive/warehouse/qf1704.db/part1/dt=2019-09';

 hive分区名称修改

alter table part1 partition(dt='2019-09-10') rename to partition(dt='2019-09-14');
ALTER TABLE table1 PARTITION (dt='2023-01-01') RENAME TO PARTITION (dt='2024-04-01');

修改hive分区数据

        其实我整个博客的核心就是这块,前边的不过是顺手整理的,因为实际中有一个需求就是将hive某时间分区下的数据改了。emmmmm~

--修改Hive表中分区字段为'2024-04-01'且字段'a1'为'2021-01-01'的数据,将'a1'中数据为'2021-01-01'改为'2022-01-01'
UPDATE table1 SET a1 = '2022-01-01' WHERE dt = '2024-04-01' AND a1 = '2021-01-01';

hive删除分区

--hive删除分区
alter table part1 drop partition(dt='2019-09-14');
alter table part1 drop partition(dt='2019-09-12'),partition(dt='2019-09-13');
举报

相关推荐

0 条评论