hbase应用场景 java_使用Java对Hbase操作总结及示例代码

前面已经给大家讲解过如何使用Hbase建表,以及基本的操作和一些常用shell命令,今天就给大家介绍下如何使用java对Hbase进行各种操作。

没印象的话可以再去浏览下:

Hbase入门教程,shell命令大全讲解

Java操作Hbase主要方法:

1.Configuration

在使用Java API时,Client端需要知道HBase的配置环境,如存储地址,zookeeper等信息。

这些信息通过Configuration对象来封装,可通过如下代码构建该对象:

Configuration config = HBaseConfiguration.create();

在调用HBaseConfiguration.create()方法时,HBase首先会在classpath下查找hbase-site.xml文件,将里面的信息解析出来封装到Configuration对象中,如果hbase-site.xml文件不存在,则使用默认的hbase-core.xml文件。

2.HBaseAdmin

HBaseAdmin用于创建数据库表格,并管理表格的元数据信息,通过如下方法构建:

HBaseAdmin admin=new HBaseAdmin(config);

3.HTableDescriptor

在HTableDescriptor中,建立了一个表结构,HTableDescriptor封装表格对象,对表格的增删改查操作主要通过它来完成,构造方法如下:

HTableDescriptor table = new HTableDescriptor(TableName.valueOf(“表名”));

4.addFamily

addFamily用于建立表下的列簇,并存放到表结构,方法如下:

HColumnDescriptor base = new HColumnDescriptor(“列簇名”);

table.addFamily(base);

代码如下:

首先建一个maven工程,导入依赖包导pom.xml

org.apache.hbase

hbase-client

1.2.0

org.apache.hbase

hbase-common

1.2.0

org.apache.hbase

hbase-server

1.2.0

1、创建表操作

public class HBaseClient {

public void createTable() throws IOException {

// 1. 创建配置

Configuration conf = HBaseConfiguration.create();

conf.set("hbase.zookeeper.quorum","ip1");

//hbase主默认端口是60000

conf.set("hbase.master","ip1:60000");

//zookeeper客户端的端口号2181

conf.set("hbase.zookeeper.property.clientPort","2181");

// 2. 创建连接

Connection conn = ConnectionFactory.createConnection(conf);

//3.获得一个建表、删表的对象hbaseAdmin()是继承admin()

Admin admin = conn.getAdmin();

// 4. 创建表的描述信息

HTableDescriptor student = new HTableDescriptor(TableName.valueOf("表名"));

// 5. 添加列簇

student.addFamily(new HColumnDescriptor("列簇名1"));

student.addFamily(new HColumnDescriptor("列簇名2"));

// 6. 调用API进行建表操作

admin.createTable(student);

}

}

2、判断表是否存在

public void isTableExists() throws IOException {

// 1. 创建配置

Configuration conf = HBaseConfiguration.create();

conf.set("hbase.zookeeper.quorum","ip1");

conf.set("hbase.zookeeper.property.clientPort","2181");

// 2. 创建连接

Connection conn = ConnectionFactory.createConnection(conf);

// 3. 创建admin

Admin admin = conn.getAdmin();

// 4. 调用API进行判断表是否存在

System.out.println(admin.tableExists(TableName.valueOf("表名")));

}

3、向表中插入数据

public void putData2Table() throws IOException {

// 1. 创建配置

Configuration conf = HBaseConfiguration.create();

conf.set("hbase.zookeeper.quorum","ip1");

conf.set("hbase.zookeeper.property.clientPort","2181");

// 2. 创建连接

Connection conn = ConnectionFactory.createConnection(conf);

// 3. 创建Table类

Table student = conn.getTable(TableName.valueOf("表名"));

// 4. 创建Put类

Put put = new Put(Bytes.toBytes("1001"));

// 5. 向Put中添加 列簇,列名,值 注意:需要转化成字节数组

put.addColumn(Bytes.toBytes("列簇1"),Bytes.toBytes("列1"),Bytes.toBytes("zhangsan"));

put.addColumn(Bytes.toBytes("列簇1"),Bytes.toBytes("列2"),Bytes.toBytes("female"));

put.addColumn(Bytes.toBytes("列簇2"),Bytes.toBytes("列3"),Bytes.toBytes("math"));

put.addColumn(Bytes.toBytes("列簇2"),Bytes.toBytes("列4"),Bytes.toBytes("89"));

// 6.调用API进行插入数据

student.put(put);

}

4、查看一条数据

public void getDataFromTable() throws IOException {

// 1. 创建配置

Configuration conf = HBaseConfiguration.create();

conf.set("hbase.zookeeper.quorum","ip1");

conf.set("hbase.zookeeper.property.clientPort","2181");

// 2. 创建连接

Connection conn = ConnectionFactory.createConnection(conf);

// 3. 创建Table类

Table student = conn.getTable(TableName.valueOf("表名"));

// 4. 创建 Get 类

Get get = new Get(Bytes.toBytes("1001"));

// 5.调用API进行获取数据

Result result = student.get(get);

// 6. 将返回的结果进行遍历输出

Cell[] cells = result.rawCells();

for (Cell cell : cells) {

System.out.println("rowkey :"+Bytes.toString(CellUtil.cloneRow(cell)));

System.out.println("列簇 :"+Bytes.toString(CellUtil.cloneFamily(cell)));

System.out.println("列名 :"+Bytes.toString(CellUtil.cloneQualifier(cell)));

System.out.println("值 :"+Bytes.toString(CellUtil.cloneValue(cell)));

System.out.println("----------------");

}

}

5、删除表操作

public void dropTable() throws IOException {

// 1. 创建配置

Configuration conf = HBaseConfiguration.create();

conf.set("hbase.zookeeper.quorum","ip1");

conf.set("hbase.zookeeper.property.clientPort","2181");

// 2. 创建连接

Connection conn = ConnectionFactory.createConnection(conf);

// 3. 创建admin

Admin admin = conn.getAdmin();

// 4. 调用API禁用表

admin.disableTable(TableName.valueOf("表名"));

// 5. 调用API删除表

admin.deleteTable(TableName.valueOf("表名"));

}

}

6、删除hbase中的table里面的rowkey

public static void deleteRow(String tableName,String rowKey) throws Exception{

HTable hTable = new HTable(configuration,tableName);

Delete delete = new Delete(rowKey.getBytes());

List list = new ArrayList();

list.add(delete);

hTable.delete(list);

}

7、查询row = rowKey的数据

public static void getRow(String tableName,String rowKey) throws Exception{

HTable hTable = new HTable(configuration, tableName);

Get get = new Get(rowKey.getBytes());

Result result = hTable.get(get);

for(KeyValue value:result.raw()){

System.out.println("cf:"+new String(value.getFamily())+new String(value.getQualifier())+"="+new String(value.getValue()));

}

}

8、查询rowkey在startRow和endRow之间的数据,及rowkey的范围查询

Put、Delete与Get对象都是Row的子类,从该继承关系中我们就可以了解到Get、Delete与Pu对象本身就只能进行单行的操作,

HBase客户端还提供了一套能够进行全表扫描的API,方便用户能够快速对整张表进行扫描,以获取想要的结果—scan:

public static void getBetweenRow(String tableName,String startRow,String stopRow) throws Exception{

HTable table = new HTable(configuration, tableName);

Scan scan = new Scan();

scan.addColumn("cf1".getBytes(), "colum1".getBytes());

scan.addColumn("cf1".getBytes(), "colum2".getBytes());

scan.addColumn("cf1".getBytes(), "colum3".getBytes());

scan.setStartRow(startRow.getBytes());

scan.setStopRow(stopRow.getBytes());

ResultScanner scanner = table.getScanner(scan);

for(Result result:scanner){

for(KeyValue value:result.raw()){

System.out.println("cf:"+new String(value.getFamily())+new String(value.getQualifier())+"="+new String(value.getValue()));

}

}

}

9、批量写入

public void puts(String tableName, Map items) {

if (items == null || items.isEmpty()) {

LOG.error("[HBase] Adding null/empty item map!");

return;

}

int maxSize = 10000;

Table table = null;

try {

table = con.getTable(TableName.valueOf(tableName));

int eachSize = Math.min(maxSize, items.size());

List puts = new ArrayList(eachSize);

int handled = 0;

for (Entry entry : items.entrySet()) {

String ultimateRowKey = getHashedID(entry.getKey());

Object value = entry.getValue();

if (ultimateRowKey == null || ultimateRowKey.isEmpty()) {

LOG.error("[HBase] Adding null/empty hashed key! Original key is " + entry.getKey());

handled++;

continue;

}

Put put = new Put(Bytes.toBytes(ultimateRowKey));

put.addColumn(Bytes.toBytes(familyName1), Bytes.toBytes("ab"), Bytes.toBytes(value .getAb()));

put.addColumn(Bytes.toBytes(familyName1), Bytes.toBytes("dt"), Bytes.toBytes(value .getDt()));

put.addColumn(Bytes.toBytes(familyName1), Bytes.toBytes("hb"), Bytes.toBytes(value .getHb()));

Gson gson = new Gson();

String valuestr = gson.toJson(value);

put.addColumn(Bytes.toBytes(familyName2), Bytes.toBytes("js"), Bytes.toBytes(valuestr));

puts.add(put);

handled++;

// 每隔10000,写一次

if (handled == eachSize) {

LOG.info("[HBase] Adding " + eachSize + "rows!");

table.put(puts);

puts = new ArrayList(eachSize);

}

}

if (puts.size() > 0)

table.put(puts);

} catch (IOException e) {

LOG.error("[HBase] Error while putting data " + e.getMessage());

} finally {

try {

if (table != null)

table.close();

} catch (IOException e) {

LOG.error("[HBase] Error while closing table " + e.getMessage());

}

}

}

到此这篇关于使用Java对Hbase操作总结及示例代码的文章就介绍到这了,更多相关Java操作hbase总结内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值