zoukankan      html  css  js  c++  java
  • hive创建表-加载表数据-创建分区表

    • 创建表(条件之间不能有逗号  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)
    
    欢迎关注我的公众号:小秋的博客 CSDN博客:https://blog.csdn.net/xiaoqiu_cr github:https://github.com/crr121 联系邮箱:rongchen633@gmail.com 有什么问题可以给我留言噢~
  • 相关阅读:
    数据库同步软件介绍以及使用说明(SyncNavigator多元异构数据实时同步工具)
    关于异构数据库的不同表之间数据同步的操作细节---syncnavigator同步工具实操
    ASP.NET Core 配置文件(无处不在的依赖注入)
    ASP.NET Core 开源项目整理
    性能差异 ASP.NET WebForm与ASP.NET MVC
    MySQL 分区知识点(三)
    Docker 资料
    MySQL 基础知识(基本架构、存储引擎差异)
    MySQL InnoDB与MyISAM存储引擎差异
    MySQL 索引知识整理(创建高性能的索引)
  • 原文地址:https://www.cnblogs.com/flyingcr/p/10326864.html
Copyright © 2011-2022 走看看