CREATE DATABASE IF NOT EXISTS DBNAME
DB property 必然是配key value

desc database extended d5_hive_2;
hive查看详细信息
set hive.cli.print.current.db=true;
创建是有用的,切换是有用的
查看主要是看那个目录
生产上100%不使用 cascade
int bigint float double
数据库里面Date Time Type
不好用
生产中 string 《= date time
生产中 各种情况都会出现
如果使用时间戳 如果出现 -
数据就丢了,因此不能使用这个
除了数字之外,其他的全部用string
生产最佳实践
大数据情况下就是用0-1
小数据类型的情况就禁止使用true false
date time 类型 不建议使用
生产中用的最多的是空格 ,制表符\t
中括号里面有括号就需要必须有
换行符用\n 默认就是这个

注意:分割符是一个char 必须是char
必须先将$$$$$替换成char单个字符
create table ruoze_emp(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int)
row format delimited fields terminated by ‘\t’;
导入数据到hive表格
load data local inpath ‘/root/data/emp.txt’ overwrite into table ruoze_emp;

create external table ruoze_emp_external(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int)
row format delimited fields terminated by ‘\t’;

load data local inpath ‘/root/data/emp.txt’ overwrite into table ruoze_emp_2;

> select * from ruoze_emp_2;

OK
NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL

分割符不对 0=1
仅仅copy表结构
create table ruoze_emp_2 like ruoze_emp;
将表结构与数据都copy
create table ruoze_emp_3 as select * from ruoze_emp;

工作中的表非常非常的多,容易出问题
因此使用 show tables like ’ruoze*‘

工作中使用的方式
desc formatted ruoze_emp;
OK

col_name data_type comment

empno int
ename string
job string
mgr int
hiredate string
sal double
comm double
deptno int

Detailed Table Information

Database: default
Owner: root
CreateTime: Thu Nov 29 21:44:48 CST 2018
LastAccessTime: UNKNOWN
Protect Mode: None
Retention: 0
Location: hdfs://hadoop001:9000/user/hive/warehouse/ruoze_emp
Table Type: MANAGED_TABLE
Table Parameters:
COLUMN_STATS_ACCURATE true
numFiles 1
numRows 0
rawDataSize 0
totalSize 700
transient_lastDdlTime 1543499104

Storage Information

SerDe Library: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe
InputFormat: org.apache.hadoop.mapred.TextInputFormat
OutputFormat: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
Compressed: No
Num Buckets: -1
Bucket Columns: []
Sort Columns: []
Storage Desc Params:
field.delim \t
serialization.format \t
Time taken: 0.71 seconds, Fetched: 39 row(s)
hive>
格式化显示表结构

drop table 就是删除文件

非常重要,
内部表和外部表

关键字段 Table Type MANAGED_TABLE
内部表
创建一张内部表
create table ruoze_emp_managed as select * from ruoze_emp;
使用了TBLS解决问题
生产上一定是分库的,不可能放在default
创建了一张内部表都是有数据的
内部表的删除就都删除了
创建一张外部表

记忆这些语法是没有意义的
创建外部表是要指定路径的
写代码需要按照官网的数据进行处理

create external table ruoze_emp_external1(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int)
row format delimited fields terminated by ‘\t’
location ‘/ruoze_emp_external1’;

load data local inpath ‘/root/data/emp.txt’ overwrite into table ruoze_emp_external1;

hive> desc formatted ruoze_emp_external1;
OK

col_name data_type comment

empno int
ename string
job string
mgr int
hiredate string
sal double
comm double
deptno int

Detailed Table Information

Database: default
Owner: root
CreateTime: Thu Nov 29 22:24:39 CST 2018
LastAccessTime: UNKNOWN
Protect Mode: None
Retention: 0
Location: hdfs://hadoop001:9000/ruoze_emp_external1


Table Type: EXTERNAL_TABLE


Table Parameters:
COLUMN_STATS_ACCURATE true
EXTERNAL TRUE
numFiles 0
numRows 0
rawDataSize 0
totalSize 0
transient_lastDdlTime 1543501507

Storage Information

SerDe Library: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe
InputFormat: org.apache.hadoop.mapred.TextInputFormat
OutputFormat: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
Compressed: No
Num Buckets: -1
Bucket Columns: []
Sort Columns: []
Storage Desc Params:
field.delim \t
serialization.format \t
Time taken: 2.084 seconds, Fetched: 40 row(s)
hive>

[root@hadoop001 ~]# hdfs dfs -ls /ruoze_emp_external1
18/11/29 22:29:09 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform… using builtin-java classes where applicable
Found 1 items
-rwxr-xr-x 1 root supergroup 700 2018-11-29 22:25 /ruoze_emp_external1/emp.txt
[root@hadoop001 ~]# hdfs dfs -rm /ruoze_emp_external1/emp.txt
18/11/29 22:29:49 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform… using builtin-java classes where applicable
Deleted /ruoze_emp_external1/emp.txt
[root@hadoop001 ~]# hdfs dfs -ls /ruoze_emp_external1
18/11/29 22:30:21 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform… using builtin-java classes where applicable
[root@hadoop001 ~]# hdfs dfs -put /root/data/emp.txt /ruoze_emp_external1/
18/11/29 22:31:32 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform… using builtin-java classes where applicable
[root@hadoop001 ~]#

hive> select * from ruoze_emp_external1;
OK
Time taken: 4.575 seconds
hive> select * from ruoze_emp_external1;
OK
7369 SMITH CLERK 7902 1980-12-17 800.0 NULL 20
7499 ALLEN SALESMAN 7698 1981-2-20 1600.0 300.0 30
7521 WARD SALESMAN 7698 1981-2-22 1250.0 500.0 30
7566 JONES MANAGER 7839 1981-4-2 2975.0 NULL 20
7654 MARTIN SALESMAN 7698 1981-9-28 1250.0 1400.0 30
7698 BLAKE MANAGER 7839 1981-5-1 2850.0 NULL 30
7782 CLARK MANAGER 7839 1981-6-9 2450.0 NULL 10
7788 SCOTT ANALYST 7566 1987-4-19 3000.0 NULL 20
7839 KING PRESIDENT NULL 1981-11-17 5000.0 NULL 10
7844 TURNER SALESMAN 7698 1981-9-8 1500.0 0.0 30
7876 ADAMS CLERK 7788 1987-5-23 1100.0 NULL 20
7900 JAMES CLERK 7698 1981-12-3 950.0 NULL 30
7902 FORD ANALYST 7566 1981-12-3 3000.0 NULL 20
7934 MILLER CLERK 7782 1982-1-23 1300.0 NULL 10
8888 HIVE PROGRAM 7839 1988-1-23 10300.0 NULL NULL
Time taken: 0.323 seconds, Fetched: 15 row(s)

外部表被删除了,数据还在
删除一张表只会讲元数据删除,location 需要设置,数据是不被删除的
hive.metastore.warehouse.dir 元数据表格

managed by processes outside of Hive 被管理在Hive之外

问题,如果全是数据库数据,存到hive的意义是什么
数据都是一样的情况是存在的
顺序错位的结果是很严重的 注意顺序错位问题
这种错位是最可怕的,语法错了很简单
这种错误,会出现报表信息错误

这种功能不要用,鸡肋,hive是做离线处理的
不要用支持SQL语句部分
进来的数据都是批次,一条在工作中是不使用的
update 事务都是不要用的,没有意义
大批量 批次处理,
数据重跑就可以了,不需要delete
删除容易不知道删除是否错误

合并也不要用了,因为有事务