所用HBase版本為1.1.2,hadoop版本為2.4
/* * 創建一個students表,並進行相關操作 */ import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.KeyValue; 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 class HBaseDBDao { // 聲明靜態配置 private static Configuration conf = null; static { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.154"); conf.set("hbase.zookeeper.property.clientPort", "2181"); conf.set("hbase.master", "192.168.1.154:6000"); } //判斷表是否存在 private static boolean isExist(String tableName) throws IOException { HBaseAdmin hAdmin = new HBaseAdmin(conf); return hAdmin.tableExists(tableName); } // 創建數據庫表 public static void createTable(String tableName, String[] columnFamilys) throws Exception { // 新建一個數據庫管理員 HBaseAdmin hAdmin = new HBaseAdmin(conf); if (hAdmin.tableExists(tableName)) { System.out.println("表 "+tableName+" 已存在!"); System.exit(0); } else { // 新建一個students表的描述 HTableDescriptor tableDesc = new HTableDescriptor(tableName); // 在描述里添加列族 for (String columnFamily : columnFamilys) { tableDesc.addFamily(new HColumnDescriptor(columnFamily)); } // 根據配置好的描述建表 hAdmin.createTable(tableDesc); System.out.println("創建表 "+tableName+" 成功!"); } } // 刪除數據庫表 public static void deleteTable(String tableName) throws Exception { // 新建一個數據庫管理員 HBaseAdmin hAdmin = new HBaseAdmin(conf); if (hAdmin.tableExists(tableName)) { // 關閉一個表 hAdmin.disableTable(tableName); hAdmin.deleteTable(tableName); System.out.println("刪除表 "+tableName+" 成功!"); } else { System.out.println("刪除的表 "+tableName+" 不存在!"); System.exit(0); } } // 添加一條數據 public static void addRow(String tableName, String row, String columnFamily, String column, String value) throws Exception { HTable table = new HTable(conf, tableName); Put put = new Put(Bytes.toBytes(row));// 指定行 // 參數分別:列族、列、值 put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value)); table.put(put); } // 刪除一條(行)數據 public static void delRow(String tableName, String row) throws Exception { HTable table = new HTable(conf, tableName); Delete del = new Delete(Bytes.toBytes(row)); table.delete(del); } // 刪除多條數據 public static void delMultiRows(String tableName, String[] rows) throws Exception { HTable table = new HTable(conf, tableName); List<Delete> delList = new ArrayList<Delete>(); for (String row : rows) { Delete del = new Delete(Bytes.toBytes(row)); delList.add(del); } table.delete(delList); } // 獲取一條數據 public static void getRow(String tableName, String row) throws Exception { HTable table = new HTable(conf, tableName); Get get = new Get(Bytes.toBytes(row)); Result result = table.get(get); // 輸出結果,raw方法返回所有keyvalue數組 for (KeyValue rowKV : result.raw()) { System.out.print("行名:" + new String(rowKV.getRow()) + " "); System.out.print("時間戳:" + rowKV.getTimestamp() + " "); System.out.print("列族名:" + new String(rowKV.getFamily()) + " "); System.out.print("列名:" + new String(rowKV.getQualifier()) + " "); System.out.println("值:" + new String(rowKV.getValue())); } } // 獲取所有數據 public static void getAllRows(String tableName) throws Exception { HTable table = new HTable(conf, tableName); Scan scan = new Scan(); ResultScanner results = table.getScanner(scan); // 輸出結果 for (Result result : results) { for (KeyValue rowKV : result.raw()) { System.out.print("行名:" + new String(rowKV.getRow()) + " "); System.out.print("時間戳:" + rowKV.getTimestamp() + " "); System.out.print("列族名:" + new String(rowKV.getFamily()) + " "); System.out .print("列名:" + new String(rowKV.getQualifier()) + " "); System.out.println("值:" + new String(rowKV.getValue())); } } } // 主函數 public static void main(String[] args) { try { String tableName = "student"; // 第一步:創建數據庫表:“student” String[] columnFamilys = { "info", "course" }; HBaseDBDao.createTable(tableName, columnFamilys); // 第二步:向數據表的添加數據 // 添加第一行數據 if (isExist(tableName)) { HBaseDBDao.addRow(tableName, "zpc", "info", "age", "20"); HBaseDBDao.addRow(tableName, "zpc", "info", "sex", "boy"); HBaseDBDao.addRow(tableName, "zpc", "course", "china", "97"); HBaseDBDao.addRow(tableName, "zpc", "course", "math", "128"); HBaseDBDao.addRow(tableName, "zpc", "course", "english", "85"); // 添加第二行數據 HBaseDBDao.addRow(tableName, "henjun", "info", "age", "19"); HBaseDBDao.addRow(tableName, "henjun", "info", "sex", "boy"); HBaseDBDao.addRow(tableName, "henjun", "course", "china","90"); HBaseDBDao.addRow(tableName, "henjun", "course", "math","120"); HBaseDBDao.addRow(tableName, "henjun", "course", "english","90"); // 添加第三行數據 HBaseDBDao.addRow(tableName, "niaopeng", "info", "age", "18"); HBaseDBDao.addRow(tableName, "niaopeng", "info", "sex","girl"); HBaseDBDao.addRow(tableName, "niaopeng", "course", "china","100"); HBaseDBDao.addRow(tableName, "niaopeng", "course", "math","100"); HBaseDBDao.addRow(tableName, "niaopeng", "course", "english","99"); // 第三步:獲取一條數據 System.out.println("**************獲取一條(zpc)數據*************"); HBaseDBDao.getRow(tableName, "zpc"); // 第四步:獲取所有數據 System.out.println("**************獲取所有數據***************"); HBaseDBDao.getAllRows(tableName); // 第五步:刪除一條數據 System.out.println("************刪除一條(zpc)數據************"); HBaseDBDao.delRow(tableName, "zpc"); HBaseDBDao.getAllRows(tableName); // 第六步:刪除多條數據 System.out.println("**************刪除多條數據***************"); String rows[] = new String[] { "henjun","niaopeng" }; HBaseDBDao.delMultiRows(tableName, rows); HBaseDBDao.getAllRows(tableName); // 第七步:刪除數據庫 System.out.println("***************刪除數據庫表**************"); HBaseDBDao.deleteTable(tableName); System.out.println("表"+tableName+"存在嗎?"+isExist(tableName)); } else { System.out.println(tableName + "此數據庫表不存在!"); } } catch (Exception e) { e.printStackTrace(); } } }
項目所需jar包如下:
程序運行結果如下:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 十一月 25, 2015 9:43:05 上午 org.apache.hadoop.util.NativeCodeLoader <clinit> 警告: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable 十一月 25, 2015 9:43:06 上午 org.apache.hadoop.hbase.zookeeper.RecoverableZooKeeper <init> 信息: Process identifier=hconnection-0x1cdc2ce connecting to ZooKeeper ensemble=192.168.1.154:2181 十一月 25, 2015 9:43:11 上午 org.apache.hadoop.hbase.client.HBaseAdmin postOperationResult 信息: Created student 創建表 student 成功! **************獲取一條(zpc)數據************* 行名:zpc 時間戳:1448415791324 列族名:course 列名:china 值:97 行名:zpc 時間戳:1448415791333 列族名:course 列名:english 值:85 行名:zpc 時間戳:1448415791329 列族名:course 列名:math 值:128 行名:zpc 時間戳:1448415791285 列族名:info 列名:age 值:20 行名:zpc 時間戳:1448415791314 列族名:info 列名:sex 值:boy **************獲取所有數據*************** 行名:henjun 時間戳:1448415791344 列族名:course 列名:china 值:90 行名:henjun 時間戳:1448415791360 列族名:course 列名:english 值:90 行名:henjun 時間戳:1448415791354 列族名:course 列名:math 值:120 行名:henjun 時間戳:1448415791336 列族名:info 列名:age 值:19 行名:henjun 時間戳:1448415791340 列族名:info 列名:sex 值:boy 行名:niaopeng 時間戳:1448415791377 列族名:course 列名:china 值:100 行名:niaopeng 時間戳:1448415791385 列族名:course 列名:english 值:99 行名:niaopeng 時間戳:1448415791381 列族名:course 列名:math 值:100 行名:niaopeng 時間戳:1448415791368 列族名:info 列名:age 值:18 行名:niaopeng 時間戳:1448415791374 列族名:info 列名:sex 值:girl 行名:zpc 時間戳:1448415791324 列族名:course 列名:china 值:97 行名:zpc 時間戳:1448415791333 列族名:course 列名:english 值:85 行名:zpc 時間戳:1448415791329 列族名:course 列名:math 值:128 行名:zpc 時間戳:1448415791285 列族名:info 列名:age 值:20 行名:zpc 時間戳:1448415791314 列族名:info 列名:sex 值:boy ************刪除一條(zpc)數據************ 行名:henjun 時間戳:1448415791344 列族名:course 列名:china 值:90 行名:henjun 時間戳:1448415791360 列族名:course 列名:english 值:90 行名:henjun 時間戳:1448415791354 列族名:course 列名:math 值:120 行名:henjun 時間戳:1448415791336 列族名:info 列名:age 值:19 行名:henjun 時間戳:1448415791340 列族名:info 列名:sex 值:boy 行名:niaopeng 時間戳:1448415791377 列族名:course 列名:china 值:100 行名:niaopeng 時間戳:1448415791385 列族名:course 列名:english 值:99 行名:niaopeng 時間戳:1448415791381 列族名:course 列名:math 值:100 行名:niaopeng 時間戳:1448415791368 列族名:info 列名:age 值:18 行名:niaopeng 時間戳:1448415791374 列族名:info 列名:sex 值:girl **************刪除多條數據*************** ***************刪除數據庫表************** 十一月 25, 2015 9:43:11 上午 org.apache.hadoop.hbase.client.HBaseAdmin call 信息: Started disable of student 十一月 25, 2015 9:43:15 上午 org.apache.hadoop.hbase.client.HBaseAdmin postOperationResult 信息: Disabled student 十一月 25, 2015 9:43:18 上午 org.apache.hadoop.hbase.client.HBaseAdmin postOperationResult 信息: Deleted student 刪除表 student 成功! 表student存在嗎?false