- 创建表(条件之间不能有逗号 fields terminated by ',' 字段之间以逗号分隔)
0: jdbc:hive2://localhost:10000/cr> create table if not exists teacher(id int,name string, age int) comment 'teacher' row format delimited fields terminated by ',' stored as textfil e;
No rows affected (17.918 seconds)
- 查看表结构
0: jdbc:hive2://localhost:10000/cr> desc teacher;
+-----------+------------+----------+
| col_name | data_type | comment |
+-----------+------------+----------+
| id | int | |
| name | string | |
| age | int | |
+-----------+------------+----------+
3 rows selected (0.531 seconds)
- 查看表结构详细信息
0: jdbc:hive2://localhost:10000/cr> desc formatted teacher;
+-------------------------------+----------------------------------------------------+-----------------------------+
| col_name | data_type | comment |
+-------------------------------+----------------------------------------------------+-----------------------------+
| # col_name | data_type | comment |
| | NULL | NULL |
| id | int | |
| name | string | |
| age | int | |
| | NULL | NULL |
| # Detailed Table Information | NULL | NULL |
| Database: | cr | NULL |
| Owner: | anonymous #匿名 | NULL |
| CreateTime: | Mon Aug 13 08:09:15 EDT 2018 | NULL |
| LastAccessTime: | UNKNOWN | NULL |
| Retention: | 0 | NULL |
| Location: | hdfs://mycluster/user/hive/warehouse/cr.db/teacher | NULL |
| Table Type: | MANAGED_TABLE #默认托管表 | NULL |
| Table Parameters: | NULL | NULL |
| | COLUMN_STATS_ACCURATE | {"BASIC_STATS":"true"} |
| | comment | teacher |
| | numFiles | 1 |
| | numRows | 1 |
| | rawDataSize | 9 |
| | totalSize | 10 |
| | transient_lastDdlTime | 1534162714 |
| | NULL | NULL |
| # Storage Information | NULL | NULL |
| SerDe Library: | org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe | NULL |
| InputFormat: | org.apache.hadoop.mapred.TextInputFormat | NULL |
| OutputFormat: | org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat | NULL |
| Compressed: | No | NULL |
| Num Buckets: | -1 | NULL |
| Bucket Columns: | [] | NULL |
| Sort Columns: | [] | NULL |
| Storage Desc Params: | NULL | NULL |
| | field.delim #字段分隔符 | , |
| | serialization.format | , |
+-------------------------------+----------------------------------------------------+-----------------------------+
34 rows selected (0.315 seconds)
- 加载表数据
exp1:从本地加载表数据
0: jdbc:hive2://localhost:10000/cr> load data local inpath '/home/xiaoqiu/teacher.txt' into table teacher ;
No rows affected (3.079 seconds)
0: jdbc:hive2://localhost:10000/cr>
查看hdfs的目录
[xiaoqiu@s150 /home/xiaoqiu]$ hadoop fs -lsr /
drwxr-xr-x - anonymous supergroup 0 2018-08-13 09:01 /user/hive/warehouse/cr.db /teacher
-rwxr-xr-x 3 anonymous supergroup 10 2018-08-13 08:18 /user/hive/warehouse/cr.db /teacher/000000_0
-rwxr-xr-x 3 anonymous supergroup 150 2018-08-13 09:01 /user/hive/warehouse/cr.db /teacher/teacher.txt
exp2:从hdfs加载表数据 (相当于剪切,从HDFS加载数据之后,HDFS的目录里面没有teacher.txt)
[xiaoqiu@s150 /home/xiaoqiu]$ hadoop fs -put teacher.txt /user/hive/warehouse #上传至HDFS目录
#从HDFS目录加载数据
0: jdbc:hive2://localhost:10000/cr> load data inpath '/user/hive/warehouse/teacher.txt' into table house;
- 复制表的数据和结构
0: jdbc:hive2://localhost:10000/cr> create table student_1 as select * from student;
- 复制表的结构
0: jdbc:hive2://localhost:10000/cr> create table teacher_1 like teacher;
- 聚集函数查询要转为MapReduce进行分组统计,如count() 、order by、group by
0: jdbc:hive2://localhost:10000/cr> select count(*) total from teacher;
WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
+--------+
| total |
+--------+
| 14 |
+--------+
- 创建分区表
0: jdbc:hive2://localhost:10000/cr> create table if not exists teacher_2(id int,name string,age int) partitioned by( year int,month int) row format delimited fields terminated by ',' stored as textfile;
No rows affected (1.301 seconds)
- 添加分区,创建目录
0: jdbc:hive2://localhost:10000/cr> alter table teacher_2 add partition (year=2018,month=06) partition(year=2018,month=08);
0: jdbc:hive2://localhost:10000/cr> alter table teacher_2 add partition (year=2018,month=07);
- 显示分区信息
0: jdbc:hive2://localhost:10000/cr> show partitions teacher_2;
+--------------------+
| partition |
+--------------------+
| year=2018/month=6 |
| year=2018/month=7 |
| year=2018/month=8 |
+--------------------+
- 加载数据到某个分区
0: jdbc:hive2://localhost:10000/cr> load data local inpath '/home/xiaoqiu/teacher.txt' into table teacher_2 partition(year=2018,month=6);
No rows affected (3.196 seconds)
0: jdbc:hive2://localhost:10000/cr> select * from teacher_2;
+---------------+-----------------+----------------+-----------------+------------------+
| teacher_2.id | teacher_2.name | teacher_2.age | teacher_2.year | teacher_2.month |
+---------------+-----------------+----------------+-----------------+------------------+
| 2 | 'bom1' | 24 | 2018 | 6 |
| 3 | 'rom1' | 25 | 2018 | 6 |
| 4 | 'som1' | 26 | 2018 | 6 |
| 5 | 'tom10' | 23 | 2018 | 6 |
| 6 | 'tom9' | 20 | 2018 | 6 |
| 7 | 'tom8' | 11 | 2018 | 6 |
| 8 | 'tom7' | 12 | 2018 | 6 |
| 9 | 'tom6' | 13 | 2018 | 6 |
| 10 | 'tom5' | 33 | 2018 | 6 |
| 12 | 'tom4' | 45 | 2018 | 6 |
| 22 | 'tom3' | 20 | 2018 | 6 |
| 32 | 'tom2' | 23 | 2018 | 6 |
| NULL | NULL | NULL | 2018 | 6 |
+---------------+-----------------+----------------+-----------------+------------------+
13 rows selected (4.417 seconds)