目录列表
Hbase伪分布式环境部署见:Hbase环境部署
前置准备
启动hdfs和hbase并验证环境正常
启动服务
通过
shell
命令启动hdfs文件系统和hbase服务,
jps
下出现如下图所示的进程即为正确。
start-all.sh # 启动HDFS文件系统 (datanode, namenode, secondary namenode)
start-hbase.sh # 启动HBase服务 (HMaster, HRegionServer, HQuorumPeer)
jps # 查看当前运行的任务进程
验证运行环境
进入
hbase shell
终端或进入
hbase
提供的web页面(
HBase
默认端口:16030),验证环境正常。
HBase web页面如下图所示
搭建HBase客户端
使用IDEA创建Maven项目
修改
pom.xml
配置文件
为项目添加
hbase-lient
的依赖(刷新完成不飘红即为引入成功)
添加
log4j
输出日志的依赖(刷新完成不飘红即为引入成功)
使用Java API操作HBase
Java连接Hbase
// 静态代码块static{Configuration conf =HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","192.168.159.139");//这里替换成自己Hbase的IP地址try{//创建HBase连接对象
connection =ConnectionFactory.createConnection(conf);}catch(IOException ex){
ex.printStackTrace();}}
创建命名空间
publicstaticvoidcreateNameSpace(String nameSpaceName)throwsIOException{if(nameSpaceName ==null|| nameSpaceName.isEmpty()){
logger.error("namespace 不能为空!");return;}Admin admin = connection.getAdmin();NamespaceDescriptor.Builder builder =NamespaceDescriptor.create(nameSpaceName);NamespaceDescriptor namespaceDescriptor = builder.build();try{
admin.createNamespace(namespaceDescriptor);
logger.info("namespace: {} 创建成功", nameSpaceName);}catch(NamespaceExistException e){
logger.error("namespace 名字已存在!");}}
运行截图:
Hbase
中前后对比:
表的创建和删除
// 表的创建和删除publicstaticvoidcreateTable(){TableName tableName =TableName.valueOf("stu");try(Admin admin = connection.getAdmin()){// 检查表是否存在if(admin.tableExists(tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
logger.info(tableName.toString()+" exists, deleted it.");}// 创建表描述符TableDescriptor tableDescriptor =TableDescriptorBuilder.newBuilder(tableName).setColumnFamily(ColumnFamilyDescriptorBuilder.of("StuInfo")).setColumnFamily(ColumnFamilyDescriptorBuilder.of("Grades")).build();// 创建表
admin.createTable(tableDescriptor);
logger.info("Table "+ tableName +" created successfully.");}catch(IOException e){
logger.error("Error: "+ e.getMessage());
e.printStackTrace();}}
运行截图:
Hbase
中前后对比:
插入数据
// 数据插入publicstaticvoidaddData()throwsIOException{Table table = connection.getTable(TableName.valueOf("stu"));Put put1 =newPut(Bytes.toBytes("row1"));
put1.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("Jack"));
put1.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("18"));
put1.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("90"));
put1.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("85"));
table.put(put1);Put put2 =newPut(Bytes.toBytes("row2"));
put2.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("Alice"));
put2.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("19"));
put2.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("88"));
put2.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("92"));
table.put(put2);Put put3 =newPut(Bytes.toBytes("row3"));
put3.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("Bob"));
put3.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("20"));
put3.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("95"));
put3.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("80"));
table.put(put3);Put put4 =newPut(Bytes.toBytes("row4"));
put4.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("Emma"));
put4.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("21"));
put4.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("82"));
put4.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("90"));
table.put(put4);Put put5 =newPut(Bytes.toBytes("row5"));
put5.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("David"));
put5.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("22"));
put5.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("91"));
put5.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("88"));
table.put(put5);
logger.info("SUCCESS");
table.close();}
运行截图:
Hbase
表中的变化:
数据查询
行键查询
// 数据获取(通过行键查询)publicstaticvoidgetData()throwsIOException{Table table = connection.getTable(TableName.valueOf("stu"));Get get =newGet(Bytes.toBytes("row1"));Result result = table.get(get);for(Cell cell : result.rawCells()){System.out.println(CellUtil.getCellKeyAsString(cell));System.out.println(newString(CellUtil.cloneQualifier(cell)));System.out.println(newString(CellUtil.cloneValue(cell)));System.out.println(cell.getTimestamp());System.out.println("----------------------------");}
table.close();}
运行截图:
全部查询
// 全部查询publicstaticvoidgetDataAll()throwsIOException{Table table = connection.getTable(TableName.valueOf("stu"));Scan scan =newScan();ResultScanner scanner = table.getScanner(scan);for(Result result : scanner){for(Cell cell : result.rawCells()){System.out.println(CellUtil.getCellKeyAsString(cell));System.out.println(newString(CellUtil.cloneQualifier(cell)));System.out.println(newString(CellUtil.cloneValue(cell)));System.out.println(cell.getTimestamp());System.out.println("----------------------------");}}
scanner.close();
table.close();}
运行截图:
特定前缀列查询
// 使用ColumnPrefixFilter过滤器 查询特定列前缀的列publicstaticvoidgetDataWithColumnPrefixFilter()throwsIOException{Table table = connection.getTable(TableName.valueOf("stu"));Filter filter =newColumnPrefixFilter(Bytes.toBytes("na"));Scan scan =newScan();
scan.setFilter(filter);ResultScanner scanner = table.getScanner(scan);for(Result result : scanner){for(Cell cell : result.rawCells()){System.out.println(CellUtil.getCellKeyAsString(cell));System.out.println(newString(CellUtil.cloneQualifier(cell)));System.out.println(newString(CellUtil.cloneValue(cell)));System.out.println(cell.getTimestamp());System.out.println("----------------------------");}}
scanner.close();
table.close();}
运行截图:
完整代码
packageorg.example;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.*;importorg.apache.hadoop.hbase.client.*;importorg.apache.hadoop.hbase.filter.*;importorg.apache.hadoop.hbase.util.Bytes;importorg.apache.logging.log4j.LogManager;importorg.apache.logging.log4j.Logger;importjava.io.IOException;publicclassHBaseDemo{privatestaticConnection connection;privatestaticfinalLogger logger =LogManager.getLogger(HBaseDemo.class);static{Configuration conf =HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","192.168.159.139");try{
connection =ConnectionFactory.createConnection(conf);
logger.info("Hbase 连接成功!");}catch(IOException ex){
logger.error(ex.getMessage(), ex);}}//创建命名空间publicstaticvoidcreateNameSpace(String nameSpaceName)throwsIOException{if(nameSpaceName ==null|| nameSpaceName.isEmpty()){
logger.error("namespace 不能为空!");return;}Admin admin = connection.getAdmin();NamespaceDescriptor.Builder builder =NamespaceDescriptor.create(nameSpaceName);NamespaceDescriptor namespaceDescriptor = builder.build();try{
admin.createNamespace(namespaceDescriptor);
logger.info("namespace: {} 创建成功", nameSpaceName);}catch(NamespaceExistException e){
logger.error("namespace 名字已存在!");}}// 表的创建和删除publicstaticvoidcreateTable(){TableName tableName =TableName.valueOf("stu");try(Admin admin = connection.getAdmin()){// 检查表是否存在if(admin.tableExists(tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
logger.info(tableName.toString()+" exists, deleted it.");}// 创建表描述符TableDescriptor tableDescriptor =TableDescriptorBuilder.newBuilder(tableName).setColumnFamily(ColumnFamilyDescriptorBuilder.of("StuInfo")).setColumnFamily(ColumnFamilyDescriptorBuilder.of("Grades")).build();// 创建表
admin.createTable(tableDescriptor);
logger.info("Table "+ tableName +" created successfully.");}catch(IOException e){
logger.error("Error: "+ e.getMessage());
e.printStackTrace();}}// 数据插入publicstaticvoidaddData()throwsIOException{Table table = connection.getTable(TableName.valueOf("stu"));Put put1 =newPut(Bytes.toBytes("row1"));
put1.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("Jack"));
put1.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("18"));
put1.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("90"));
put1.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("85"));
table.put(put1);Put put2 =newPut(Bytes.toBytes("row2"));
put2.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("Alice"));
put2.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("19"));
put2.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("88"));
put2.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("92"));
table.put(put2);Put put3 =newPut(Bytes.toBytes("row3"));
put3.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("Bob"));
put3.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("20"));
put3.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("95"));
put3.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("80"));
table.put(put3);Put put4 =newPut(Bytes.toBytes("row4"));
put4.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("Emma"));
put4.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("21"));
put4.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("82"));
put4.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("90"));
table.put(put4);Put put5 =newPut(Bytes.toBytes("row5"));
put5.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("David"));
put5.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("22"));
put5.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("91"));
put5.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("88"));
table.put(put5);
logger.info("SUCCESS");
table.close();}// 全部查询publicstaticvoidgetDataAll()throwsIOException{Table table = connection.getTable(TableName.valueOf("stu"));Scan scan =newScan();ResultScanner scanner = table.getScanner(scan);for(Result result : scanner){for(Cell cell : result.rawCells()){System.out.println(CellUtil.getCellKeyAsString(cell));System.out.println(newString(CellUtil.cloneQualifier(cell)));System.out.println(newString(CellUtil.cloneValue(cell)));System.out.println(cell.getTimestamp());System.out.println("----------------------------");}}
scanner.close();
table.close();}// 数据获取 - 行键publicstaticvoidgetDataRow()throwsIOException{Table table = connection.getTable(TableName.valueOf("stu"));Get get =newGet(Bytes.toBytes("row1"));Result result = table.get(get);for(Cell cell : result.rawCells()){System.out.println(CellUtil.getCellKeyAsString(cell));System.out.println(newString(CellUtil.cloneQualifier(cell)));System.out.println(newString(CellUtil.cloneValue(cell)));System.out.println(cell.getTimestamp());System.out.println("----------------------------");}
table.close();}// 使用ColumnPrefixFilter过滤器 查询特定列前缀的列publicstaticvoidgetDataWithColumnPrefixFilter()throwsIOException{Table table = connection.getTable(TableName.valueOf("stu"));Filter filter =newColumnPrefixFilter(Bytes.toBytes("na"));Scan scan =newScan();
scan.setFilter(filter);ResultScanner scanner = table.getScanner(scan);for(Result result : scanner){for(Cell cell : result.rawCells()){System.out.println(CellUtil.getCellKeyAsString(cell));System.out.println(newString(CellUtil.cloneQualifier(cell)));System.out.println(newString(CellUtil.cloneValue(cell)));System.out.println(cell.getTimestamp());System.out.println("----------------------------");}}
scanner.close();
table.close();}publicstaticvoidmain(String[] args)throwsIOException{// 创建命名空间// createNameSpace("test");// 表的创建和删除// createTable();// 数据插入// addData();// 数据获取// getDataRow();// 全部查询// getDataAll();// 使用PrefixFilter过滤器查询// getDataWithColumnPrefixFilter();}}
@鲨鱼爱兜兜
版权归原作者 鲨鱼爱兜兜 所有, 如有侵权,请联系我们删除。