创建表:
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;