環境準備
需要環境:
PC-1 Suse Linux 9 10.192.1.1
PC-2 Suse Linux 9 10.192.1.2
PC-3 Suse Linux 9 10.192.1.3
PC-4 Suse Linux 9 10.192.1.4
其中,PC-1做namenode節點,PC-2、PC-3和PC-4做datanode節點。
并且已經安裝成功Hadoop-0.20.1及以上版本。
安裝包準備
需要安裝包:
zookeeper-3.2.1.tar.gz(stable版本)
hbase-0.20.1.tar.gz(stable版本)
安裝步驟
安裝和配置ZooKeeper
HBase從0.20.0開始,需要首先安裝ZooKeeper。從apache上下載zookeeper-3.2.1.tar.gz(Stable版本),解壓到/home/hdfs/目錄下。
(1),在namenode節點新建zookeeper目錄,在該目錄下新建myid文件。
(2),在zookeeper-3.2.1/conf目錄下,拷貝zoo_sample.cfg為zoo.cfg。在zoo.cfg中將dataDir改為/home/hdfs/zookeeper,在文件末位添加所有的主機:
server.1=10.192.1.1:2888:3888
server.2=10.192.1.2:2888:3888
server.3=10.192.1.3:2888:3888
server.4=10.192.1.4:2888:3888
server.5=10.192.1.5:2888:3888
server.6=10.192.1.62888:3888
(3)用scp命令將namenode節點的的/home/hdfs/ zookeeper-3.2.1和/home/hdfs/ zookeeper拷貝到其余所有主機的/home/hdfs目錄下。
(4)參照zoo.cfg中的配置,在各主機myid文件中寫入各自的編號。如:10.192.1.1入1,10.192.1.2寫入2
(5)在所有節點上執行bin/zkServer.sh start,分別啟動。
執行bin/zkCli.sh -server xxx.xxx.xxx.xxx:2181,檢查指定服務器是否成功啟動。
安裝和配置HBase
下載HBase0.20.1版本,解壓到namenode節點的/home/hdfs目錄下。
配置說明
(1)系統所有配置項的默認設置在hbase-default.xml中查看,如果需要修改配置項的值,在hbase-site.xml中添加配置項。
在分布式模式下安裝HBase,需要添加的最基本的配置項如下:
<property>
<name>hbase.rootdir</name>
<value>hdfs://namenode.hdfs:54310/hbase</value>
<description>The directory shared by region servers.</description>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
<description>The mode the cluster will be in. Possible values are
false: standalone and pseudo-distributed setups with managed Zookeeper
true: fully-distributed with unmanaged Zookeeper Quorum (see hbase-env.sh)
</description>
</property>
(2)在conf/hbase-env.sh中修改添加配置項:
export JAVA_HOME=/usr/java/jdk1.6.0_16
export HBASE_MANAGES_ZK=false
export HBASE_CLASSPATH=/home/hdfs/hadoop-0.20.1/conf
并把~/hadoop-0.20.1/conf/hdfs-site.xml拷貝至~/hbase-3.2.1/conf/目錄下。
(3)將ZooKeeper的配置文件zoo.cfg添加到HBase所有主機的CLASSPATH中。
(4)在conf/regionservers中添加hadoop-0.20.1/conf/slaves中所有的datanode節點。
啟動
Hadoop、ZooKeeper和HBase之間應該按照順序啟動和關閉:啟動Hadoop—>啟動ZooKeeper集群—>啟動HBase—>停止HBase—>停止ZooKeeper集群—>停止Hadoop。
在namenode節點執行bin/hbase-daemon.sh,啟動master。執行bin/start-hbase.sh和bin/stop-hbase.sh 腳本啟動和停止HBase服務。
接口說明
HBase按列存儲結構化數據,支持建表、插入記錄、查詢記錄、刪除記錄和索引操作等等,不支持連接和更新操作。
開發步驟
引入JAR包
在Windows客戶端編寫JAVA程序操作HBase,需要引入一些JAR包。需要引入的JAR如下:hadoop-0.20.1-core.jar,commons-logging-1.0.4.jar,commons-logging-api-1.0.4.jar,zookeeper-3.2.1.jar,hbase-0.20.1.jar,log4j-1.2.15.jar。
開發模式
在分布式模式下開發,在程序中配置與HDFS和ZooKeeper的連接,即可對數據進行操作。
view plaincopy to clipboardprint?
import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.IOException;
import java.awt.List;
import java.util.Map;
import java.util.NavigableMap;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.HColumnDescriptor;
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.Get;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseManager {
public static void main(String[] args) throws Exception{
HBaseManager manager = new HBaseManager();
manager.testGet();
}
public void testQueryRS()throws Exception{
HBaseConfiguration config = new HBaseConfiguration();
config.set("hbase.master", "10.192.1.1:60000");
config.set("hbase.zookeeper.quorum", "10.192.1.1");
HTable table = new HTable(config, "commodity");
System.out.println("Get Spin's commodity info");
Scan scanner = new Scan();
scanner.addColumn(Bytes.toBytes("description"));
scanner.setMaxVersions();
ResultScanner rsScanner = table.getScanner(scanner);
System.out.println(rsScanner.toString());
Result rs = rsScanner.next();
while(null != rs){
System.out.println(rs.size());
NavigableMap<byte[],NavigableMap<byte[],NavigableMap<Long,byte[]>>> nMap = rs.getMap();
NavigableMap<byte[],NavigableMap<Long,byte[]>> columnMap = nMap.get(Bytes.toBytes("description"));
NavigableMap<Long,byte[]> qualMap = columnMap.get(new byte[]{});
if(qualMap.entrySet().size() > 0){
System.out.println("---------------------------");
for(Map.Entry<Long, byte[]> m :qualMap.entrySet())
{
System.out.println("Value:"+ new String(m.getValue()));
}
}
rs = rsScanner.next();
}
}
public void testQueryCommodity()throws Exception{
HBaseConfiguration config = new HBaseConfiguration();
config.set("hbase.master", "10.192.1.1:60000");
config.set("hbase.zookeeper.quorum", "10.192.1.1.203");
HTable table = new HTable(config, "commodity");
System.out.println("Get Spin's commodity info");
Get mathGet = new Get(new String("Spin").getBytes());
mathGet.addColumn(Bytes.toBytes("widgetname"));
mathGet.setMaxVersions();
Result rs = table.get(mathGet);
NavigableMap<byte[],NavigableMap<byte[],NavigableMap<Long,byte[]>>> nMap = rs.getMap();
NavigableMap<byte[],NavigableMap<Long,byte[]>> columnMap = nMap.get(Bytes.toBytes("widgetname"));
NavigableMap<Long,byte[]> qualMap = columnMap.get(new byte[]{});
if(qualMap.entrySet().size() > 0){
for(Map.Entry<Long, byte[]> m :qualMap.entrySet())
{
System.out.println("Value:"+ new String(m.getValue()));
break;
}
}
}
public void test()throws Exception{
HBaseConfiguration config = new HBaseConfiguration();
config.set("hbase.master", "10.192.1.1:60000");
config.set("hbase.zookeeper.quorum", "10.192.1.1");
HBaseAdmin admin = new HBaseAdmin(config);
HTable table = new HTable(config, "scores");
if (admin.tableExists("scores")){
System.out.println("drop table");
admin.disableTable("scores");
admin.deleteTable("scores");
}
System.out.println("create table");
HTableDescriptor tableDescripter = new HTableDescriptor("scores".getBytes());
tableDescripter.addFamily(new HColumnDescriptor("grade"));
tableDescripter.addFamily(new HColumnDescriptor("course"));
admin.createTable(tableDescripter);
System.out.println("add Tom's data");
Put tomPut = new Put(new String("Tom").getBytes());
tomPut.add(new String("grade").getBytes(), new byte[]{}, new String("1").getBytes());
tomPut.add(new String("grade").getBytes(), new String("math").getBytes(), new String("87").getBytes());
tomPut.add(new String("course").getBytes(), new String("math").getBytes(), new String("97").getBytes());
table.put(tomPut);
System.out.println("add Jerry's data");
Put jerryPut = new Put(new String("Jerry").getBytes());
jerryPut.add(new String("grade").getBytes(), new byte[]{}, new String("2").getBytes());
jerryPut.add(new String("grade").getBytes(), new String("math").getBytes(), new String("77").getBytes());
jerryPut.add(new String("course").getBytes(), new String("math").getBytes(), new String("92").getBytes());
table.put(jerryPut);
System.out.println("Get Tom's data");
Get tomGet = new Get(new String("Tom").getBytes());
Result tomResult = table.get(tomGet);
System.out.println(tomResult.toString());
System.out.println("Get Tom's Math grade");
Get mathGet = new Get(new String("Tom").getBytes());
mathGet.addColumn(Bytes.toBytes("grade"));
mathGet.setMaxVersions();
Result rs = table.get(mathGet);
NavigableMap<byte[],NavigableMap<byte[],NavigableMap<Long,byte[]>>> nMap = rs.getMap();
NavigableMap<byte[],NavigableMap<Long,byte[]>> columnMap = nMap.get(Bytes.toBytes("grade"));
NavigableMap<Long,byte[]> qualMap = columnMap.get(Bytes.toBytes("math"));
for(Map.Entry<Long, byte[]> m :qualMap.entrySet())
{
System.out.println("TimeStamp:"+m.getKey());
System.out.println("Value:"+ new String(m.getValue()));
}
System.out.println("Delete a column");
Delete deleteArt = new Delete(Bytes.toBytes("Tom"));
deleteArt.deleteColumn(Bytes.toBytes("grade"), Bytes.toBytes("math"));
table.delete(deleteArt);
}
public void testScanner() throws IOException{
HBaseConfiguration config = new HBaseConfiguration();
config.set("hbase.master", "10.192.1.1:60000");
config.set("hbase.zookeeper.quorum", "10.192.1.1");
HTable table = new HTable(config, "commodity");
System.out.println("Scan commodity info");
Scan scanner = new Scan();
scanner.addColumn(Bytes.toBytes("widgetname"));
scanner.addColumn(Bytes.toBytes("filename"));
scanner.addColumn(Bytes.toBytes("description"));
scanner.addColumn(Bytes.toBytes("createtime"));
//scanner.setMaxVersions();
//scanner.setMaxVersions(4);
ResultScanner rsScanner = table.getScanner(scanner);
Result rs = rsScanner.next();
for(;null != rs; rs = rsScanner.next()){
System.out.println("rs.getRow()[" + new String(rs.getRow()) + "]");
System.out.println("[" + new String(rs.getValue(Bytes.toBytes("widgetname"))) + "]");
System.out.println("[" + new String(rs.getValue(Bytes.toBytes("filename"))) + "]");
System.out.println("[" + new String(rs.getValue(Bytes.toBytes("description"))) + "]");
String timeStr = new String(rs.getValue(Bytes.toBytes("createtime")));
System.out.println("[" + timeStr + "]");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try{
Date after = dateFormat.parse(timeStr);
System.out.println(after);
}
catch(Exception exp){
exp.printStackTrace();
}
}
}
public void testGet()throws IOException{
HBaseConfiguration config = new HBaseConfiguration();
config.set("hbase.master", "10.192.1.1:60000");
config.set("hbase.zookeeper.quorum", "10.192.1.1");
HTable table = new HTable(config, "commodity");
Get get = new Get(new String("xxxx.wgt").getBytes());
get.addColumn(Bytes.toBytes("widgetname"));
get.addColumn(Bytes.toBytes("filename"));
get.addColumn(Bytes.toBytes("description"));
get.addColumn(Bytes.toBytes("createtime"));
get.setMaxVersions(2);
System.out.println("00000000000000");
Result dbResult = table.get(get);
System.out.println("11111111111111");
System.out.println(dbResult.size());
System.out.println("2222222222222222");
System.out.println(new String(dbResult.value()));
System.out.println("3333333333333333");
System.out.println(dbResult.containsColumn(Bytes.toBytes("description"), new byte[]{}));
System.out.println("44444444444444444");
System.out.println(dbResult.isEmpty());
System.out.println("55555555555555555");
System.out.println(dbResult.list());
System.out.println("66666666666666666");
System.out.println(dbResult.raw());
System.out.println("77777777777777777");
System.out.println(dbResult.toString());
System.out.println("88888888888888888");
System.out.println(dbResult.raw().clone());
System.out.println("99999999999999999");
}
}