1.配置環(huán)境
1)eclipse和jdk的下載安裝配置
2)加載相關(guān)xml文件
pom.xml添加maven依賴
添加配置文件至新創(chuàng)建的resources中 ? 找到hbase? zookeeper? hadoop
configuration實(shí)例中添加 ? conf ?將配置文件放到classpath
新建一個source folder碘箍,將下面5個文件放入
hbase-site ?regionserver ? log4j ?hdfs-site ? core-site
3)client 操作 ? ? ? ? ?增刪改查
package org.apache.hadoop.hbase;
import java.io.IOException;
import java.io.InterruptedIOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
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.filter.Filter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;
public class HClient {
public static HTable getTable(String name) throws Exception{
//create a hbase configuration instance
Configuration conf = HBaseConfiguration.create();
//create a htabel instance
HTable table = new HTable(conf, name);
return table;
}
/**
* get data from hbase table
* get:
* get 'student:stu_info','20161204_1001'
* get 'student:stu_info','20161204_1001','info'
* get 'student:stu_info','20161204_1001','info:name'
* @throws Exception
*/
public static void getData(HTable table) throws Exception {
// TODO Auto-generated method stub
//create a get instance
Get get = new Get(Bytes.toBytes("20161204_1003"));
//configure the get
//get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
//get.addFamily(family);
//add to table
Result? rs = table.get(get);
for(Cell cell : rs.rawCells()){
System.out.println(
Bytes.toString(CellUtil.cloneRow(cell))
+"->"+
Bytes.toString(CellUtil.cloneFamily(cell))
+"->"+
Bytes.toString(CellUtil.cloneQualifier(cell))
+"->"+
Bytes.toString(CellUtil.cloneValue(cell))
+"->"+
cell.getTimestamp()
);
System.out.println("---------------------------------------------------");
}
}
/**
* put the data to hbase table
* put:
* put 'student:stu_info','20161204_1003','info:name','laosan'
* @param args
* @throws Exception
* @throws RetriesExhaustedWithDetailsException
* @throws Exception
*/
public static void putData(HTable table) throws RetriesExhaustedWithDetailsException, Exception{
//create a put instance
Put put = new Put(Bytes.toBytes("20161204_1003"));
put.add(
Bytes.toBytes("info"),
Bytes.toBytes("name"),
Bytes.toBytes("laosan")
);
//add to table
table.put(put);
getData(table);
}
/**
* delete data from hbase table
* delete
* delete 'student:stu_info', '20161204_1003', 'info:age'
* @param args
* @throws Exception
* @throws Exception
*/
public static void deleteData(HTable table) throws Exception{
//create a delete instance
Delete delete = new Delete(Bytes.toBytes("20161204_1003"));
//conf the delete
delete.deleteColumn(Bytes.toBytes("info"), Bytes.toBytes("age"));
//delete.deleteColumns(family, qualifier);
//add the delete
table.delete(delete);
getData(table);
}
/**
* scan the data from the hbase table
* scan :
* scan 'student:stu_info'
*
* @param args
* @throws Exception
* @throws Exception
*/
public static void scanData(HTable table) throws Exception{
//create a scan instance
Scan scan = new Scan();
//add to table
ResultScanner rsscan = table.getScanner(scan);
for(Result rs : rsscan){
System.out.println(Bytes.toString(rs.getRow()));
for(Cell cell: rs.rawCells()){
System.out.println(
Bytes.toString(CellUtil.cloneRow(cell))
+"->"+
Bytes.toString(CellUtil.cloneFamily(cell))
+"->"+
Bytes.toString(CellUtil.cloneQualifier(cell))
+"->"+
Bytes.toString(CellUtil.cloneValue(cell))
+"->"+
cell.getTimestamp()
);
}
System.out.println("------------------------------");
}
}
/**
* range scan
* @param table
* @throws Exception
*/
public static void rangeScan(HTable table) throws Exception {
// TODO Auto-generated method stub
//create a scan
Scan scan = new Scan();
//conf the scan
//scan.addColumn(family, qualifier);
//scan.addFamily(Bytes.toBytes("info"));
//scan.setStartRow(Bytes.toBytes("20161204_1002"));
//scan.setStopRow(Bytes.toBytes("20161204_1003"));
Filter filter = new PrefixFilter(Bytes.toBytes("2016121"));
scan.setFilter(filter);
//特殊配置
//設(shè)置是否開啟緩存
scan.setCacheBlocks(true);
//緩存的條數(shù)
scan.setCaching(100);
//設(shè)置每次取多少條
scan.setBatch(10);
//這兩個參數(shù)共同決定了RPC請求次數(shù)
//add to table
ResultScanner rsscan = table.getScanner(scan);
for(Result rs : rsscan){
System.out.println(Bytes.toString(rs.getRow()));
for(Cell cell: rs.rawCells()){
System.out.println(
Bytes.toString(CellUtil.cloneRow(cell))
+"->"+
Bytes.toString(CellUtil.cloneFamily(cell))
+"->"+
Bytes.toString(CellUtil.cloneQualifier(cell))
+"->"+
Bytes.toString(CellUtil.cloneValue(cell))
+"->"+
cell.getTimestamp()
);
}
System.out.println("------------------------------");
}
}
public static void main(String[] args) throws Exception {
HTable table = getTable("student:stu_info");
//getData(table);
//putData(table);
//deleteData(table);
//scanData(table);
rangeScan(table);
}
}