hbase java 编程,Java编程实现操作HBase

操作系统:ubuntu 15.04

hbase版本:hbase 1.1.2

HBase提供了使用hbase-client-x.x.x.jar包,可用于Java编程操作HBase

包在hbase目录下的lib目录,此外还需要hbase-common,hadoop-common等jar

如果不清楚那些要那些不要,可先把lib下的jar包全部引入

Java操作Hbase示例

package com.test;

import java.io.IOException;

import java.util.Map.Entry;

import java.util.NavigableMap;

import org.apache.hadoop.conf.Configuration;

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.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.util.Bytes;

public class TestHBase {

public static void main(String[] args) {

TestHBase test = new TestHBase();

try {

test.createTable("users", "info");

test.insertRow("users", "bossky", "info", "name", "bo");

test.insertRow("users", "bossky", "info", "age", "22");

Result rt = test.getRow("users", "bossky");

for (Entry>> e1 : rt.getMap().entrySet()) {

System.out.println(new String(e1.getKey()));

for (Entry> e2 : e1.getValue().entrySet()) {

System.out.println("--" + new String(e2.getKey()));

for (Entry e3 : e2.getValue().entrySet()) {

System.out.println("----" + e3.getKey() + "=" + new String(e3.getValue()));

}

}

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* 创始表

*

* @param tableName

* @param familys

* @throws IOException

*/

public void createTable(String tableName, String... familys) throws IOException {

Configuration config = HBaseConfiguration.create();

// hbase的hbase.zookeeper.quorum默认就是localhost,不需要设置

// config.set("hbase.zookeeper.quorum", "localhost");

Connection connection = ConnectionFactory.createConnection(config);

Admin admin = connection.getAdmin();

HTableDescriptor hbaseTable = new HTableDescriptor(TableName.valueOf(tableName));

for (String col : familys) {

hbaseTable.addFamily(new HColumnDescriptor(col));

}

admin.createTable(hbaseTable);

}

/**

* 删除表

*

* @param tableName

* @throws IOException

*/

public void deleteTable(String tableName) throws IOException {

Configuration config = HBaseConfiguration.create();

Connection connection = ConnectionFactory.createConnection(config);

Admin admin = connection.getAdmin();

TableName tName = TableName.valueOf(tableName);

admin.disableTable(tName);// 要先disable才能删除

admin.deleteTable(tName);

}

/**

* 插入数据

*

* @param tableName

* @param rowKey

* @param family

* @param qualifier

* @param value

* @throws IOException

*/

public void insertRow(String tableName, String rowKey, String family, String qualifier, String value)

throws IOException {

Configuration config = HBaseConfiguration.create();

Connection connection;

connection = ConnectionFactory.createConnection(config);

Table table = connection.getTable(TableName.valueOf(tableName));

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

put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));

table.put(put);

}

/**

* 获取行数据

*

* @param tableName

* @param rowKey

* @return

* @throws IOException

*/

public Result getRow(String tableName, String rowKey) throws IOException {

Configuration config = HBaseConfiguration.create();

Connection connection;

connection = ConnectionFactory.createConnection(config);

Table talbe = connection.getTable(TableName.valueOf(tableName));

Get get = new Get(Bytes.toBytes(rowKey));

Result rt = talbe.get(get);

return rt;

}

/**

* 扫描表

*

* @param tableName

* @return

* @throws IOException

*/

public ResultScanner scan(String tableName) throws IOException {

Configuration config = HBaseConfiguration.create();

Connection connection;

connection = ConnectionFactory.createConnection(config);

Table talbe = connection.getTable(TableName.valueOf(tableName));

Scan scan = new Scan();

ResultScanner rts = talbe.getScanner(scan);

return rts;

}

}

无论是单机模式,还是伪分布模式都可以使用上面的例子,如果是伪分布模式有一点需要注意,如果hbase.root.dir不是localhost路径的

就需要使用config.set("hbase.zookeeper.quorum", "localhost"); 设置对应的路径了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值