柚子快報邀請碼778899分享:HBase數(shù)據(jù)庫安裝及編程實踐
(官網(wǎng):http://hbase.apache.org/)
一、偽分布式搭建
(1)下載hbase并解壓?
? ?tar -zxvf hbase-0.98.12.1-hadoop2-bin.tar.gz -C /opt/jxxy/
(2)配置環(huán)境變量
? ?vi /etc/profile
export HBASE_HOME=/opt/jxxy/hbase-0.98.12.1-hadoop2 PATH=$PATH:$HBASE_HOME/bin
? ?./etc/profile使之生效
(3)配置 hbase-env.sh (路徑:/opt/jxxy/hbase-0.98.12.1-hadoop2/conf/) ? ? export JAVA_HOME=/usr/java/jdk1.7.0_67 export HBASE_MANAGES_ZK=true
(4)配置 hbase-site.xml(路徑:/opt/jxxy/hbase-0.98.12.1-hadoop2/conf/)
?
?
?
?
(5)啟動HBase?
? ?start-hbase.sh ?//內(nèi)置zookeeper
? ?jps ?/查看進程
?二、hbase shell常用命令
(1)hbase shell ?//ctrl+Backspace:后退
? ? hbase(main):001:0>help
? ? hbase(main):001:0>status
? ? hbase(main):001:0>whoami
? ? hbase(main):001:0>list ? //列出表
? ? hbase(main):001:0>describe 'psn' ?//查看psn表
? ? hbase(main):001:0>disable 'psn'//使psn表無效
? ? hbase(main):001:0>drop 'psn'//刪除psn表
? ? hbase(main):001:0>create //創(chuàng)建表,列出各種用法
? ? hbase(main):001:0>create 'student', 'info'
? ? hbase(main):001:0>describe 'student'
? ? hbase(main):001:0>create 'techer', 'info1','info2'
? ? hbase(main):001:0>put 'student','0001','info:name','xiaohua'//插入數(shù)據(jù)
? ? hbase(main):001:0>put 'student','0001','info:age','22'
? ? hbase(main):001:0>get 'student','0001' ?//取數(shù)據(jù)
? ? hbase(main):001:0>scan 'student'//全表查看,類似于select * from student
? ? hbase(main):001:0>put 'student','0001','info:age','25' ?//修改age
(2) ctrl+alt+]:退出hbase shell ? ?? ? ? exit:回到shell
? ? //如果hbase.rootdir設(shè)置:file:///home/testuser/hbase,? ? ? ? cd /home/testuser/hbase/data/default/student ?? ? ? ? ls ? ? ? ? ? 1a1bbe7d2cf952cdc97d172136c05ad5 ? //region名稱
? ? //如果hbase.rootdir設(shè)置:hdfs://node01:9000/hbase,可以在瀏覽器node01:9000查看
? ? //在瀏覽器(node01:60010),查看region名稱
? ? cd ?1a1bbe7d2cf952cdc97d172136c05ad5 ? ? ls ? ? ? ? info ? //列簇
? ? //info里無內(nèi)容,上面輸入的數(shù)據(jù)還在內(nèi)存里
(3) ?hbase(main):001:0>flush 'student' //將內(nèi)存數(shù)據(jù)保存到磁盤
? ? ?hbase hfile -p -f 文件//查看info里面的文件
? ? ?hbase(main):001:0>delete 'student','0001','info:name' ?//刪除數(shù)據(jù)
? ? ?hbase(main):001:0>truncate ?/刪除所有數(shù)據(jù)
?三、Hbase編程
(1)打開eclipse新建一個java 項目
? ? ?導(dǎo)入hbase包和JUnit: ? ? ? ?菜單:window-preferences-java-build path-user libraries ? ? ? ? ? ? ? ? ? ? ? ?自定義一個jar包(比如hbase_jars)
? ? ? ? ? ?菜單:add external JARS
? ? ? ? ? ?選擇hbase安裝目錄\lib里所有jar包,除了ruby
? ? ? ? ? ?項目里導(dǎo)入hbase_jars包 //右擊項目名-build path-configure build path-java build path-libraries-add library-use library-
?(2) 新建一個com.jxxy.hbase.HBaseDemo類 ?? ?(3) 導(dǎo)入配置文件(hbase-site.xml,log4j.properties)
?(4)代碼
public class HBaseDemo{
HBaseAdmin admin;
Configuration conf;
@Before
public void init() throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
conf= HBaseConfiguration.create();
admin=new HBaseAdmin(conf);
}
/**
* 創(chuàng)建表
* @param tableName
* @param fields
* @throws IOException
*/
public void creatTable(String tableName,String[] fields) throws IOException{
//(2)刪除已存在的同名表
if(admin.tableExists(tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
HTableDescriptor desc=new HTableDescriptor(TableName.valueOf(tableName));
for(String family:fields){
HColumnDescriptor cf=new HColumnDescriptor(family.getBytes());
desc.addFamily(cf);
}
admin.createTable(desc);
}
/**
* 向表中指定單元格添加數(shù)據(jù)
* @param tableName
* @param row
* @param fields
* @param values
* @throws RetriesExhaustedWithDetailsException
* @throws InterruptedIOException
*/
public void addRecord(String tableName,String row,String[] fields,String[] values) throws IOException {
HTable htable=new HTable(conf,tableName.getBytes());
Put put=new Put(row.getBytes());
for(int i=0;i put.add(fields[i].split(":")[0].getBytes(),fields[i].split(":")[1].getBytes(),values[i].getBytes()); } htable.put(put); } /** * 瀏覽表中得某一列數(shù)據(jù) * @param tableName * @param column * @throws Exception */ public void scanColumn(String tableName,String column,String[] fields) throws Exception{ Scan scan=new Scan(); HTable htable=new HTable(conf,tableName.getBytes()); if (column.contains(":")) { System.out.println("\""+column+"\""); // 如果參數(shù) column 包含 ":",則按具體列名掃描 String[] parts = column.split(":"); scan.addColumn(Bytes.toBytes(parts[0]), Bytes.toBytes(parts[1])); try (ResultScanner scanner = htable.getScanner(scan)) { for (Result r : scanner) { byte[] value = r.getValue(Bytes.toBytes(parts[0]), Bytes.toBytes(parts[1])); if (value != null) { System.out.println(Bytes.toString(value)); } else { System.out.println("null"); } } } } else { // 如果參數(shù) column 不包含 ":",則按列族名掃描 scan.addFamily(Bytes.toBytes(column)); for(int i=0;i System.out.println(); try (ResultScanner scanner = htable.getScanner(scan)) { for (Result r : scanner) { for(int i=0;i byte[] value = r.getValue(Bytes.toBytes(column), Bytes.toBytes(fields[i].split(":")[1])); if (value != null) { System.out.print(Bytes.toString(value)+"\t\t"); } else { System.out.print("null\t\t"); } } System.out.println(); } } } System.out.println(); } /** * 將關(guān)系數(shù)據(jù)庫表裝換轉(zhuǎn)換成hbase數(shù)據(jù)庫并插入數(shù)據(jù) * @throws IOException */ @Test public void toHbaseTable() throws Exception { String info[]={"info"}; String score[]={"score"}; creatTable("Student",info); creatTable("Course",info); creatTable("SC",score); String fields0[]={"info:name","info:sex","info:age"}; String fields2[]={"score:Math","score:Computer Science","score:English"}; String fields1[]={"info:name","info:credit"}; addRecord("Student","0001",fields0,new String[]{"xiaoming","male","20"}); addRecord("Student","0002",fields0,new String[]{"xiaohong","female","21"}); addRecord("Course","0001",fields1,new String[]{"Math","2.0"}); addRecord("Course","0002",fields1,new String[]{"Computer Science","2.0"}); addRecord("Course","0003",fields1,new String[]{"English","2.0"}); addRecord("SC","0001",fields2,new String[]{"90","80","70"}); addRecord("SC","0002",new String[]{"score:Math","score:English"},new String[]{"50","90"}); scanColumn("Student","info",fields0); scanColumn("Course","info:name",fields1); scanColumn("SC","score",fields2); } @After public void close() throws IOException{ if (admin!=null) admin.close(); } } ? 四、hbase完全分布式 ? ?(1)修改 hbase-env.sh ? ? ? ? ?export HBASE_MANAGES_ZK=false ? (2)修改 hbase-site.xml ? ? ? ? ? ? ? ?(3) 修改 regionservers ? ? ? ? node02 ? ? ? ? node03 ? ? ? ? node04 ? ?(4) 新建 backup-masters ? ? ? ? node05 ? ?(5) 復(fù)制 hdfs-site.xml到 conf ? ? ? ?? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? 柚子快報邀請碼778899分享:HBase數(shù)據(jù)庫安裝及編程實踐 文章來源
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。