创建表
public static void main(String[] args) throws IOException {
//添加配置
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","192.168.XXX.100");
conf.set("hbase.zookeeper.property.clientPort","2181");
//创建连接
Connection conn = ConnectionFactory.createConnection(conf);
//获取admin
Admin admin = conn.getAdmin();
//创建表
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("test"));
//创建列族
HColumnDescriptor cf1 = new HColumnDescriptor("name");
HColumnDescriptor cf2 = new HColumnDescriptor("address");
//将列族添加到表中
htd.addFamily(cf1);
htd.addFamily(cf2);
//admin创建表
admin.createTable(htd);
//关闭资源
admin.close();
conn.close();
}
统一化
public class TestHbase {
static Connection conn;
static Admin admin;
public static void initConn() throws IOException {
//添加配置
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","192.168.XXX.100");
conf.set("hbase.zookeeper.property.clientPort","2181");
//创建连接
if(null==conn) {
conn = ConnectionFactory.createConnection(conf);
}
}
public static void initAdmin() throws IOException {
if(null==conn){
initConn();
}
if(null==admin) {
admin = conn.getAdmin();
}
}
public static void close(){
if(null!=admin) {
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null!=conn) {
try {
conn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
创建表
public class CreateTable {
public static void create(String tablename,String... columnFamilies) throws IOException {
Connection conn = TestHbase.conn;
TestHbase.initConn();
Admin admin = TestHbase.admin;
if(null==tablename || null==columnFamilies){
TestHbase.close();
return;
}
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tablename));
for (int i = 0; i < columnFamilies.length; i++) {
if(null==columnFamilies[i])
continue;
htd.addFamily(new HColumnDescriptor(columnFamilies[i]));
}
try {
admin.createTable(htd);
} catch (IOException e) {
e.printStackTrace();
}finally {
TestHbase.close();
}
}
}
删除表
public class DropTable {
public static void drop(String tbName) throws IOException {
TestHbase.initAdmin();
Admin admin = TestHbase.admin;
if (!admin.tableExists(TableName.valueOf(tbName))) {
System.out.println("table:"+tbName+"don't exists");
return;
}
if (!admin.isTableDisabled(TableName.valueOf(tbName))) {
admin.disableTable(TableName.valueOf(tbName));
}
admin.deleteTable(TableName.valueOf(tbName));
TestHbase.close();
}
}
查询
public class GetRowValue {
public static void printRowValue(String tbName,String rk,String...cfAndColumn) throws IOException {
TestHbase.initAdmin();
Connection conn = TestHbase.conn;
Table table = conn.getTable(TableName.valueOf(tbName));
Get get = new Get(rk.getBytes());
if (cfAndColumn==null) {
//不加
}else {
if(cfAndColumn.length==1){
get.addFamily(cfAndColumn[0].getBytes());
}else if(cfAndColumn.length==2){
get.addColumn(cfAndColumn[0].getBytes(),cfAndColumn[1].getBytes());
}else {
throw new HBaseIOException("params is not correct");
}
}
Result result = table.get(get);
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
String s = new String(cell.getRowArray());
String s1 = new String(cell.getFamilyArray());
System.out.println(s+"\t"+s1);
}
TestHbase.close();
}
}
scan查询
【注意:下面遍历的都是Byte数组,原因如下】
了解HBase底层实现的都知道是LSM树(LSM中存储的是多个KeyValue组成的集合,每一个KeyValue一般都会用一个字节数组来表示)
public class ScanTable {
public static void scan(String tbName) throws IOException {
TestHbase.initConn();
TestHbase.initAdmin();
Connection conn = TestHbase.conn;
Table table = conn.getTable(TableName.valueOf(tbName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
Result tmp ;
System.out.println("RowKey\t"+"ColumnFamily\t"+"column\t");
while ((tmp = scanner.next())!=null) {
List<Cell> cells = tmp.listCells();
for (Cell cell : cells) {
String s = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
String s1 = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(),cell.getFamilyLength());
String s2 = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
String s3 = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println(s+"\t"+s1+"\t"+s2+"\t"+s3);
}
}
TestHbase.close();
}
}
插入
public class InsertValues {
public static void insert(String tbName,String rk,String cf,String column,String value) throws IOException {
TestHbase.initAdmin();
Connection conn = TestHbase.conn;
Admin admin = TestHbase.admin;
Table table = conn.getTable(TableName.valueOf(tbName));
Put put = new Put(rk.getBytes());
put.addColumn(cf.getBytes(),column.getBytes(),value.getBytes());
table.put(put);
TestHbase.close();
}
}