//Linux文件转码

iconv PhoneInfo.txt  -f GBK -t UTF-8  -o  data.txt

//查看表的结构

desc 表名 desc extends 表名 desc formatted 表名

//查询数据

select id,name,sex,age,department from student;

//表中加入数据

load data local(从本地加载) inpath "文件位置" into table 表名

数据库的基本操作:
 

 DDL: define 定义   DML:   mamager 管理   DQL:    query 查询 

一.关于库的
    1、创建库
   

 create database hadoop if no exists hadoop;  show databases;

查询库列表信息
    2、查询正在使用的库 
    

select current_database();

3、切换库
    

use hadoop;

 4、查询库的详细信息
  

 desc database hadoop;

 5、删除库
  

 drop database hive;(库名)     注: 数据库中有表不能删除

  6、全删除包括库中的所有表 使用此命令  
    

drop database [库名] cascade

二.关于表的
    1、创建表
   

 create table [If no exists]表名      [(col_name data_type) [Comment col_comment]]     # 字段名 数据类型  列名注释     [COMMENT table_comment]     [PARTITIONED BY (col_name data_type [Comment col_comment])]     #分区 通过字段名 数据类型 切记:不能和表的字段名同名     [CLUSTERED BY (col_name,col_name,.....)]     #分桶字段可以有多个

总结:
    a、创建分区表的时候 分区字段
    b、创建分桶表的时候,分桶字段,必须是表字段中的一部分

  

[row format row_format]

2、#指定行列分隔符,指定解析数据的格式和方式
    

row format delimited fields terminated by "|"     [STOPRED AS file_format]     file_format:     textfile 普通的文本文件格式     sequencefile 序列化文件     rcfile行列存储结合的文件     默认的就是 textfile.

    3、创建表的时候,可以指定表的路径不管是外部表还是内部表 [LOCATION hdfs_path]
    最佳实践:
    内部表:如果一份数据已经存储在Hdfs,并且被多个         用户或客户端所使用,最好创建内部表
    外部表:当一份数据需要被多种工具分析时如Pig,        Hive,意味着这份数据的所有权并不由Hive数据所拥有.
    如果不指定,就按照默认的规则存储在默认的仓库

    4、复制表
   

 create table student_1 like student;     注:只复制表的定义,不复制表的数据

    5、清空表

truncate table [表名]

6、删除表  

 drop table [表名]

三、实战6道例题
    1.创建内部表

create table if no exists test ()row format delimited fields terminated by "|"

 2.创建外部表
    首先指定外部表路径

create external table student_exit(id int ,name string)row format delimited fields terminated by "|" location "/user/hive/student";

3.1创建分区表
    

create external table student_ptn(id int ,name string) partitioned by ( city string) row format delimited fields terminated by "|" location "/user/hive/student";

 3.2添加分区

alter table student_ptn add patition(city="city");可以的多个分区字段

   如果某一张表是分区表,那么每个分区的定义,其实就是表现为了这张表的数据存储目录下的一个子目录。
    如果是分区表,那么数据一定要存储在某个分中,而不能直接存储在表中。
    3.3查看表的分区命令
 

 show  partitions  student_ptn; 

3.4创建动态分区表
    (1)创建一张带分区的表

 create table student_ptn  (id int,name string)  partitioned by (sex string)  row format delimited fields terminated by "\t";

(2)一张源数据表
   

 id name sex      01 甲回 男     (3)开启动态分区     set hive.exec.dynamic.partition=true     set hive.exec.dynamic.partition.mode=nonstrict     insert overwrite table home_ptn partition(sex)     select id,name from data_s;

 4.创建分通表
    

create table stedent_bck(id int ,name string) clustered by (id)      sorted by (id asc,name desc) into 4 buckets      row format delimited fields terminated by "|";     clustered by : 指定划分桶所用的列。     sorted by : 对桶中的一个或多个列另外排序

5.使用CTAS创建表
    作用:就是从一个查询sql的结果来创建一个表进行存储。
    

create table student_cats as select* from student where id <10;     create table student_cats as select 字段(name) from student where id <10;

 6.1复制表(结构)
    

create table student_copy like student; 只复制 student 表的结构。

    (1)如果在table的前面没有加 external关键字 复制出来的是内部表。
    (2)如果在table的前面加了 external关键字 复制出来的是外部表。
    6.2查看表列表 
   

 show tables;  show tables in [表名]  show table like 's*'(查询表名中有s字母的) [表名]    

6.3查看表的详细结构 
  

 desc [表名] :查看表的列信息     desc formatted 表名     desc formatted student;     第二种 不常用 show extends [表名]      show partitions [表名] :查看分区表的结构

    6.4修改表
    

alter table student rename to new_student;     修改字段定义:

    6.5增加一个字段
    

alter table student{表名} add columns(sex string , age int )     //执行会在表的第一列添加了一个名为columns2 INT型的字段。     alter table student{表名}add columns2 int(10)first 

6.6修改一个字段的定义
   

 alter table student change age new_age string;     alter table movies change movieid  movieid string;

6.7删除一个表的字段
   

 create table tbl_data     (user_name string ,user_age string,user_city string,To_destination string,To_time string)     row format delimited fields terminated by "|";

6.8替换所有字段
    

alter table student replace(id int,name string); 字段名的数据类型 一致 否则出错。(hive-1.2.2版本无任何影响)

  6.9修改分区信息:
 
   a、添加分区:
   

 alter table student_ptn add partition(city="chongqing1") partition(city="chongqing2")     partition(city="chongqing3");

 b、添加一个或多个分区。
    修改分区 一般指修改分区的存储目录
 

   alter table student_ptn add if no exists partition(part='bb')location /"home/student" 

     删除分区
    

alter table student_ptn drop partition(city="beijing");

   清空表 

 truncate table student;

删除表
   

 drop table student;

四、DML:
    1.导入数据
    a、load 方式导入数据
    从Linux本地导入数据

load data local inpath "/home/data" into table student;

b、从HDFS导入数据
    

load data  inpath "/home/data" into table student;