hbase java api
前期准备
- 在src/main下新建resource文件夹
- 在pom.xml添加下面内容
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hbase.version>0.98.6-hadoop2</hbase.version>
</properties>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>0.98.6-hadoop2</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>0.98.6-hadoop2</version>
</dependency>
```
- 将hadoop下的core-site.xml和hdfs-site.xml以及hbase下的hbase-site.xml复制到resource中
### api基础操作
package weice.gao.hbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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.Delete;
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;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IOUtils;
public class Hbase {
public static void get(String tablename,String row) throws IOException{
Configuration configuration = HBaseConfiguration.create();
HTable table = new HTable(configuration, tablename);
Get get = new Get(Bytes.toBytes(row));
Result result = table.get(get);
Cell[] cells = result.rawCells();
for(Cell cell:cells)
{
System.out.print(Bytes.toString(CellUtil.cloneFamily(cell))+”:”);
System.out.print(Bytes.toString(CellUtil.cloneQualifier(cell))+”->”);
System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}
table.close();
}
public static void put(String tablename,String row,String info,String key,String value) throws IOException{
Configuration configuration = HBaseConfiguration.create();
HTable table = new HTable(configuration, tablename);
Put put = new Put(Bytes.toBytes(row));
put.add(Bytes.toBytes(info),
Bytes.toBytes(key),
Bytes.toBytes(value));
table.put(put);
table.close();
}
public static void del(String tablename,String row,String info,String field) throws IOException{
Configuration configuration = HBaseConfiguration.create();
HTable table = new HTable(configuration, tablename);
Delete delete = new Delete(Bytes.toBytes(row));
delete.deleteColumn(Bytes.toBytes(info), Bytes.toBytes(field));
table.delete(delete);
table.close();
}
public static void scan(String tablename) throws IOException{
Configuration configuration = HBaseConfiguration.create();
HTable table = new HTable(configuration, tablename);
Scan scan = new Scan();
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
System.out.println(Bytes.toString(result.getRow()));
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.print(Bytes.toString(CellUtil.cloneFamily(cell))+":");
System.out.print(Bytes.toString(CellUtil.cloneQualifier(cell))+"->");
System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}System.out.println("--------------------");
}
IOUtils.closeStream(resultScanner);
IOUtils.closeStream(table);
}
public static void create(String tablename) throws IOException{
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "node-1:2181");
HBaseAdmin admin = new HBaseAdmin(configuration);
HTableDescriptor tbl = new HTableDescriptor(TableName.valueOf(tablename));
HColumnDescriptor hcd = new HColumnDescriptor("info");
hcd.setValue("VERSIONS", "3");
tbl.addFamily(hcd);
admin.createTable(tbl);
admin.close();
}
public static void delTable(String tablename) throws IOException{
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "node-1:2181");
HBaseAdmin admin = new HBaseAdmin(configuration);
admin.disableTable(tablename);
admin.deleteTable(tablename);
admin.close();
}
public static void main(String[] args) throws IOException {
Hbase hbase = new Hbase();
hbase.put("people", "10001", "info1", "name", "gao");
hbase.get("people", "10001");
hbase.del("people", "10001", "info1","age");
hbase.get("people", "10001");
hbase.scan("people");
hbase.delTable("people1");
}
}
“`