好吧,其实就是一个简单的Hbase客户端java操作
HbaseTestCase.java
package hbase;
import java.io.IOException;
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.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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;
/**
*
* <p>
* Title: HbaseTestCase.java
* Package hbase
* </p>
* <p>
* Description: Hbase Client TestCase
* <p>
* @author Tom.Cai
* @created 2015-4-28 下午9:14:01
* @version V1.0
*
*/
public class HbaseTestCase {
public static final String TABLE_NAME = "table1";
public static final String FAMILY_NAME = "family1";
public static final String ROW_KEY = "row1";
public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
//定位到hadoop
conf.set("hbase.rootdir", "hdfs://192.168.80.100:9000/hbase");
//hadoop地址也可以配置成主机名的形式
conf.set("hbase.zookeeper.quorum", "192.168.80.100");
HBaseAdmin admin = new HBaseAdmin(conf);
//新增表
createTable(admin);
//删除表
//deleteTable(admin);
HTable htable = new HTable(conf, TABLE_NAME);
//增加表记录
//addRecodes(htable);
//获取表记录
getRecodes(htable);
//全表扫描表
scanTable(htable);
}
private static void scanTable(HTable htable) throws IOException {
Scan scan = new Scan();
ResultScanner rScanner = htable.getScanner(scan);
for (Result result : rScanner) {
byte[] be = result.getValue(FAMILY_NAME.getBytes(), "qualifier".getBytes());
System.out.println(result+"---->value:"+new String(be));
}
}
private static void getRecodes(HTable htable) throws IOException {
Get get = new Get(ROW_KEY.getBytes());
Result result = htable.get(get);
byte[] b = result.getValue(FAMILY_NAME.getBytes(), "qualifier".getBytes());
System.out.println(new String(b));
}
private static void addRecodes(HTable htable) throws IOException {
Put put = new Put(ROW_KEY.getBytes());
put.add(FAMILY_NAME.getBytes(),"qualifier".getBytes(),"value".getBytes());
htable.put(put);
}
private static void deleteTable(HBaseAdmin admin) throws IOException {
admin.disableTable(TABLE_NAME);
admin.deleteTable(TABLE_NAME);
}
private static void createTable(HBaseAdmin admin) throws IOException {
if(!admin.tableExists(TABLE_NAME)){
HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);
HColumnDescriptor family = new HColumnDescriptor(FAMILY_NAME);
desc.addFamily(family);
admin.createTable(desc);
}
}
}
我的个人网站:http://www.caicongyang.com
我的CSDN博客地址: http://blog.csdn.net/caicongyang