目录
一、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表
org.apache.hbase hbase-client 2.3.5 org.apache.hbase hbase-server 2.3.5
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
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();}}
插入成功
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;
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方法");}
}
@Afterpublic void close() throws IOException {System.out.println("执行close()方法");if (admin != null)admin.close();if (conn != null)conn.close();}
@Testpublic void createNameSpace() throws IOException {NamespaceDescriptor kb21 = NamespaceDescriptor.create("kb21").build();try {admin.createNamespace(kb21);} catch (IOException e) {e.printStackTrace();}}
@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);}
@Testpublic void deleteTable() throws IOException {admin.disableTable(TableName.valueOf("kb21:student"));admin.deleteTable(TableName.valueOf("kb21:student"));}
@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());}
@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);}
@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));}
@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("-----------------------"); }}
[root@lxm147 ~]# vim /opt/soft/hive312/conf/hive-site.xml hive.zookeeper.quorum 192.168.180.147
hbase.zookeeper.quorum 192.168.180.147
[root@lxm147 conf]# cp /opt/soft/hbase235/lib/* /opt/soft/hive312/lib/
如果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
[root@lxm147 ~]# vim /opt/soft/hive312/conf/hive-site.xml hive.aux.jars.path file:///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
刷新DataGrip,不报错就说明HBase映射到Hive成功!
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;
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;
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;
上一篇:C. Serval and Toxel‘s Arrays
下一篇:CI流水线的理解