注意喉童,這部分的學(xué)習(xí)內(nèi)容撇寞,我們先學(xué)習(xí)使用老版本的API,接著再寫出新版本的API調(diào)用方式堂氯。因?yàn)樵谄髽I(yè)中蔑担,有些時(shí)候我們需要一些過時(shí)的API來提供更好的兼容性。
1咽白、HBase API Code -> GitHub
https://github.com/liufengji/HBase_API_Demo.git
2啤握、安裝Maven并配置環(huán)境變量
http://www.reibang.com/p/79544e383b6e
3、新建Maven Project
新建項(xiàng)目后在pom.xml中添加依賴
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
4晶框、編寫HBaseAPI
注意排抬,這部分的學(xué)習(xí)內(nèi)容,我們先學(xué)習(xí)使用老版本的API授段,接著再寫出新版本的API調(diào)用方式蹲蒲。因?yàn)樵谄髽I(yè)中,有些時(shí)候我們需要一些過時(shí)的API來提供更好的兼容性侵贵。
1)首先需要獲取Configuration對象
public static Configuration conf;
static{
//使用HBaseConfiguration的單例方法實(shí)例化
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.216.20");
conf.set("hbase.zookeeper.property.clientPort", "2181");
}
2)判斷表是否存在
public static boolean isTableExist(String tableName)
throws MasterNotRunningException,ZooKeeperConnectionException, IOException{
//在HBase中管理届搁、訪問表需要先創(chuàng)建HBaseAdmin對象
//Connection connection = ConnectionFactory.createConnection(conf);
//HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
HBaseAdmin admin = new HBaseAdmin(conf);
return admin.tableExists(tableName);
}
3)創(chuàng)建表
public static void createTable(String tableName, String... columnFamily)
throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
HBaseAdmin admin = new HBaseAdmin(conf);
//判斷表是否存在
if(isTableExist(tableName)){
System.out.println("表" + tableName + "已存在");
//System.exit(0);
}else{
//創(chuàng)建表屬性對象,表名需要轉(zhuǎn)字節(jié)
HTableDescriptor descriptor =
new HTableDescriptor(TableName.valueOf(tableName));
//創(chuàng)建多個(gè)列族
for(String cf : columnFamily){
descriptor.addFamily(new HColumnDescriptor(cf));
}
//根據(jù)對表的配置,創(chuàng)建表
admin.createTable(descriptor);
System.out.println("表" + tableName + "創(chuàng)建成功窍育!");
}
}
4)刪除表
public static void dropTable(String tableName)
throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
HBaseAdmin admin = new HBaseAdmin(conf);
if(isExisTables (tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println("表" + tableName + "刪除成功卡睦!");
}else{
System.out.println("表" + tableName + "不存在!");
}
}
5)向表中插入數(shù)據(jù)
public static void addRowData(String tableName, String rowKey,
String columnFamily, String column, String value) throws IOException{
//創(chuàng)建HTable對象
HTable hTable = new HTable(conf, tableName);
//向表中插入數(shù)據(jù)
Put put = new Put(Bytes.toBytes(rowKey));
//向Put對象中組裝數(shù)據(jù)
put.add(Bytes.toBytes(columnFamily),
Bytes.toBytes(column),
Bytes.toBytes(value));
hTable.put(put);
hTable.close();
System.out.println("插入數(shù)據(jù)成功");
}
6)刪除一行數(shù)據(jù)
//刪除一行
public static void deleteRow(String tableName, String rowKey) throws Exception {
HTable hTable = new HTable(conf, tableName);
Delete delete = new Delete(Bytes.toBytes(rowKey));
hTable.delete(delete);
hTable.close();
System.out.println("刪除數(shù)據(jù)成功");
}
7)刪除多行數(shù)據(jù)
public static void deleteMultiRow(String tableName, String... rows)
throws IOException{
HTable hTable = new HTable(conf, tableName);
List<Delete> deleteList = new ArrayList<Delete>();
for(String row : rows){
Delete delete = new Delete(Bytes.toBytes(row));
deleteList.add(delete);
}
hTable.delete(deleteList);
hTable.close();
}
8)得到所有數(shù)據(jù)
public static void getAllRows(String tableName) throws IOException{
HTable hTable = new HTable(conf, tableName);
//得到用于掃描region的對象
Scan scan = new Scan();
//使用HTable得到resultcanner實(shí)現(xiàn)類的對象
ResultScanner resultScanner = hTable.getScanner(scan);
for(Result result : resultScanner){
Cell[] cells = result.rawCells();
for(Cell cell : cells){
//得到rowkey
System.out.println("行鍵:" + Bytes.toString(CellUtil.cloneRow(cell)));
//得到列族
System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
9)得到某一行所有數(shù)據(jù)
public static void getRow(String tableName, String rowKey) throws IOException{
HTable table = new HTable(conf, tableName);
Get get = new Get(Bytes.toBytes(rowKey));
//get.setMaxVersions();顯示所有版本
//get.setTimeStamp();顯示指定時(shí)間戳的版本
Result result = table.get(get);
for(Cell cell : result.rawCells()){
System.out.println("行鍵:" + Bytes.toString(result.getRow()));
System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("時(shí)間戳:" + cell.getTimestamp());
}
}
10)獲取某一行指定“列族:列”的數(shù)據(jù)
public static void getRowQualifier(String tableName, String rowKey, String family,
String qualifier) throws IOException{
HTable table = new HTable(conf, tableName);
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
Result result = table.get(get);
for(Cell cell : result.rawCells()){
System.out.println("行鍵:" + Bytes.toString(result.getRow()));
System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
11)主方法
import java.util.ArrayList;
import java.util.List;
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;
public static void main(String[] args) throws Exception {
//判斷HBase表是否存在
//System.out.println(isTableExist("Student"));
//創(chuàng)建表
//createTable("staff", "info","fa");
//刪除表
//dropTable("staff");
//添加一行數(shù)據(jù)
//addRow("staff", "1001", "info", "name", "Nick");
//addRow("staff", "1002", "info", "name", "Nick");
//addRow("staff", "1003", "info", "sex", "女");
//刪除一行
//deleteRow("staff", "1001");
//刪除多行
//deleteMultiRow("staff","1001","1002","1003");
//獲取所有數(shù)據(jù)
//getAllRows("staff");
//獲取一行
//getRow("staff","1001");
//只獲取一列
//getRowQualifier("staff","1003","info","sex");
}
尖叫提示:core-site.xml hbase-site.xml hdfs-site.xml log4j.properties 放入src/main/resources目錄下