如下操做在Hadoop分佈式集羣基礎上進行。 java
下載:http://www.apache.org/dyn/closer.cgi/hbase/ ,hbase-0.98.0-hadoop2-bin.tar.gz。 web
SHELL$ tar -zxvf hbase-0.98.0-hadoop2-bin.tar.gz
SHELL$ mv hbase-0.98.0-hadoop2 ~/hbase0.98.0hadoop2
shell
(1)修改/etc/profile文件
SHELL$ sudo gedit /etc/profile
apache
(2)驗證
服務器
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <!-- hbase的master主機名和端口 --> <name>hbase.master</name> <value>hdfs://192.168.1.240:60000</value> </property> <property> <!-- Hbase數據保存目錄 --> <name>hbase.rootdir</name> <!-- 主機和端口號與$HADOOP_HOME/.../core-site.xml的fs.defaultFS的主機和端口號一致 --> <value>hdfs://192.168.1.240:9000/hbase</value> </property> <property> <!-- 開啓分佈式 --> <name>hbase.cluster.distributed</name> <value>true</value> </property> <property> <!-- hbase集羣中zookeeper的主機各節點,使用奇數可儘可能確保選舉leader公平 --> <name>hbase.zookeeper.quorum</name> <!-- value>hapsalve1,hapsalve2,hapsalve3</value --> <value>192.168.1.241,192.168.1.242,192.168.1.243</value> </property> <property> <!-- hbase臨時文件位置 --> <name>hbase.tmp.dir</name> <value>/home/hadoop/hbase0.98.0hadoop2/hbase-tmp</value> </property> <property> <!-- hbase臨時zookeeper數據存放位置 --> <name>hbase.zookeeper.property.dataDir</name> <value>/home/hadoop/hbase0.98.0hadoop2/zookeeper-temp</value> </property> </configuration>
SHELL$ sudo scp -rpv /home/hadoop/hbase0.98.0hadoop2/ hadoop@hapslave*:/home/hadoop/ 異步
在Hadoop集羣啓動後,再啓動HBase集羣。
SHELL$ start-hbase.sh
在主控機經過web界面查看(本例配置4個節點):
分佈式
SHELL$ stop-hbase.sh
函數
SHELL$ hbase shell
oop
清空表:
truncate是一個可以快速清空資料表內全部資料的SQL語法。而且能針對具備自動遞增值的字段,作計數重置歸零從新計算的做用
ui
所有API在%HBase%/docs目錄裏,徹底是英文的。
本例所須所有jar均可以在%HBase安裝目錄%/lib目錄中找到。圖省事,我一古腦兒全導入了。
package com.cuiweiyou.test; import java.io.IOException; import java.util.Iterator; 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; import org.junit.Test; public class HBaseTest { //建立表 @Test public void creatTable() throws Exception { String strTBName = "tb_test"; //表 String strColFamily = "cf"; //列族 //配置 Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.241,192.168.1.242,192.168.1.243"); //管理員 HBaseAdmin hbaseAdmin = new HBaseAdmin(conf); //addColumn(String tableName, HColumnDescriptor column) //向一個已經存在的表添加咧 //checkHBaseAvailable(HBaseConfiguration hbaseConf) //靜態函數,查看HBase是否處於運行狀態 //deleteTable(byte[] tableName) //刪除一個已經存在的表 //enableTable(byte[] tableName) //使表處於有效狀態 //disableTable(byte[] tableName) //使表處於無效狀態 //HTableDescriptor[] listTables() //列出全部用戶控件表項 //modifyTable(byte[] tableName, HTableDescriptor tableDesc) //修改表的模式,是異步的操做,耗時 //tableExists(String tableName) //檢查表是否存在 //表名稱 TableName tableName = TableName.valueOf(strTBName); //表描述器 HTableDescriptor tableDesc = new HTableDescriptor(tableName); //removeFamily(byte[] column) //移除一個列族 //getName() //獲取表的名字 //getValue(byte[] key) //獲取屬性的值 //setValue(String key, String value) //設置屬性的值 tableDesc.addFamily(new HColumnDescriptor(strColFamily));//添加列族 //建立一個表,同步操做 hbaseAdmin.createTable(tableDesc); System.out.println("建立表" + strTBName + "成功"); } }
//爲表添加數據 @Test public void addData() throws IOException { String strTBName = "tb_test"; String strColFamily = "cf"; String strColumn = "col"; //列名 String strRowKey = "row1"; //行號 String strValue = "values"; //值 Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.241,192.168.1.242,192.168.1.243"); //表實例 HTable table = new HTable(conf, strTBName); //close() 釋放全部的資源或掛起內部緩衝區中的更新 //exists(Get get) 檢查Get實例所指定的值是否存在於HTable的列中 //get(Get get) 獲取指定行的某些單元格所對應的值 //getEndKeys() 獲取當前一打開的表每一個區域的結束鍵值 //getScanner(byte[] family) 獲取當前給定列族的scanner實例 //getTableDescriptor() 獲取當前表的HTableDescriptor實例 //getTableName() 獲取表名 //isTableEnabled(HBaseConfiguration conf, String tableName) 檢查表是否有效 // 獲取全部的列族 HColumnDescriptor[] columnFamilies = table.getTableDescriptor().getColumnFamilies(); //HColumnDescriptor的經常使用方法: //getName() //獲取列族的名字 //getValue(byte[] key) //獲取對應的屬性的值 //setValue(String key, String value) //設置對應屬性的值 //插入器 Put put = new Put(Bytes.toBytes(strRowKey));// 設置行號,RowKey //add(byte[] family, byte[] qualifier, byte[] value) 將指定的列和對應的值添加到Put實例中 //add(byte[] family, byte[] qualifier, long ts, byte[] value) 將指定的列和對應的值及時間戳添加到Put實例中 //getRow() 獲取Put實例的行 //getRowLock() 獲取Put實例的行鎖 //getTimeStamp() 獲取Put實例的時間戳 //isEmpty() 檢查familyMap是否爲空 //setTimeStamp(long timeStamp) 設置Put實例的時間戳 for (int i = 0; i < columnFamilies.length; i++) { String familyName = columnFamilies[i].getNameAsString(); // 獲取列族名 //指定列族 if (familyName.equals(strColFamily)) { //插入 put.add(Bytes.toBytes(familyName), Bytes.toBytes(strColumn), Bytes.toBytes(strValue)); } } table.put(put); //運行插入器 System.out.println("存入數據完畢"); }
//根據RowKey查詢整行 @Test public void getRow() throws IOException { String strTBName = "tb_test"; String strRowKey = "row1"; Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.241,192.168.1.242,192.168.1.243"); HTable table = new HTable(conf, strTBName); //獲取表實例 //查詢器 Get get = new Get(Bytes.toBytes(strRowKey)); //查詢指定行 //addColumn(byte[] family, byte[] qualifier) 獲取指定列族和列修飾符對應的列 //addFamily(byte[] family) 經過指定的列族獲取其對應列的全部列 //setTimeRange(long minStamp,long maxStamp) 獲取指定取件的列的版本號 //setFilter(Filter filter) 當執行Get操做時設置服務器端的過濾器 Result result = table.get(get); //containsColumn(byte[] family, byte[] qualifier) 檢查指定的列是否存在 //getFamilyMap(byte[] family) 獲取對應列族所包含的修飾符與值的鍵值對 //getValue(byte[] family, byte[] qualifier) 獲取對應列的最新值 List<Cell> listCells = result.listCells(); //指定行、所有列族的所有列 for (Cell cell : listCells) { 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("時間戳:" + cell.getTimestamp()); } }
//遍歷所有條目 @Test public void getAllRows() throws IOException { String strTBName = "tb_test"; Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.241,192.168.1.242,192.168.1.243"); HTable table = new HTable(conf, strTBName); //獲取表實例 //掃描器 ResultScanner resultScanner = table.getScanner(new Scan()); //針對全表的查詢器 Iterator<Result> results = resultScanner.iterator(); while(results.hasNext()) { Result result = results.next(); List<Cell> cells = result.listCells(); for(Cell cell : cells) { 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("時間戳:" + cell.getTimestamp() + "\n------------------"); } } }
//更新表中某行的某一列 @Test public void updateTable() throws IOException { String strTBName = "tb_test"; String strColFamily = "cf"; String strColumn = "col"; String strRowKey = "row1"; String strNewValue = "NewValues"; Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.241,192.168.1.242,192.168.1.243"); HTable table = new HTable(conf, strTBName); //獲取表實例 Put put = new Put(Bytes.toBytes(strRowKey)); //仍然是插入操做(已知列族,已知列,新值) put.add(Bytes.toBytes(strColFamily), Bytes.toBytes(strColumn), Bytes.toBytes(strNewValue)); table.put(put); System.out.println("更新結束"); }
//刪除指定行的指定的列(刪除單元格) @Test public void deleteColumn() throws IOException { String strTBName = "tb_test"; String strColFamily = "cf"; String strColumn = "col"; String strRowKey = "row1"; Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.241,192.168.1.242,192.168.1.243"); HTable table = new HTable(conf, strTBName); //獲取表實例 //刪除器 Delete del = new Delete(Bytes.toBytes(strRowKey)); del.deleteColumns(Bytes.toBytes(strColFamily), Bytes.toBytes(strColumn)); table.delete(del); System.out.println("行:" + strRowKey + ",列族:"+ strColFamily +",列:"+ strColumn +",刪除完畢"); }
//刪除整行 @Test public void deleteAllColumn() throws IOException { String strTBName = "tb_test"; String strRowKey = "row1"; Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.241,192.168.1.242,192.168.1.243"); HTable table = new HTable(conf, strTBName); //獲取表實例 Delete deleteAll = new Delete(Bytes.toBytes(strRowKey)); table.delete(deleteAll); System.out.println("這一行全刪除了"); }
//刪除表 @Test public void deleteTable() throws IOException { String strTBName = "tb_test"; Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.1.241,192.168.1.242,192.168.1.243"); HBaseAdmin admin = new HBaseAdmin(conf); admin.disableTable(strTBName); admin.deleteTable(strTBName); System.out.println(strTBName + "表 刪除了"); }
- end
歡迎加入Hadoop技術交流及就業指導羣:199362441