记:
最近在公司写单元测试,因为要涉及到周任务达标问题,所以天天加班,今天周末,安排一波Hbase完全分布式的搭建安装
HBase java api操作
在很多情况下,操作hbase不是在命令行直接操作的,所以有了很多api,这里我用java api进行操作。所以打开eclipse。
1.解压hbase,然后把lib下的jar包拷贝到eclipse中;
2.选中那些jar包,bulid path一波
3.直接上代码嘿
package com.junxi.hbase;
import java.io.IOException;
import java.io.InterruptedIOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.RetriesExhaustedWithDetailsException;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HBaseDemo {
HBaseAdmin admin = null;
String tb = "person";
HTable table = null;
@Before
public void init() throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "master,node1,node2");
admin = new HBaseAdmin(conf);
table = new HTable(conf, tb.getBytes());
}
@Test
public void createTable() throws IOException {
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tb));
HColumnDescriptor family = new HColumnDescriptor("cf".getBytes());
desc.addFamily(family);
if(admin.tableExists(tb)) {
admin.disableTable(tb);
admin.deleteTable(tb);
}
admin.createTable(desc);
}
@Test
public void insert() throws RetriesExhaustedWithDetailsException, InterruptedIOException {
Put put = new Put("111".getBytes());
put.add("cf".getBytes(), "name".getBytes(), "zhangsan".getBytes());
put.add("cf".getBytes(), "age".getBytes(), "11".getBytes());
put.add("cf".getBytes(), "sex".getBytes(), "1".getBytes());
table.put(put);
}
@Test
public void get() throws IOException {
Get get = new Get("111".getBytes());
Result result = table.get(get);
Cell cell1 = result.getColumnLatestCell("cf".getBytes(), "name".getBytes());
Cell cell2 = result.getColumnLatestCell("cf".getBytes(), "age".getBytes());
Cell cell3 = result.getColumnLatestCell("cf".getBytes(), "sex".getBytes());
System.out.println(Bytes.toString(CellUtil.cloneValue(cell1)));
System.out.println(Bytes.toString(CellUtil.cloneValue(cell2)));
System.out.println(Bytes.toString(CellUtil.cloneValue(cell3)));
}
@Test
public void scan() throws IOException {
Scan scan = new Scan();
ResultScanner res = table.getScanner(scan);
for (Result result : res) {
Cell cell1 = result.getColumnLatestCell("cf".getBytes(), "name".getBytes());
Cell cell2 = result.getColumnLatestCell("cf".getBytes(), "age".getBytes());
Cell cell3 = result.getColumnLatestCell("cf".getBytes(), "sex".getBytes());
System.out.println(Bytes.toString(CellUtil.cloneValue(cell1)));
System.out.println(Bytes.toString(CellUtil.cloneValue(cell2)));
System.out.println(Bytes.toString(CellUtil.cloneValue(cell3)));
}
}
@After
public void destory() throws IOException {
if(admin != null) {
admin.close();
}
}
}