spark +hive+hbase+hadoop

前言:

hive:数据仓库
hbase:分布式nosql数据库
haoop:分布式文件系统
spark:分布式内存计算系统
这个四个结合(当然少不了zookeeper)简直完美。当然有完美就有不和谐,
各组件缺点如下:
1. hive的底层计算是mapreduce,计算太慢
2. hbase 是一个nosql数据库,对sql用户不友好。
3. spark计算结果是内存中,最终是要落地的。
4. hadoop 只是一个文件系统。
组件结合在一起就起到了化学反应。
1. hive计算慢 被spark sql解决,spark sql完全支持hql,只用到hive的metastore。
2. hbase对sql不友好,hive 的hql解决,将hbase列表映射到hive表。
3. spark 的计算结果直接存在hive中
4. hbase 和hive都在hadoop 上的服务。

整合spark +hive+hbase+hadoop

前提:以上4个组件已经成功安装,如果没有安装,建议使用ambari。
下面将4个组件进行整合,然后进行表的创建、查询、插入、删除等基础操作。
以数据分析引擎为例子:

  1. 启动spark 的thriftserver服务
    spark sql 已经把hive2进行重写,然后同样提供了thriftserver服务。这样可以方便第三方应用可以通过jdbc/odbc,如同操作mysql数据库一样,进行数据分析
root/spark-2.1.1-bin-hadoop2.7/sbin/./start-thriftserver.sh --master yarn --deploy-mode client --queue offline
 --jars /root/apache-hive-2.3.0-bin/lib/hive-hbase-handler-2.3.0.jar,/root/hbase-1.3.1/lib/hbase-protocol-1.3.1.jar,/root/hbase-1.3.1/lib/hbase-server-1.3.1.jar,/root/hbase-1.3.1/lib/hbase-hadoop2-compat-1.3.1.jar,/root/hbase-1.3.1/lib/hbase-hadoop-compat-1.3.1.jar,/root/hbase-1.3.1/lib/hbase-client-1.3.1.jar,/root/hbase-1.3.1/lib/hbase-common-1.3.1.jar,/root/apache-hive-2.3.0-bin/lib/zookeeper-3.4.6.jar,/root/apache-hive-2.3.0-bin/lib/guava-14.0.1.jar,/root/hbase-1.3.1/lib/metrics-core-2.2.0.jar
  1. 启动beeline
    因为是在同一台启动的,所以使用的localhost
 ${spark_home}/bin/./beeline

 beeline>!connect jdbc:hive2//localhost:10000
Enter username for jdbc:hive2://localhost:10000: hive
Enter password for jdbc:hive2://localhost:10000: ****
INFO Utils: Supplied authorities: localhost:10000
INFO Utils: Resolved authority: localhost:10000
INFO HiveConnection: Will try to open client transport with JDBC Uri: jdbc:hive2://localhost:10000
Connected to: Spark SQL (version 2.1.1)
Driver: Hive JDBC (version 1.2.1.spark2)
Transaction isolation: TRANSACTION_REPEATABLE_READ

经过认证后,成功登陆

基础操作

详见:HPL

创建hive表

  1. 创建hive内部表(create internal tables)
create table test(name String ,age Int);

desc test;//查询表结构
+-----------+------------+----------+--+
| col_name  | data_type  | comment  |
+-----------+------------+----------+--+
| name      | string     | NULL     |
| age       | int        | NULL     |
+-----------+------------+----------+--+

2.创建hive外部表(create extenral tables)

Hive storage handler is not supported yet when creating table, you can create a table using storage handler at Hive side, and use Spark SQL to read it.
目前创建表,还不支持Hive storage handler,但是你能使用hive创建以后,使用spark sql 读取它。 

但是我还是尝试了一下,

create table test2(name String ,age Int) 
ROW FORMAT SERDE
  'org.apache.hadoop.hive.hbase.HBaseSerDe'
WITH SERDEPROPERTIES (
  'hbase.columns.mapping'=':key,cf1:age'
)
TBLPROPERTIES('storage_handler'='org.apache.hadoop.hive.hbase.HBaseStorageHandler')
stored as inputformat "org.apache.hadoop.hive.hbase.HiveHBaseTableInputFormat" outputformat "org.apache.hadoop.hive.hbase.HiveHBaseTableOutputFormat";

虽然创建成功,但是插入数据的时候没有成功,出现异常:

java.lang.ClassCastException: org.apache.hadoop.hive.hbase.HiveHBaseTableOutputFormat cannot be cast to org.apache.hadoop.hive.ql.io.HiveOutputFormat

然后在github上面看了spark提交的bug,提到已经解决了,但是按照他的方法,并没有成功。

https://github.com/apache/spark/pull/17989

2.查询表
首先在hive上创建

create table lecture1(name string,age int)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
with serdeproperties('hbase.columns.mapping'=':key,cf1:age')
tblproperties('hbase.table.name'='lecture1');

select * from lecture1;
+----------------+---------------+--+
| lecture1.name  | lecture1.age  |
+----------------+---------------+--+
| 123            | 123           |
+----------------+---------------+--+

spark -sql

select * from lecture1;
+-------+------+--+
| name  | age  |
+-------+------+--+
| 123   | 123  |
+-------+------+--+

3.插入数据

nsert into people values("123",123);
elect * from people;
+-------+------+--+
| name  | age  |
+-------+------+--+
| 123   | 123  |
+-------+------+--+

(0.509 seconds)–spark sql
(14.234 seconds)–mapreduce

Error: org.apache.spark.sql.AnalysisException: org.apache.hadoop.hive.ql.metadata.HiveException: Unable to move source hdfs://valuka-engine-1:8020/apps/hive/warehouse/people/.hive-staging_hive_2018-01-01_02-19-44_822_3162211435950410821-18/-ext-10000/part-00000 to destination hdfs://valuka-engine-1:8020/apps/hive/warehouse/people/part-00000_copy_2; (state=,code=0)

解决方案:
hive-site中添加属性
<property>
  <name>hive.exec.stagingdir</name>
  <value>/tmp/hive/spark-${user.name}</value>
</property>

4.删除表

drop table people;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值