1、使用HiveServer2及Beeline

  HiveServer2的作用:将hive变成一种server服务对外开放,多个客户端可以连接。

  启动namenode、datanode、resourcemanager、nodemanager。

  一个窗口输入:hive-0.13.1]$ bin/hiveserver2 启动hiveserver2服务,等效于:$ bin/hive --service hiveserver2

  第二个窗口输入:~]$ ps -ef | grep java 查看hiveserver2进程

  第二个窗口输入:hive-0.13.1]$ bin/beeline 启动beeline

  第二个窗口输入:beeline> !connect jdbc:hive2://hadoop-senior.ibeifeng.com:10000 beifeng beifeng org.apache.hive.jdbc.HiveDriver 将beeline连接hiveserver2服务

  hiveserver2的默认端口号:10000,临时修改hiveserver2的端口号:$ bin/hiveserver2 --hiveconf hive.server2.thrift.port=14000

  使用beeline:

  0: jdbc:hive2://hadoop-senior.ibeifeng.com:10> show databases;

  0: jdbc:hive2://hadoop-senior.ibeifeng.com:10> use default;

  0: jdbc:hive2://hadoop-senior.ibeifeng.com:10> show tables;

  0: jdbc:hive2://hadoop-senior.ibeifeng.com:10> select * from emp; 不使用mapreduce

  0: jdbc:hive2://hadoop-senior.ibeifeng.com:10> select empno from emp; 使用mapreduce

  [beifeng@hadoop-senior hive-0.13.1]$ bin/beeline -u jdbc:hive2://hadoop-senior.ibeifeng.com:10000/default

  直接连接hiveserver2进入beeline的default数据库

  $ !connect jdbc:hive2://bigdata-senior01.ibeifeng.com:10000

  $ bin/beeline -u jdbc:hive2://bigdata-senior01.ibeifeng.com:10000 -n beifeng -p 123456

  u:代表链接的意思

  $ bin/beeline --help:通过帮助命令获取常用选项参数

  HiveServer2 JDBC的使用:

  将分析的结果存储在hive表中,前端通过DAO代码,进行数据的查询。但是HiveServer2的并发存在问题,需要做并发处理。使用JDBC连接之前一定要先启动HiveServer2。

  Hive使用JDBC格式:

  Class.forName("org.apache.hive.jdbc.HiveDriver");

  Connection conn = DriverManager.getConnection("jdbc:hive2://:","","");

  2、Hive运行配置

  Shell命令行临时生效:

  set hive.fetch.task.conversion;

  hive.fetch.task.conversion=minimal

  hive-site.xml配置文件生效:

  hive.fetch.task.conversion

  minimal

  Some select queries can be converted to single FETCH task minimizing latency.

  Currently the query should be single sourced not having any subquery and should not have

  any aggregations or distincts (which incurs RS), lateral views and joins.

  1. minimal : SELECT STAR, FILTER on partition columns, LIMIT only

  2. more : SELECT, FILTER, LIMIT only (TABLESAMPLE, virtual columns)

  3、虚拟列

  One is 【INPUT__FILE__NAME】, which is the input file's name for a mapper task.

  the other is 【BLOCK__OFFSET__INSIDE__FILE】, which is the current global file position.

  【INPUT__FILE__NAME】代表这行数据属于哪个文件中的:

  select deptno,dname,INPUT__FILE__NAME from dept;

  【BLOCK__OFFSET__INSIDE__FILE】代表块的偏移量:

  select deptno,dname,BLOCK__OFFSET__INSIDE__FILE from dept;

  这两个虚拟列的格式都需要两个下划线。

  4、安装snappy数据压缩格式

  (1)安装snappy:下载snappy安装包,并解压安装。

  snappy安装包的下载地址:http://google.github.io/snappy/。

  (2)编译haodop 2.x源码:

  mvn package -Pdist,native -DskipTests -Dtar -Drequire.snappy /opt/modules/hadoop-2.5.0-src/target/hadoop-2.5.0/lib/native

  [beifeng@hadoop-senior hadoop-2.5.0]$ bin/hadoop checknative

  15/08/31 23:10:16 INFO bzip2.Bzip2Factory: Successfully loaded & initialized native-bzip2 library system-native

  15/08/31 23:10:16 INFO zlib.ZlibFactory: Successfully loaded & initialized native-zlib library

  Native library checking:

  hadoop: true /opt/modules/hadoop-2.5.0/lib/native/libhadoop.so

  zlib: true /lib64/libz.so.1

  snappy: true /opt/modules/hadoop-2.5.0/lib/native/libsnappy.so.1

  lz4: true revision:99

  bzip2: true /lib64/libbz2.so.1

  用安装好的snappy压缩格式运行mapreduce程序wordcount。

  bin/yarn jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.5.0.jar wordcount /user/beifeng/mapreduce/wordcount/input /user/beifeng/mapreduce/wordcount/output bin/yarn jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.5.0.jar wordcount -Dmapreduce.map.output.compress=true -Dmapreduce.map.output.compress.codec=org.apache.hadoop.io.compress.SnappyCodec /user/beifeng/mapreduce/wordcount/input /user/beifeng/mapreduce/wordcount/output2

  5、Hive中的数据压缩

  压缩格式:bzip2,gzip,lzo,snappy等

  压缩比:bzip2>gzip>lzo bzip2最节省存储空间

  解压速度:lzo>gzip>bzip2 lzo解压速度是最快的

  数据压缩的好处:

  (1)节省了磁盘IO(map输出压缩)和网络传输IO(reduce输出压缩)。

  (2)数据大小变小。

  (3)任务运行的性能提高(由于任务大小小了)。

  (4)必须考虑压缩后的文件可拆分,即压缩后的每一个任务分片可独立运行。

  MapReduce过程中的数据压缩与解压缩:

  Hadoop中支持的压缩格式:

  压缩格式  压缩格式所在的类

  Zlib  org.apache.hadoop.io.compress.DefaultCodec

  Gzip  org.apache.hadoop.io.compress.GzipCodec

  Bzip2  org.apache.hadoop.io.compress.BZip2Codec

  Lzo  com.hadoop.compression.lzo.LzoCodec

  Lz4  org.apache.hadoop.io.compress.Lz4Codec

  Snappy  org.apache.hadoop.io.compress.SnappyCodec

  MapReduce压缩的属性设置:

  压缩的用法  在core-site.xml中设置的属性

  Map 输出设置  mapreduce.map.output.compress = True;

  mapreduce.map.output.compress.codec = CodecName;

  Reducer 输出设置  mapreduce.output.fileoutputformat.compress = True;

  mapreduce.output.fileoutputformat.compress.codec = CodecName;

  MapReduce配置Snappy压缩运行示例:

  bin/yarn jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.5.0.jar wordcount -Dmapreduce.map.output.compress=true -Dmapreduce.map.output.compress.codec=org.apache.hadoop.io.compress.SnappyCodec /user/beifeng/mr/input /user/beifeng/mr/output2

  Hive压缩的属性设置:

  hive.exec.compress.intermediate

  true

  压缩的用法  新建表时定义的属性

  Map 输出设置  SET hive.exec.compress.intermediate = True;

  SET mapreduce.map.output.compress=true

  SET mapred.map.output.compression.codec = CodecName;

  SET mapred.map.output.compression.type = BLOCK/RECORD;

  Reducer 输出设置  SET hive.exec.compress.output = True;

  SET mapred.output.compression.codec = CodecName;

  SET mapred.output.compression.type = BLOCK/RECORD;

  6、数据文件存储格式

  file_format:

  : SEQUENCEFILE

  | TEXTFILE -- (Default, depending on hive.default.fileformat configuration)

  | RCFILE -- (Note: Available in Hive 0.6.0 and later)

  | ORC -- (Note: Available in Hive 0.11.0 and later)

  | PARQUET -- (Note: Available in Hive 0.13.0 and later)

  | AVRO -- (Note: Available in Hive 0.14.0 and later)

  | INPUTFORMAT input_format_classname OUTPUTFORMAT output_format_classname

  数据存储格式分为按行存储数据和按列存储数据。

  (1)ORCFile(Optimized Row Columnar File):hive/shark/spark支持。使用ORCFile格式存储列数较多的表。

  (2)Parquet(twitter+cloudera开源,被Hive、Spark、Drill、Impala、Pig等支持)。Parquet比较复杂,其灵感主要来自于dremel,parquet存储结构的主要亮点是支持嵌套数据结构以及高效且种类丰富算法(以应对不同值分布特征的压缩)。

  (1)存储为TEXTFILE格式

  create table page_views(

  track_time string,

  url string,

  session_id string,

  referer string,

  ip string,

  end_user_id string,

  city_id string

  )

  ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'

  STORED AS TEXTFILE ;

  load data local inpath '/opt/datas/page_views.data' into table page_views ;

  dfs -du -h /user/hive/warehouse/page_views/ ;

  18.1 M /user/hive/warehouse/page_views/page_views.data

  (2)存储为ORC格式

  create table page_views_orc(

  track_time string,

  url string,

  session_id string,

  referer string,

  ip string,

  end_user_id string,

  city_id string

  )

  ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'

  STORED AS orc ;

  insert into table page_views_orc select * from page_views ;

  dfs -du -h /user/hive/warehouse/page_views_orc/ ;

  2.6 M /user/hive/warehouse/page_views_orc/000000_0

  (3)存储为Parquet格式

  create table page_views_parquet(

  track_time string,

  url string,

  session_id string,

  referer string,

  ip string,

  end_user_id string,

  city_id string

  )

  ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'

  STORED AS PARQUET ;

  insert into table page_views_parquet select * from page_views ;

  dfs -du -h /user/hive/warehouse/page_views_parquet/ ;

  13.1 M /user/hive/warehouse/page_views_parquet/000000_0

  (4)存储为ORC格式,使用snappy压缩

  create table page_views_orc_snappy(

  track_time string,

  url string,

  session_id string,

  referer string,

  ip string,

  end_user_id string,

  city_id string

  )无锡人流医院哪家好 http://www.wxbhnkyy120.com/

  ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'

  STORED AS orc tblproperties ("orc.compress"="SNAPPY");

  insert into table page_views_orc_snappy select * from page_views ;

  dfs -du -h /user/hive/warehouse/page_views_orc_snappy/ ;

  (5)存储为ORC格式,不使用压缩

  create table page_views_orc_none(

  track_time string,

  url string,

  session_id string,

  referer string,

  ip string,

  end_user_id string,

  city_id string

  )

  ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'

  STORED AS orc tblproperties ("orc.compress"="NONE");

  insert into table page_views_orc_none select * from page_views ;

  dfs -du -h /user/hive/warehouse/page_views_orc_none/ ;

  (6)存储为Parquet格式,使用snappy压缩

  set parquet.compression=SNAPPY ;

  create table page_views_parquet_snappy(

  track_time string,

  url string,

  session_id string,

  referer string,

  ip string,

  end_user_id string,

  city_id string

  )

  ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'

  STORED AS parquet;

  insert into table page_views_parquet_snappy select * from page_views ;

  dfs -du -h /user/hive/warehouse/page_views_parquet_snappy/ ;

  在实际的项目开发当中,hive表的数据的存储格式一般使用orcfile / parquet,数据压缩一般使用snappy压缩格式。

Hive中的HiveServer2、Beeline及数据的压缩和存储的更多相关文章

  1. hive中导入json格式的数据(hive分区表)

    hive中建立外部分区表,外部数据格式是json的如何导入呢? json格式的数据表不必含有分区字段,只需要在hdfs目录结构中体现出分区就可以了 This is all according to t ...

  2. hive中同列多行数据组合的方法以及array to string要点(行转列)

    1. 同列多行数据组合成一个字段cell的方法, top N 问题的hive方案 如下: hive 列转行 to json与to array list set等复杂结构,hive topN的提取的窗口 ...

  3. hive中使用union出现异常数据

    select * from tbl where id=2 union select * from tbl where id =1 如果hive使用union这么查询的时候,我们会发现数据变乱了. 解决 ...

  4. Hive中典型的表内数据除重写法

    insert overwrite table store select t.p_key,t.sort_word from ( select p_key, sort_word , row_number( ...

  5. 在Hive中使用Avro

    作者:过往记忆 | 新浪微博:左手牵右手TEL | 可以转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明博客地址:http://www.iteblog.com/文章标题:<在Hiv ...

  6. sqoop导oracle数据到hive中并动态分区

    静态分区: 在hive中创建表可以使用hql脚本: test.hql USE TEST; CREATE TABLE page_view(viewTime INT, userid BIGINT, pag ...

  7. hive中rcfile格式(收藏文)

    首先声明,此文是属于纯粹收藏文,感觉讲的很不错. 本文介绍了Facebook公司数据分析系统中的RCFile存储结构,该结构集行存储和列存储的优点于一身,在MapReduce环境下的大规模数据分析中扮 ...

  8. Hive 中日志的存放位置

    目前hive启动无法成功,想查看下hive的日志定位问题,但发现hive的安装目录下并没有hive的日志,后来经过在网上谷歌发现: Hive中的日志分为两种 系统日志,记录了hive的运行情况,错误状 ...

  9. SparkSQL读取Hive中的数据

    由于我Spark采用的是Cloudera公司的CDH,并且安装的时候是在线自动安装和部署的集群.最近在学习SparkSQL,看到SparkSQL on HIVE.下面主要是介绍一下如何通过SparkS ...

随机推荐

  1. Xamarin.FormsShell基础教程(9)Shell相关类体系

    Xamarin.FormsShell基础教程(9)Shell相关类体系 在Shell中,最为主要的类是Shell类.Shell类实现了大多数应用程序所需的基本UI功能的页面.除此以外,常用的类还有Sh ...

  2. Sword 正则表达式

    Metacharacters(元字符) 在正则表达式中有一些具有特殊含义的字母,被称为元字符,简言之,元字符就是描述字符的字符,它用于对字符表达式的内容.转换及各种操作信息进行描述. \ 将下一个字符 ...

  3. 【KakaJSON手册】08_其他用法

    除了完成JSON和Model的转换之外,KakaJSON内部还有很多实用的功能,有些也开放为public接口了 遍历属性 struct Cat { var age: Int = 0 let name: ...

  4. nexus 3.x最新版下载安装和上传下载jar

    注意: nexus 3.x最新版好像不用下载索引了,目前我使用一些基本功能没有索引也能耍的很6 下载 nexus最新版下载https://www.sonatype.com/download-oss-s ...

  5. Spring MVC -- 单元测试和集成测试

    测试在软件开发中的重要性不言而喻.测试的主要目的是尽早发现错误,最好是在代码开发的同时.逻辑上认为,错误发现的越早,修复的成本越低.如果在编程中发现错误,可以立即更改代码:如果软件发布后,客户发现错误 ...

  6. OPMS是什么?

    OPMS OPMS项目+OA管理系统 OPMS管理系统是意思是PMS+OA,项目+办公管理.符合日常项目和OA管理,特别适合扁平化管理的微中小企业. OPMS采用是Beego框架和Bootstrap前 ...

  7. 问题解决: 此文件来自其他计算机,可能被阻止以帮助保护该计算机/WORD在试图打开文件时遇到错误……

    最近,在打开下载的office文档(包括word.excel.ppt等)时候,总是无法直接打开,错误提示如下: 无论是邮件中的还是别的网站下载的,均提示该错误.后来搜索相关资料发现,修改其文件属性即可 ...

  8. 【Docker学习之一】初始Docker

    一.云计算的概念 PaaS(Platform-as-a-Service:平台即服务),把应用服务的运行和开发环境作为一种服务.SaaS(Software-as-a-Service),意思为软件即服务, ...

  9. (CSDN 迁移) jFinal找不到或无法加载主类

    错误: 找不到或无法加载主类 com.demo.common.DemoConfig 项目上右键 -> Build Path -> Order and Export 修改顺序: 从上到下依次 ...

  10. phpredis封装

    <pre><?php/** * This is a Redis exntend class */ class RedisClient{ private static $instanc ...