Hive基本操作

本文详细介绍Hive SQL的基本操作,包括表的创建、数据导入、表结构调整等关键步骤,适用于初学者快速掌握Hive数据仓库的基础使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建表:
create table person(id bigint,name string,age bigint) row format delimited fields terminated by ",";

导入数据:
load data local inpath '/home/wql/app/hData/person.txt' into table person;
load data inpath 'hdfs://wql-pc:9000/person.txt' into table person;

spark操作hive
wql@wql-pc:~$ spark-sql --master spark://wql-pc:7077 --total-executor-cores 2 --executor-memory 2g --total-executor-cores 2 --driver-class-path /home/wql/app/spark/spark-2.2.2-bin-hadoop2.7/mysql-connector-java-5.1.32.jar

查看数据库:
show databases like 'db_hive*'

查看数据库信息:
desc database spark;

删除数据库:
cascade:表示级联删除
drop database if exists spark cascade;


创建表:
create table if not exists default.student(id int,name string) row format delimited fields terminated by "\t";

创建表2:
CREATE TABLE IF NOT EXISTS default.access_log_20190301(
ip string,username string,req_url string COMMENT 'user request url') COMMENT 'test web access log' ROW FORMAT DELIMITED FIELDS TERMINATED BY ' '
STORED AS TEXTFILE;
LOCATION '/user/hive/warehouse/access_log_20190301'

创建表3:
CREATE TABLE IF NOT EXISTS default.access_log_20190228 AS select ip,username from default.access_log_20190301;

创建表4:
CREATE TABLE IF NOT EXISTS default.access_log_20190227 like default.access_log_20190301;

数据准备:
"27.38.5.159" "-" "31/Aug/2015:00:04:37 +0800"
"27.38.5.159" "-" "31/Aug/2015:00:04:37 +0800"
"27.38.5.159" "-" "31/Aug/2015:00:04:53 +0800"
"27.38.5.159" "-" "31/Aug/2015:00:04:53 +0800"


查看表的具体信息:
desc formatted student;

导入数据:
load data local inpath '/home/wql/app/hData/hiveData.txt' into table access_log_20190301;

在hive命令行操作文件系统:
hive (default)> dfs -ls /user/hive ;

hive常见几种交互操作:
1:hive -e 'select * from student'
2:hive -f /home/wql/app/hData/hsql.sql
3:hive -i <filename>

hive执行shell命令:
hive (default)> !ls -al;

数据类型:

data_type
  : primitive_type
  | array_type
  | map_type
  | struct_type
  | union_type
 
primitive_type
  : TINYINT
  | SMALLINT
  | INT
  | BIGINT
  | BOOLEAN
  | FLOAT
  | DOUBLE
  | DOUBLE PRECISION
  | STRING
  | BINARY      
  | TIMESTAMP   
  | DECIMAL     
  | DECIMAL(precision, scale)  
  | DATE        
  | VARCHAR     
  | CHAR
 
array_type
  : ARRAY < data_type >
 
map_type
  : MAP < primitive_type, data_type >
 
struct_type
  : STRUCT < col_name : data_type [COMMENT col_comment], ...>
 
union_type
   : UNIONTYPE < data_type, data_type, ... >

实例:
员工表:
CREATE TABLE IF NOT EXISTS default.emp(
empno int,ename string,job string,mgr int,hiredate string,sal double,comm double,deptno int) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

部门表:
CREATE TABLE IF NOT EXISTS default.dept(
deptno int,dname string,loc string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

导入数据:
load data local inpath '/home/wql/app/hData/emp.txt' overwrite into table emp;
load data local inpath '/home/wql/app/hData/dept.txt' overwrite into table dept;

创建一个子表
create table if not exists default.dept_cats
as
select * from dept

清除表中的数据
truncate table dept_cats;

create table if not exists default.dept_like like dept;

修改表名
alter table dept_like rename to dept_like_rename;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值