1.hive的交互方式

  1.1 bin/hive 进入hive交互命令行环境

  1.2 bin/hive -e 'select * from hive.student;' (可以通过 > 将结果写入到指定的文件中)

    

  1.3 bin/hive -f /opt/data/hive-select.sql (可以通过 > 将结果写入到指定的文件中)

    

    

2.hive命令行模式下如何访问hdfs文件系统和本地文件系统

  2.1 访问hdfs文件系统  dfs 命令

    

  2.2 访问本地文件系统 !命令

    

3.表的创建

  3.1 创建普通表存储格式为textfile

    create table if not exists hive.employee (id int,name string,job string,manager_id int comment '上级领导id',apply_date string comment '入职时间',salary double,
      reward double,dept_id int comment '所在部门id') comment '员工表'
      row format delimited
      fields terminated by '\t'
      stored as textfile;

    其中:fields terminated by '\t' 表示字段间以tab分割

       stored as textfile 表示存储格式为textfile(默认)

    

    3.1.2 创建复杂结构表

      create table if not exists cdh_hive.teachers(
        id int,
        name string,
        flag boolean,
        score array<int>,
        tech map<string,string>,
        other struct<phone:string,email:string>
      ) row format delimited
      fields terminated by '\;'
      collection items terminated by ','
      map keys terminated by ':';

      其中:fields terminated by '\;' 表示字段间以分号分割

           collection items terminated by ','  表示集合字段中各个元素间以,分割

             map keys terminated by ':';  表示map字段中键值直接以:分割

      以下是测试数据:

      1;test1;false;100,85,52;age:29,sex:m;\N,\N
      2;test2;true;25,35;\N;021-111111,192165@qq.com
      3;test3;true;\N;age:20,sex:F,height:168;\N,3@qq.com
      4;test4;true;\N;\N;021-44444444,\N
      5;;true;1,2,3;;021-44444444,

      

  3.2 创建外部表(删除表是不删除数据文件)

    create external table if not exists hive.dept(id int,name string,address string) comment '部门表'
      row format delimited
      fields terminated by '\t'
      stored as textfile
      location '/user/yanglin/data/hive/dept';

     其中 external 表示创建的是外部表,默认不写为管理表

      location 表示数据所在hdfs上的目录

    

  3.3 创建分区表

    create external table if not exists hive.dept_partition(id int,name string,address string)
      partitioned by (day string)
      row format delimited
      fields terminated by '\t';

    其中 partitioned by 指定了分区字段,可以指定多个(mouth stirng,day string)

    

    分区表中数据的加载问题:

    3.3.1  load data local inpath '/opt/data/dept.txt' into table dept_partition partition (day='20160901');

      

    3.3.2 直接创建目录,把数据放入到指定的目录中,不能查询到数据

      

      

      需要进行修复:

        修复方式一:msck repair table dept_partition;

          

        修复方式二:alter table dept_partition add partition(day=20160903);

          

  3.4 通过like 语句创建表 create table hive.dept_like like hive.dept;

    

  3.5 通过as select 创建表  create table hive.dept_select as select * from dept;

    

4.加载数据到表中

  4.1 通过load语句进行加载数据

    load data [local] inpath 'filepath' [overwrite] into table tablename [partition (partcol1=val1,partcol2=val2 ...)]

    其中:1、如果数据文件在本地需要local,如果在hdfs上不加local

       2、如果要覆盖表中原有的数据需要 overwrite ,如果在原有数据的基础上添加数据不加 overwrite

       3、如果是分区表需要制定partition 分区字段

  4.2 通过insert into 语句进行加载数据

    insert into table hive.dept_into select * from hive.dept;

  4.3 创建表时通过location 指定数据所在的目录,例3.2

  4.4 通过import语句进行导入

    import table hive.employee_like from '/user/yanglin/data/export/employee';

    

5.数据的导出

  5.1 通过insert 方法将结果导入到指定的文件中

    insert overwrite local directory '/opt/data/hive_dept_exp' select * from dept;

    

    

    我们可以对输出的内容进行格式化:

      insert overwrite local directory '/opt/data/hive_dept_exp_format'

        row format delimited fields terminated by '\t' collection items terminated by '\n'
        select * from dept;

      其中,不加local时表示将内容放入到hdfs文件系统中。

      

  5.2 使用 > 将查询结果放入指定的文件中

      bin/hive -e 'select * from hive.dept;' > /opt/data/hive_dept_exp_e

        

      bin/hive -f /opt/data/hive-select.sql > /opt/data/hive_user_exp_f

        

  5.3 使用sqoop进行数据导入和导出

     在接下来的文章中进行讲解。

  5.4 使用export进行导出

    export table hive.employee to '/user/yanglin/data/export/employee';

    注意:这里的路径为hdfs文件系统上的路径

    

    

hive的使用02的更多相关文章

  1. 单节点伪分布集群(weekend110)的Hive子项目启动顺序

    因为,我的mysql是用root用户,在/home/hadoop/app/目录下,创建的. 第一步:开启mysql服务 第二步:启动hive [hadoop@weekend110 app]$ su r ...

  2. 说说单节点集群里安装hive、3\5节点集群里安装hive的诡异区别

    这几天,无意之间,被这件事情给迷惑,不解!先暂时贴于此,以后再解决! 详细问题如下: 在hive的安装目录下(我这里是 /home/hadoop/app/hive-1.2.1),hive的安装目录的l ...

  3. [Spark][Hive]外部文件导入到Hive的例子

    外部文件导入到Hive的例子: [training@localhost ~]$ cd ~[training@localhost ~]$ pwd/home/training[training@local ...

  4. Hive的单节点集群详细启动步骤

    说在前面的话, 在这里,推荐大家,一定要先去看这篇博客,如下 再谈hive-1.0.0与hive-1.2.1到JDBC编程忽略细节问题 Hadoop Hive概念学习系列之hive三种方式区别和搭建. ...

  5. Caused by: java.net.ConnectException: Connection refused: master/192.168.3.129:7077

    1:启动Spark Shell,spark-shell是Spark自带的交互式Shell程序,方便用户进行交互式编程,用户可以在该命令行下用scala编写spark程序. 启动Spark Shell, ...

  6. hive

    Hive Documentation https://cwiki.apache.org/confluence/display/Hive/Home 2016-12-22  14:52:41 ANTLR  ...

  7. hive学习笔记

    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...

  8. Hive 笔记

    DESCRIBE EXTENDED mydb.employees  DESCRIBE EXTENDED mydb.employees DESCRIBE EXTENDED mydb.employees ...

  9. Hadoop学习笔记—17.Hive框架学习

    一.Hive:一个牛逼的数据仓库 1.1 神马是Hive? Hive 是建立在 Hadoop 基础上的数据仓库基础构架.它提供了一系列的工具,可以用来进行数据提取转化加载(ETL),这是一种可以存储. ...

随机推荐

  1. busybox rootfs 启动脚本分析(二)

    上次分析了busybox的启动脚本,这次分析一下init.d中一些脚本的内容. 参考链接 http://www.cnblogs.com/helloworldtoyou/p/6169678.html h ...

  2. 分布式缓存Memcached---开篇的话

    大数据.高并发这是最近一段时间内被IT行业提的最为火热的概念,看过<大数据时代>的同学应该不会陌生大数据的概念,尤其是对于互联网行业来说,大数据是每天都要接触的问题,简单通俗地说,每天得大 ...

  3. python_excel

    1. xlrd, xlwt, xlutils的关系 Python中一般使用xlrd(excel read)来读取Excel文件,使用xlwt(excel write)来生成Excel文件(可以控制Ex ...

  4. DNS CNAME的一些细节

    1, 概述 DNS中的CNAME可以减轻运维压力,使得已有的DNS配置具有一定的灵活性和可扩展性.本文对CNAME中的一些细节做阐述, 使DNS服务器的运维人员和开发人员能合理地使用CNAME. 2, ...

  5. Response.End抛出ThreadAbortException 异常

    最近在写程序过程中遇到了一个匪夷所思的错误:Response.End()方法抛出了ThreadAbortException异常,我的代码如下: public void doResponse(){ st ...

  6. 在IE下,如果在readonly的input里面键入backspace键,会触发history.back()

    在IE下,如果在readonly的input里面键入backspace键,会触发history.back(), 用以下jQuery代码修正之 $("input[readOnly]" ...

  7. java布局学习 (二)

    前文中介绍了FlowLayout和BorderLayout 本文我们将会继续介绍java中的布局方式 (3)GridLayout 网格布局 这种布局会将整个容器划分成M行*N列的网格. 如下图:    ...

  8. 提供一个表单,进行post数据处理

    var strContent = "aaaaa"; html.Append("<html><body><form id='postUploa ...

  9. oracle优化:避免全表扫描(高水位线)

    如果我们查询了一条SQL语句,这条SQL语句进行了全表扫描,那到底是扫描了多少个数据块呢?是表有多少数据,就扫描多少块吗?不是的.而是扫描高水位线一下的所有块.有的时候有人经常说,我的表也不大呀,怎么 ...

  10. MAC OS UI设计

    对比MAC OS 10.9到10.11的UI设计,苹果曾经卓尔不群的审美观逐步变得泯然众人. 当苹果也跟上扁平化的浪潮,许多搞设计的朋友都一时难以接受,曾经潮流的引领者变成了亦步亦趋的跟随者. MAC ...