创建表

package com.zyd.api;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
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.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.junit.Before;
import org.junit.Test;

public class HBaseApi {
//创建连接的时候比较慢,可以设置一个连接池
private HBaseAdmin admin = null;
private HTable table = null;
private String TABLE_NAME = "hbaseApi";

//初始化
@Before
public void init() throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
Configuration conf = new Configuration();
//连接zookeeper,因为zookeeper知道HMaster地址 第一个是在hbase-site.xml文件中
conf.set("hbase.zookeeper.quorum","note01,note02,note03");
admin = new HBaseAdmin(conf);

table = new HTable(conf, TableName.valueOf(TABLE_NAME));

}
@Test
public void createTable() throws IOException{
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));

//列族
HColumnDescriptor hColumnDescriptor1 = new HColumnDescriptor("cf1");
HColumnDescriptor hColumnDescriptor2 = new HColumnDescriptor("cf2");
//设置最大版本号,默认是1
hColumnDescriptor1.setMaxVersions(3);
// 设置生命周期 默认单位为秒
// hColumnDescriptor1.setTimeToLive(0);
//add 表示可以创建多个
hTableDescriptor.addFamily(hColumnDescriptor1);
hTableDescriptor.addFamily(hColumnDescriptor2);

admin.createTable(hTableDescriptor);
}
}

结果通过主页http://note03:60010/master-status 访问tables

添加rowkey column value

//添加数据
@Test
public void put() throws RetriesExhaustedWithDetailsException, InterruptedIOException{
//rowkey
Put put = new Put("1000".getBytes());
//列族 列名 value
put.add("cf1".getBytes(),"name".getBytes(), "zs".getBytes());

put.add("cf1".getBytes(),"age".getBytes(), "18".getBytes());

// table.put(put);


Put put2 = new Put("100".getBytes());
//列族 列名 value
put2.add("cf1".getBytes(),"name".getBytes(), "zs".getBytes());

put2.add("cf1".getBytes(),"age".getBytes(), "18".getBytes());


List<Put> list = new ArrayList<Put>();
list.add(put);
list.add(put2);
//也可以是批量的插入
table.put(list);
}

查询

//查询数据
@Test
public void get() throws IOException{
//获取行
Get get = new Get("100".getBytes());

Result result = table.get(get);

byte[] value = result.getValue("cf1".getBytes(), "name".getBytes());

Cell cell = result.getColumnLatestCell("cf1".getBytes(),"name".getBytes());
//可以得到列族名以及其他的信息等
byte [] cloneFamily = CellUtil.cloneFamily(cell);
//hbase里面全是字节 自己要知道类型
String str = new String(value);
System.out.println(str);

System.out.println("第二种方式:"+new String(cloneFamily));
//动态添加列时,获取类名方法和value
List<Cell> listCells = result.listCells();

for (Cell cell2 : listCells) {
System.out.println("列名(column)"+new String(CellUtil.cloneQualifier(cell2))+" value"
+ ":"+new String(CellUtil.cloneValue(cell2)));
}
}

扫描

//扫描
@Test
public void scan() throws IOException{
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);

for (Result result : scanner) {
List<Cell> lisCells = result.listCells();

for (Cell cell : lisCells) {
System.out.println("column:"+new String(CellUtil.cloneQualifier(cell))+" value"
+ new String(CellUtil.cloneValue(cell)));
}
}
}

出现小bug column 打印的是地址值,

解决:new String()括号位置不正确