hbase - java

来源:趣味经验馆 5.76K

<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>

hbase java是什么,让我们一起了解一下?

HBase是一个分布式的、面向列的开源数据库,具有高可靠性、高性能、面向列、可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群。

如何使用JAVA语言操作Hbase、整合Hbase?

可分为五步骤:

步骤1:新创建一个Java Project 。

步骤2:导入JAR包,在工程根目录下新建一个“lib”文件夹,将官方文档中的lib目录下的jar全部导入。

步骤3:修改开发机的hosts文件,在文件莫为增加一行虚拟机IP的映射信息。

步骤4:修改虚拟机的配置文件,修改虚拟机的设备名称,名称需要与之前两个配置文件的映射名称一致。

步骤5:实现查询、新建、删除等。

hbase java

案例代码展示如下:

package hbase;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.Admin;import org.apache.hadoop.hbase.client.Connection;import org.apache.hadoop.hbase.client.ConnectionFactory;import org.apache.hadoop.hbase.client.Delete;import org.apache.hadoop.hbase.client.Get;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.client.Result;import org.apache.hadoop.hbase.client.ResultScanner;import org.apache.hadoop.hbase.client.Scan;import org.apache.hadoop.hbase.client.Table;import org.apache.hadoop.hbase.exceptions.DeserializationException;import org.apache.hadoop.hbase.filter.Filter;import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;import org.apache.hadoop.hbase.util.Bytes;import org.junit.Before;import org.junit.Test;public class HBaseDemo {// 与HBase数据库的连接对象Connection connection;// 数据库元数据操作对象Admin admin;@Beforepublic void setUp() throws Exception {// 取得一个数据库连接的配置参数对象Configuration conf = HBaseConfiguration.create();// 设置连接参数:HBase数据库所在的主机IPconf.set("hbase.zookeeper.quorum", "192.168.137.13");// 设置连接参数:HBase数据库使用的端口conf.set("hbase.zookeeper.property.clientPort", "2181");// 取得一个数据库连接对象connection = ConnectionFactory.createConnection(conf);// 取得一个数据库元数据操作对象admin = connection.getAdmin();}/**    * 创建表    */public void createTable() throws IOException{System.out.println("---------------创建表 START-----------------");// 数据表表名String tableNameString = "t_book";// 新建一个数据表表名对象TableName tableName = TableName.valueOf(tableNameString);// 如果需要新建的表已经存在if(admin.tableExists(tableName)){System.out.println("表已经存在!");}// 如果需要新建的表不存在else{// 数据表描述对象HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);// 列族描述对象HColumnDescriptor family= new HColumnDescriptor("base");;// 在数据表中新建一个列族hTableDescriptor.addFamily(family);// 新建数据表admin.createTable(hTableDescriptor);}System.out.println("---------------创建表 END-----------------");}/**    * 查询整表数据    */@Testpublic void queryTable() throws IOException{System.out.println("---------------查询整表数据 START-----------------");// 取得数据表对象Table table = connection.getTable(TableName.valueOf("t_book"));// 取得表中所有数据ResultScanner scanner = table.getScanner(new Scan());// 循环输出表中的数据for (Result result : scanner) {byte[] row = result.getRow();System.out.println("row key is:" + new String(row));List<Cell> listCells = result.listCells();for (Cell cell : listCells) {byte[] familyArray = cell.getFamilyArray();byte[] qualifierArray = cell.getQualifierArray();byte[] valueArray = cell.getValueArray();System.out.println("row value is:" + new String(familyArray) + new String(qualifierArray)+ new String(valueArray));}}System.out.println("---------------查询整表数据 END-----------------");}/**    * 按行键查询表数据    */@Testpublic void queryTableByRowKey() throws IOException{System.out.println("---------------按行键查询表数据 START-----------------");// 取得数据表对象Table table = connection.getTable(TableName.valueOf("t_book"));// 新建一个查询对象作为查询条件Get get = new Get("row8".getBytes());// 按行键查询数据Result result = table.get(get);byte[] row = result.getRow();System.out.println("row key is:" + new String(row));List<Cell> listCells = result.listCells();for (Cell cell : listCells) {byte[] familyArray = cell.getFamilyArray();byte[] qualifierArray = cell.getQualifierArray();byte[] valueArray = cell.getValueArray();System.out.println("row value is:" + new String(familyArray) + new String(qualifierArray)+ new String(valueArray));}System.out.println("---------------按行键查询表数据 END-----------------");}/**    * 按条件查询表数据    */@Testpublic void queryTableByCondition() throws IOException{System.out.println("---------------按条件查询表数据 START-----------------");// 取得数据表对象Table table = connection.getTable(TableName.valueOf("t_book"));// 创建一个查询过滤器Filter filter = new SingleColumnValueFilter(Bytes.toBytes("base"), Bytes.toBytes("name"),CompareOp.EQUAL, Bytes.toBytes("bookName6"));// 创建一个数据表扫描器Scan scan = new Scan();// 将查询过滤器加入到数据表扫描器对象scan.setFilter(filter);// 执行查询操作,并取得查询结果ResultScanner scanner = table.getScanner(scan);// 循环输出查询结果for (Result result : scanner) {byte[] row = result.getRow();System.out.println("row key is:" + new String(row));List<Cell> listCells = result.listCells();for (Cell cell : listCells) {byte[] familyArray = cell.getFamilyArray();byte[] qualifierArray = cell.getQualifierArray();byte[] valueArray = cell.getValueArray();System.out.println("row value is:" + new String(familyArray) + new String(qualifierArray)+ new String(valueArray));}}System.out.println("---------------按条件查询表数据 END-----------------");}/**    * 清空表    */@Testpublic void truncateTable() throws IOException{System.out.println("---------------清空表 START-----------------");// 取得目标数据表的表名对象TableName tableName = TableName.valueOf("t_book");// 设置表状态为无效admin.disableTable(tableName);// 清空指定表的数据admin.truncateTable(tableName, true);System.out.println("---------------清空表 End-----------------");}/**    * 删除表    */@Testpublic void deleteTable() throws IOException{System.out.println("---------------删除表 START-----------------");// 设置表状态为无效admin.disableTable(TableName.valueOf("t_book"));// 删除指定的数据表admin.deleteTable(TableName.valueOf("t_book"));System.out.println("---------------删除表 End-----------------");}/**    * 删除行    */@Testpublic void deleteByRowKey() throws IOException{System.out.println("---------------删除行 START-----------------");// 取得待操作的数据表对象Table table = connection.getTable(TableName.valueOf("t_book"));// 创建删除条件对象Delete delete = new Delete(Bytes.toBytes("row2"));// 执行删除操作table.delete(delete);System.out.println("---------------删除行 End-----------------");}/**    * 删除行(按条件)    */@Testpublic void deleteByCondition() throws IOException, DeserializationException{System.out.println("---------------删除行(按条件) START-----------------");// 步骤1:调用queryTableByCondition()方法取得需要删除的数据列表// 步骤2:循环步骤1的查询结果,对每个结果调用deleteByRowKey()方法System.out.println("---------------删除行(按条件) End-----------------");}/**    * 新建列族    */@Testpublic void addColumnFamily() throws IOException{System.out.println("---------------新建列族 START-----------------");// 取得目标数据表的表名对象TableName tableName = TableName.valueOf("t_book");// 创建列族对象HColumnDescriptor columnDescriptor = new HColumnDescriptor("more");// 将新创建的列族添加到指定的数据表admin.addColumn(tableName, columnDescriptor);System.out.println("---------------新建列族 END-----------------");}/**    * 删除列族    */@Testpublic void deleteColumnFamily() throws IOException{System.out.println("---------------删除列族 START-----------------");// 取得目标数据表的表名对象TableName tableName = TableName.valueOf("t_book");// 删除指定数据表中的指定列族admin.deleteColumn(tableName, "more".getBytes());System.out.println("---------------删除列族 END-----------------");}/**    * 插入数据    */@Testpublic void insert() throws IOException{System.out.println("---------------插入数据 START-----------------");// 取得一个数据表对象Table table = connection.getTable(TableName.valueOf("t_book"));// 需要插入数据库的数据集合List<Put> putList = new ArrayList<Put>();Put put;// 生成数据集合for(int i = 0; i < 10; i++){put = new Put(Bytes.toBytes("row" + i));put.addColumn(Bytes.toBytes("base"), Bytes.toBytes("name"), Bytes.toBytes("bookName" + i));putList.add(put);}// 将数据集合插入到数据库table.put(putList);System.out.println("---------------插入数据 END-----------------");}}

热门标签