HBaseAPI——IDEA操作HBase数据库HBase与Hive的集成
创始人
2024-05-30 15:40:43
0

目录

一、IDEA操作HBase数据库

(一)添加依赖

(二)配置log4j

(三)IDEA连接HBase并插入数据

1.代码实现

2.查看命名空间的表

(四)java操作HBase数据库——单元测试

1.导包

2.初始化

3.关闭连接

4.创建命名空间

5.创建表

6.删除命名空间下的指定表  

7.查看所有的命名空间

8.往表中新增数据

9.get查询数据

10.全表扫描

二、HBase与 Hive 的集成

(一)停止hive服务并配置hive-site.xml

(二)将HBase的lib目录下所有的文件复制到Hive的lib目录下

(三)不覆盖路径复制

(四)删除HBase/lib目录下低版本的guava

(五)继续配置hive-site.xml

(六)重新启动Hive

(七)HQL创建表

1.student表

2.kb21_student表

3.car表


一、IDEA操作HBase数据库

(一)添加依赖

    org.apache.hbasehbase-client2.3.5org.apache.hbasehbase-server2.3.5

(二)配置log4j

hadoop.root.logger=DEBUG, console
log4j.rootLogger = ERROR, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n

(三)IDEA连接HBase并插入数据

1.代码实现

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;public static void main( String[] args ){// 配置HBase信息,连接HBase数据库Configuration conf = new Configuration();// 2. 添加配置参数conf.set(HConstants.HBASE_DIR,"hdfs://lxm147:9000/hbase");conf.set(HConstants.ZOOKEEPER_QUORUM,"lxm147");conf.set(HConstants.CLIENT_PORT_STR,"2181");try {Connection conn = ConnectionFactory.createConnection(conf);System.out.println(conn);// hconnection-0x55182842Table stuTb = conn.getTable(TableName.valueOf("bigdata:student"));// 打印表名System.out.println(stuTb.getName());// bigdata:student// 插入数据Put put = new Put(Bytes.toBytes("rowkey11"));put.addColumn("baseinfo".getBytes(),"name".getBytes(),"zhangsan".getBytes());put.addColumn("baseinfo".getBytes(),"age".getBytes(),"20".getBytes());put.addColumn("baseinfo".getBytes(),"birthday".getBytes(),"2003-01-01".getBytes());put.addColumn("schoolinfo".getBytes(),"schoolname".getBytes(),"清华大学".getBytes());put.addColumn("schoolinfo".getBytes(),"address".getBytes(),"北京".getBytes());stuTb.put(put);conn.close();} catch (IOException e) {e.printStackTrace();}}

2.查看命名空间的表

插入成功

(四)java操作HBase数据库——单元测试

1.导包

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;import static org.junit.Assert.assertTrue;

2.初始化

public class AppTest {static Configuration config = null;private Connection conn = null;private Admin admin = null;@Beforepublic void init() throws Exception {System.out.println("执行init()方法");config = HBaseConfiguration.create();config.set(HConstants.HBASE_DIR, "hdfs://lxm147:9000/hbase");config.set(HConstants.ZOOKEEPER_QUORUM, "lxm147");config.set(HConstants.CLIENT_PORT_STR, "2181");conn = ConnectionFactory.createConnection(config);admin = conn.getAdmin();}@Testpublic void test1() {System.out.println(conn);System.out.println("执行test1方法");}
}

3.关闭连接

 @Afterpublic void close() throws IOException {System.out.println("执行close()方法");if (admin != null)admin.close();if (conn != null)conn.close();}

4.创建命名空间

    @Testpublic void createNameSpace() throws IOException {NamespaceDescriptor kb21 = NamespaceDescriptor.create("kb21").build();try {admin.createNamespace(kb21);} catch (IOException e) {e.printStackTrace();}}

5.创建表

    @Testpublic void createTable() throws IOException {// 创建表的描述类TableName tableName = TableName.valueOf("kb21:student");HTableDescriptor desc = new HTableDescriptor(tableName);// 创建列族的描述类HColumnDescriptor family1 = new HColumnDescriptor("info1");HColumnDescriptor family2 = new HColumnDescriptor("info2");desc.addFamily(family1);desc.addFamily(family2);admin.createTable(desc);}

6.删除命名空间下的指定表  

    @Testpublic void deleteTable() throws IOException {admin.disableTable(TableName.valueOf("kb21:student"));admin.deleteTable(TableName.valueOf("kb21:student"));}

7.查看所有的命名空间

    @Testpublic void getAllNamespace() throws IOException {String[] nps = admin.listNamespaces();String s = Arrays.toString(nps);System.out.println(s);// 获取命名空间下的表List tableDesc = admin.listTableDescriptorsByNamespace("kb21".getBytes());System.out.println(tableDesc.toString());}

8.往表中新增数据

    @Testpublic void insertData() throws IOException {Table table = conn.getTable(TableName.valueOf("kb21:student"));Put put = new Put(Bytes.toBytes("student1"));put.addColumn("info1".getBytes(), "name".getBytes(), "zs".getBytes());put.addColumn("info2".getBytes(), "school".getBytes(), "fudan".getBytes());table.put(put);Put put2 = new Put(Bytes.toBytes("student2"));put2.addColumn("info1".getBytes(), "name".getBytes(), "zs111".getBytes());put2.addColumn("info2".getBytes(), "school".getBytes(), "fudan111".getBytes());Put put3 = new Put(Bytes.toBytes("student3"));put3.addColumn("info1".getBytes(), "name".getBytes(), "zs222".getBytes());put3.addColumn("info2".getBytes(), "school".getBytes(), "fudan222".getBytes());List list = new ArrayList<>();list.add(put2);list.add(put3);table.put(list);}

9.get查询数据

    @Testpublic void queryData() throws IOException {Table table = conn.getTable(TableName.valueOf("kb21:student"));Get get = new Get(Bytes.toBytes("student1"));Result result = table.get(get);byte[] value = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));System.out.println("姓名" + Bytes.toString(value));value = result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("school"));System.out.println("学校" + Bytes.toString(value));}

10.全表扫描

    @Testpublic void scanData() throws IOException {Table table = conn.getTable(TableName.valueOf("kb21:student"));Scan scan = new Scan();ResultScanner scanner = table.getScanner(scan);for (Result result :scanner) {// 打印表名System.out.println(Bytes.toString(result.getRow()));// 打印表数据byte[] value = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));System.out.println("姓名" + Bytes.toString(value));value = result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("school"));System.out.println("学校" + Bytes.toString(value));System.out.println("-----------------------");           }}

二、HBase与 Hive 的集成

(一)停止hive服务并配置hive-site.xml

[root@lxm147 ~]# vim /opt/soft/hive312/conf/hive-site.xml hive.zookeeper.quorum192.168.180.147

hbase.zookeeper.quorum192.168.180.147

(二)将HBase的lib目录下所有的文件复制到Hive的lib目录下

[root@lxm147 conf]# cp /opt/soft/hbase235/lib/* /opt/soft/hive312/lib/

(三)不覆盖路径复制

(四)删除HBase/lib目录下低版本的guava

如果hive的lib目录下有两个guava,需要删除低版本guava
rm -rf /opt/soft/hive312/lib/guava-11.0.2.jar将Hive/lib的guava复制到HBase/lib目录下
cp /opt/soft/hive312/lib/guava-27.0-jre.jar /opt/soft/hbase235/lib

(五)继续配置hive-site.xml

[root@lxm147 ~]# vim /opt/soft/hive312/conf/hive-site.xml hive.aux.jars.pathfile:///opt/soft/hive312/lib/hive-hbase-handler-3.1.2.jar,file:///opt/soft/hive312/lib/zookeeper-3.4.6.jar,file:///opt/soft/hive312/lib/hbase-client-2.3.5.jar,file:///opt/soft/hive312/lib/hbase-common-2.3.5-tests.jar,file:///opt/soft/hive312/lib/hbase-server-2.3.5.jar,file:///opt/soft/hive312/lib/hbase-common-2.3.5.jar,file:///opt/soft/hive312/lib/hbase-protocol-2.3.5.jar,file:///opt/soft/hive312/lib/htrace-core-3.2.0-incubating.jar

(六)重新启动Hive

刷新DataGrip,不报错就说明HBase映射到Hive成功!

(七)HQL创建表

1.student表

use default;
drop table if exists student;
create external table student(id string,name string,age int,birthday string,schoolname string,address string
)stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with
serdeproperties ("hbase.columns.mapping"=":key,baseinfo:name,baseinfo:age,baseinfo:birthday,schoolinfo:schoolname,schoolinfo:address")
tblproperties ("hbase.table.name"="bigdata:student");select * from student;

2.kb21_student表

drop table if exists kb21_student;
create external table kb21_student(id string,name string,school string
)stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with
serdeproperties ("hbase.columns.mapping"=":key,info1:name,info2:school")
tblproperties ("hbase.table.name"="kb21:student");select * from kb21_student;

3.car表

Xshell端

create 'bigdata:car','product'put 'bigdata:car','product1','product:name','宝马'
put 'bigdata:car','product1','product:info','x1'
put 'bigdata:car','product1','product:address','北京'put 'bigdata:car','product2','product:name','奔驰'
put 'bigdata:car','product2','product:info','x2'                                                
put 'bigdata:car','product2','product:address','南京'put 'bigdata:car','product3','product:name','夏利'
put 'bigdata:car','product3','product:info','x3'               
put 'bigdata:car','product3','product:address','上海' scan 'bigdata:car'
ROW                     COLUMN+CELL                                                      product1               column=product:address, timestamp=2023-03-08T14:23:46.731, value=\xE5\x8C\x97\xE4\xBA\xACproduct1               column=product:info, timestamp=2023-03-08T14:18:12.235, value=x1 product1               column=product:name, timestamp=2023-03-08T14:22:49.590, value=\xE5\xAE\x9D\xE9\xA9\xACproduct2               column=product:address, timestamp=2023-03-08T14:23:58.666, value=\xE5\x8D\x97\xE4\xBA\xACproduct2               column=product:info, timestamp=2023-03-08T14:18:34.899, value=x2 product2               column=product:name, timestamp=2023-03-08T14:23:13.930, value=\xE5\xA5\x94\xE9\xA9\xB0product3               column=product:address, timestamp=2023-03-08T14:24:16.804, value=\xE4\xB8\x8A\xE6\xB5\xB7product3               column=product:info, timestamp=2023-03-08T14:19:12.294, value=x3 product3               column=product:name, timestamp=2023-03-08T14:19:03.873, value=\xE5\xA4\x8F\xE5\x88\xA9

DataGrip中建表

create database bigdata;
USE bigdata;drop table if exists car;
create external table car(id string,name string,info string,address string
)stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with
serdeproperties ("hbase.columns.mapping"=":key,product:name,product:info,product:address")
tblproperties ("hbase.table.name"="bigdata:car");select * from car;

相关内容

热门资讯

杭州西湖导游词 杭州西湖导游词500字范文  杭州西湖的导游词怎么写好呢?下面是由应届毕业生小编为大家带来的关于杭州...
平遥古城导游词 平遥古城导游词平遥古城,历史悠久。据载:西周时期周宣工为抵御北方游牧民族的侵扰,曾派兵北伐萨犹,并修...
云南民族村的导游词 云南民族村的导游词  作为一名乐于为游客排忧解难的导游,有必要进行细致的导游词准备工作,导游词是讲解...
京北第一草原的导游词 京北第一草原的导游词  导读:京北第一草原是距首都北京最近的天然草原,故名“京北第一草原”,因其美丽...
秦皇岛联峰山导游词 秦皇岛联峰山导游词3篇  作为一位杰出的导游,通常会被要求编写导游词,导游词具有极强的实用性,涉及的...
车溪民俗导游词 车溪民俗导游词4篇  作为一名优秀的导游,时常需要编写导游词,导游词由引言、主体和结语三部分构成。那...
石宝寨导游词 石宝寨导游词范文  坐落在重庆市忠县长江北岸,西距县城40公里,东距万县52公里。此处临江有一俯高十...
长江三峡大瀑布的导游词 长江三峡大瀑布的导游词  关于三峡大瀑布导游词篇一  大家好,一路辛苦了,欢迎来到水电之都——宜昌,...
汉中武侯祠导游词 汉中武侯祠导游词  作为一位兢兢业业的旅游从业人员,编写导游词是必不可少的,一篇完整的导游词,其结构...
泰州国清寺导游词 泰州国清寺导游词(精选6篇)  作为一无名无私奉献的导游,有必要进行细致的导游词准备工作,导游词的主...
天津独乐寺导游词参考 天津独乐寺导游词参考  独乐寺位于蓟县城武定北侧,它始建于天宝十一年,辽代重建。关于独乐寺的得名,这...
清远英西峰林走廊导游词 清远英西峰林走廊导游词  英西峰林走廊位于英德市区西南60多公里的九龙、明迳、岩背三镇一带,这里密集...
家乡南京导游词-导游词 家乡南京导游词-导游词范文  作为一名尽职尽责的导游,就不得不需要编写导游词,导游词一般是根据实际的...
古漪园的导游词 古漪园的导游词范文  各位游客,大家好,欢迎大家来到古漪园参观,我是金凤旅游公司的66号导游,我叫刘...
颐和园景点导游词 颐和园景点导游词(精选6篇)  作为一位杰出的导游,时常需要编写导游词,导游词是我们引导游览时使用的...
天水南郭寺导游词 天水南郭寺导游词  南郭寺位于甘肃天水市秦州区南两公里处龙王沟东侧的慧音山坳,被誉为“天水第一名刹”...
山东崂山东麓华严寺导游词 山东崂山东麓华严寺导游词  作为一位兢兢业业的旅游从业人员,通常需要准备好一份导游词,导游词具有形象...
西安古城墙简单导游词 西安古城墙简单导游词  西安明城墙位于陕西省西安市中心区,墙高12米,顶宽12—14米,底宽15—1...
介绍贵州百里杜鹃的导游词 关于介绍贵州百里杜鹃的导游词范文(精选7篇)  作为一名专门为游客提供帮助的导游,通常需要用到导游词...
太原汾河公园导游词资料 太原汾河公园导游词资料  在公园两岸带状绿化平台上分布着4个主题广场、6个自然景区和7个观光景点。沿...