




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、實(shí)驗(yàn)三HBase環(huán)境搭建、sehll操作及Java API編程實(shí)驗(yàn)步驟:1.搭建Zookeeper和HBase1.ntp時(shí)間同步服務(wù)器搭建與使用安裝ntp服務(wù)端(master)# apt-get install ntp啟動(dòng)ntp服務(wù)# /etc/init.d/ntp start修改配置文件# vim /etc/ntp.conf修改內(nèi)容如下:重啟ntp服務(wù)# /etc/init.d/ntp restart1.2安裝ntp客戶端(slaver1、slaver2)使用ntpdate命令,如果不存在這個(gè)命令,則先安裝apt-get install ntp同步服務(wù)器時(shí)間# /usr/sbin/ntpda
2、te 72設(shè)置定時(shí)同步# vim /etc/crontab1.3 ulimit 和 nproc設(shè)置(集群均配置)HBase是數(shù)據(jù)庫,會(huì)在同一時(shí)間使用很多的文件句柄。大多數(shù)Ubuntu系統(tǒng)使用的默認(rèn)值1024是不能滿足的,所以你需要修改你的最大文件句柄限制??梢栽O(shè)置到10k. 你還需要修改 hbase 用戶的 nproc,如果過低會(huì)造成 OutOfMemoryError異常。需要澄清的,這兩個(gè)設(shè)置是針對操作系統(tǒng)的,不是Hbase本身的。有一個(gè)常見的錯(cuò)誤是Hbase運(yùn)行的用戶,和設(shè)置最大值的用戶不是一個(gè)用戶。在Hbase啟動(dòng)的時(shí)候,第一行日志會(huì)現(xiàn)在ulimit信息,所以你最好檢
3、查一下。1)修改limits.conf文件# vim /etc/security/limits.conf 添加如下內(nèi)容:2)修改common-session文件# vim /etc/pam.d/common-session添加如下內(nèi)容:重啟系統(tǒng)1.4 Zookeeper集群環(huán)境安裝過程詳解1)解壓zookeepertar zxvf zookeeper-3.4.5.tar.gz2) 修改zoo.cfg配置文件進(jìn)入到zookeeper的conf目錄下將zoo_sample.cfg文件拷貝一份,命名為為zoo.cfgvim zoo.cfg修改內(nèi)容如下:配置文件中"server.id=hos
4、t:port:port"中的第一個(gè)port是從機(jī)器(follower)連接到主機(jī)器(leader)的端口號(hào),第二個(gè)port是進(jìn)行l(wèi)eadership選舉的端口號(hào)。接下來在dataDir所指定的目錄下創(chuàng)建一個(gè)文件名為myid的文件,文件中的內(nèi)容只有一行,為本主機(jī)對應(yīng)的id值,也就是上圖中server.id中的id。例如:在服務(wù)器1中的myid的內(nèi)容應(yīng)該寫入1,在服務(wù)器2中的myid的內(nèi)容應(yīng)該寫入2,在服務(wù)器3中的myid的內(nèi)容應(yīng)該寫入3。3)同時(shí)創(chuàng)建log目錄# mkdir log4)修改環(huán)境變量# vim /etc/profile# source /etc/profile# scp
5、-r /root/zookeeper-3.4.5/ cc-slaver1:/root/# scp -r /root/zookeeper-3.4.5/ cc-slaver2:/root/在對應(yīng)slaver節(jié)點(diǎn)同時(shí)修改profile文件,添加export ZOOKEEPER_HOME=/root/zookeeper-3.4.5export CLASSPATH=.:$ZOOKEEPER_HOME/lib:$CLASSPATHexport PATH=$ZOOKEEPER_HOME/bin:$ZOOKEEPER_HOME/conf:$PATH同時(shí):在slaver1節(jié)點(diǎn)中# pwd/soft/zookee
6、per# mkdir data# echo "2" > myid在slaver2節(jié)點(diǎn)中# pwd/soft/zookeeper# mkdir data# echo "3" > myid1.5 啟動(dòng)zookeeper集群在ZooKeeper集群的每個(gè)結(jié)點(diǎn)上,執(zhí)行啟動(dòng)ZooKeeper服務(wù)的腳本:# zkServer.sh start如下圖所示:其中,QuorumPeerMain是zookeeper進(jìn)程,啟動(dòng)正常。出現(xiàn)錯(cuò)誤可以通過# tail -f /soft/zookeeper/zookeeper.out如上依次啟動(dòng)了所有機(jī)器上的Zookeep
7、er之后可以通過ZooKeeper的腳本來查看啟動(dòng)狀態(tài),包括集群中各個(gè)結(jié)點(diǎn)的角色(或是Leader,或是Follower),如下所示,是在ZooKeeper集群中的每個(gè)結(jié)點(diǎn)上查詢的結(jié)果:通過上面狀態(tài)查詢結(jié)果可見,cc-slaver1是集群的Leader,其余的兩個(gè)結(jié)點(diǎn)是Follower。另外,可以通過客戶端腳本,連接到ZooKeeper集群上。對于客戶端來說,ZooKeeper是一個(gè)整體(ensemble),連接到ZooKeeper集群實(shí)際上感覺在獨(dú)享整個(gè)集群的服務(wù),所以,你可以在任何一個(gè)結(jié)點(diǎn)上建立到服務(wù)集群的連接,例如:# zkCli.sh -server cc-slaver2:21811.
8、6 停止zookeeper進(jìn)程在ZooKeeper集群的每個(gè)結(jié)點(diǎn)上,執(zhí)行停止ZooKeeper服務(wù)的腳本:# zkServer.sh stop至此,Zookeeper集群安裝完成。1.7HBase的安裝和配置# tar -zxvf hbase-1.2.4.tar.gz1)配置conf/hbase-env.sh修改內(nèi)容如下:一個(gè)分布式運(yùn)行的Hbase依賴一個(gè)zookeeper集群。所有的節(jié)點(diǎn)和客戶端都必須能夠訪問zookeeper。默認(rèn)的情況下Hbase會(huì)管理一個(gè)zookeep集群,即Hbase默認(rèn)自帶一個(gè)zookeep集群。這個(gè)集群會(huì)隨著Hbase的啟動(dòng)而啟動(dòng)。而在實(shí)際的商業(yè)項(xiàng)目中通常自己管理
9、一個(gè)zookeeper集群更便于優(yōu)化配置提高集群工作效率,但需要配置Hbase。需要修改conf/hbase-env.sh里面的HBASE_MANAGES_ZK 來切換。這個(gè)值默認(rèn)是true的,作用是讓Hbase啟動(dòng)的時(shí)候同時(shí)也啟動(dòng)zookeeper.在本實(shí)驗(yàn)中,我們采用獨(dú)立運(yùn)行zookeeper集群的方式,故將其屬性值改為false。2)配置conf/hbase-site.xml配置如下:<configuration><property><name>hbase.rootdir</name><value>hdfs:/10.49.85.
10、172:9000/hbase</value></property><property><name>hbase.master</name><value>hdfs:/72:60000</value></property><property><name>.port</name><value>60010</value></property><property><na
11、me>.port</name><value>60030</value></property><property><name>perty.dataDir</name><value>file:/root/zookeeper-3.4.5/data</value></property><property><name>hbase.cluster.distributed<
12、;/name><value>true</value></property><property><name>hbase.zookeeper.quorum</name><value>cc-master,cc-slaver1,cc-slaver2</value></property></configuration>3)配置conf/regionservers寫入:cc-slaver1、cc-slaver2在這里列出了你希望運(yùn)行的全部 HRegionServer,一行寫一個(gè)ho
13、st (就像Hadoop里面的 slaver 一樣). 列在這里的server會(huì)隨著集群的啟動(dòng)而啟動(dòng),集群的停止而停止。4)hadoop配置文件拷入# cp /hadoop-2.6.5/etc/hadoop/hdfs-site.xml /hbase-1.2.4/conf# cp /hadoop-2.6.5/etc/hadoop/core-site.xml /hbase-1.2.4/conf5)分發(fā)hbase# scp -r /root/hbase-1.2.4 cc-slaver1:/root# scp -r /root/hbase-1.2.4 cc-slaver2:/root配置環(huán)境變量# v
14、im /etc/profile在末尾添加如下內(nèi)容6)運(yùn)行和測試在master上執(zhí)行:(1)# start-all.sh (2)# zkServer.sh start(各個(gè)節(jié)點(diǎn)均執(zhí)行)(3)# start-hbase.sh (涵蓋web管理界面的啟動(dòng))使用jps查看進(jìn)程通過瀏覽器查看60010,60030端口查看72:60010/瀏覽器主節(jié)點(diǎn)信息7)多節(jié)點(diǎn)啟動(dòng)HMaster# hbase-daemon.sh start master在其他子節(jié)點(diǎn)同時(shí)啟動(dòng)HMaster可以做等待備份作用;2.使用HBase shell命令進(jìn)行表的創(chuàng)建,增加刪除修改操作。Hbase腳本
15、啟動(dòng):hbase shell創(chuàng)建表在member表中添加幾條數(shù)據(jù)修改表數(shù)據(jù)刪除數(shù)據(jù)3使用Java API進(jìn)行表的創(chuàng)建,增加刪除修改操作。向表中添加數(shù)據(jù):更新表updateTable:刪除所有列deleteAllColumn:刪除列deleteColumn:刪除表deleteTable:刪除所有表getAllTables:獲取結(jié)果getResult:獲取某一列的值getResultByColumn:查詢某列的多版本getResultByVersion:遍歷查詢getResultByScann:具體代碼如下:import java.io.IOException;import java.u
16、til.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.HTableDescript
17、or;import org.apache.hadoop.hbase.KeyValue;import org.apache.hadoop.hbase.MasterNotRunningException;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.ZooKeeperConnectionException;import org.apache.hadoop.hbase.client.Delete;import org.apache.hadoop.hbase.client.Get;import org.a
18、pache.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.
19、Bytes;public class Hbase / 聲明靜態(tài)配置 static Configuration conf = null; static conf = HBaseConfiguration.create(); conf.set("hadoop.home.dir","hdfs:/72:9000/hbase"); /conf.set("hadoop.home.dir", "hdfs:/ns/hbas"); conf.set("perty.c
20、lientPort", "2181"); /hbase.zookeeper.quorum值不能采用IP方式,必須 使用名稱 conf.set("hbase.zookeeper.quorum", "cc-master,cc-slaver1,cc-slaver2"); /conf.set("hbase.master", "60000"); / System.setProperty("hadoop.home.dir", "hdfs:/ns/hbase"
21、); / Hbase獲取所有的表信息 public static List getAllTables() throws MasterNotRunningException, ZooKeeperConnectionException, IOException HBaseAdmin admin = new HBaseAdmin(conf); List<String> tables = null; if (admin != null) try HTableDescriptor allTable = admin.listTables(); if (allTable.length >
22、0) tables = new ArrayList<String>(); for (HTableDescriptor hTableDescriptor : allTable) tables.add(hTableDescriptor.getNameAsString(); System.out.println(hTableDescriptor.getNameAsString(); catch (IOException e) e.printStackTrace(); return tables; /* * 創(chuàng)建表 * * tableName 表名 * * family 列族列表 */ p
23、ublic static void creatTable(String tableName, String family) throws Exception HBaseAdmin admin = new HBaseAdmin(conf);/ HTableDescriptor desc = new HTableDescriptor(tableName); HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName); / System.out.println("="); for (int i
24、 = 0; i < family.length; i+) desc.addFamily(new HColumnDescriptor(familyi); if (admin.tableExists(tableName) System.out.println("table Exists!"); System.exit(0); else System.out.println("="); admin.createTable(desc); System.out.println("create table Success!"); /* *
25、為表添加數(shù)據(jù)(適合知道有多少列族的固定表) * * rowKey rowKey * * tableName 表名 * * column1 第一個(gè)列族列表 * * value1 第一個(gè)列的值的列表 * * column2 第二個(gè)列族列表 * * value2 第二個(gè)列的值的列表 */ public static void addData(String rowKey, String tableName, String column1, String value1, String column2, String value2) throws IOException Put put = new Put
26、(Bytes.toBytes(rowKey);/ 設(shè)置rowkey HTable table = new HTable(conf, Bytes.toBytes(tableName); / HTabel負(fù)責(zé)跟記錄相關(guān)的操作如增刪改查等 / 獲取所有的列族 HColumnDescriptor columnFamilies = table.getTableDescriptor() .getColumnFamilies(); for (int i = 0; i < columnFamilies.length; i+) String familyName = columnFamiliesi.get
27、NameAsString(); / 獲取列族名 if (familyName.equals("article") / article列族put數(shù)據(jù) for (int j = 0; j < column1.length; j+) put.add(Bytes.toBytes(familyName), Bytes.toBytes(column1j), Bytes.toBytes(value1j); if (familyName.equals("author") / author列族put數(shù)據(jù) for (int j = 0; j < column2.
28、length; j+) put.add(Bytes.toBytes(familyName), Bytes.toBytes(column2j), Bytes.toBytes(value2j); table.put(put); System.out.println("add data Success!"); /* * 根據(jù)rwokey查詢 * * rowKey rowKey * * tableName 表名 */ public static Result getResult(String tableName, String rowKey) throws IOException
29、Get get = new Get(Bytes.toBytes(rowKey); HTable table = new HTable(conf, Bytes.toBytes(tableName);/ 獲取表/ Result result = table.get(get);/ for (KeyValue kv : result.list() / System.out.println("family:" + Bytes.toString(kv.getFamily();/ System.out/ .println("qualifier:" + Bytes.to
30、String(kv.getQualifier();/ System.out.println("value:" + Bytes.toString(kv.getValue();/ System.out.println("Timestamp:" + kv.getTimestamp();/ System.out.println("-");/ Result result = table.get(get); for (Cell cell : result.rawCells() System. out .println( "Rowkey
31、: " +Bytes. toString (result.getRow()+ " Familiy:Quilifier : " +Bytes.toString (CellUtil.cloneQualifier (cell)+ " Value : " +Bytes.toString (CellUtil.cloneValue (cell) + " Timestamp:" + cell.getTimestamp() ); return result; /* * 遍歷查詢hbase表 * * tableName 表名 */ publi
32、c static void getResultScann(String tableName) throws IOException Scan scan = new Scan(); ResultScanner rs = null; HTable table = new HTable(conf, Bytes.toBytes(tableName); try rs = table.getScanner(scan); for (Result r : rs) for (KeyValue kv : r.list() System.out.println("row:" + Bytes.to
33、String(kv.getRow(); System.out.println("family:" + Bytes.toString(kv.getFamily(); System.out.println("qualifier:" + Bytes.toString(kv.getQualifier(); System.out .println("value:" + Bytes.toString(kv.getValue(); System.out.println("timestamp:" + kv.getTimestamp
34、(); System.out .println("-"); finally rs.close(); /* * 遍歷查詢hbase表 * * tableName 表名 */ public static void getResultScann(String tableName, String start_rowkey, String stop_rowkey) throws IOException Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes(start_rowkey); scan.setStopRow(Bytes.
35、toBytes(stop_rowkey); ResultScanner rs = null; HTable table = new HTable(conf, Bytes.toBytes(tableName); try rs = table.getScanner(scan); for (Result r : rs) for (KeyValue kv : r.list() System.out.println("row:" + Bytes.toString(kv.getRow(); System.out.println("family:" + Bytes.t
36、oString(kv.getFamily(); System.out.println("qualifier:" + Bytes.toString(kv.getQualifier(); System.out .println("value:" + Bytes.toString(kv.getValue(); System.out.println("timestamp:" + kv.getTimestamp(); System.out .println("-"); finally rs.close(); /* * 查詢表
37、中的某一列 * * tableName 表名 * * rowKey rowKey */ public static void getResultByColumn(String tableName, String rowKey, String familyName, String columnName) throws IOException HTable table = new HTable(conf, Bytes.toBytes(tableName); Get get = new Get(Bytes.toBytes(rowKey); get.addColumn(Bytes.toBytes(fa
38、milyName), Bytes.toBytes(columnName); / 獲取指定列族和列修飾符對應(yīng)的列 Result result = table.get(get); for (KeyValue kv : result.list() System.out.println("family:" + Bytes.toString(kv.getFamily(); System.out .println("qualifier:" + Bytes.toString(kv.getQualifier(); System.out.println("val
39、ue:" + Bytes.toString(kv.getValue(); System.out.println("Timestamp:" + kv.getTimestamp(); System.out.println("-"); table.close(); /* * 更新表中的某一列 * * tableName 表名 * * rowKey rowKey * * familyName 列族名 * * columnName 列名 * * value 更新后的值 */ public static void updateTable(String ta
40、bleName, String rowKey, String familyName, String columnName, String value) throws IOException HTable table = new HTable(conf, Bytes.toBytes(tableName); Put put = new Put(Bytes.toBytes(rowKey); put.add(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes.toBytes(value); table.put(put); System
41、.out.println("update table Success!"); /* * 查詢某列數(shù)據(jù)的多個(gè)版本 * * tableName 表名 * * rowKey rowKey * * familyName 列族名 * * columnName 列名 */ public static void getResultByVersion(String tableName, String rowKey, String familyName, String columnName) throws IOException HTable table = new HTable(conf,
42、 Bytes.toBytes(tableName); Get get = new Get(Bytes.toBytes(rowKey); get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName); get.setMaxVersions(5); Result result = table.get(get); for (KeyValue kv : result.list() System.out.println("family:" + Bytes.toString(kv.getFamily(); Syst
43、em.out.println("qualifier:" + Bytes.toString(kv.getQualifier(); System.out.println("value:" + Bytes.toString(kv.getValue(); System.out.println("Timestamp:" + kv.getTimestamp(); System.out.println("-"); /* * 刪除指定的列 * * tableName 表名 * * rowKey rowKey * * familyN
44、ame 列族名 * * columnName 列名 */ public static void deleteColumn(String tableName, String rowKey, String falilyName, String columnName) throws IOException HTable table = new HTable(conf, Bytes.toBytes(tableName); Delete deleteColumn = new Delete(Bytes.toBytes(rowKey); deleteColumn.deleteColumns(Bytes.to
45、Bytes(falilyName), Bytes.toBytes(columnName); table.delete(deleteColumn); System.out.println(falilyName + ":" + columnName + "is deleted!"); /* * 刪除指定的列 * * tableName 表名 * * rowKey rowKey */ public static void deleteAllColumn(String tableName, String rowKey) throws IOException HTable table = new HTable(conf, Bytes.toBytes(tableName); Delete deleteAll = new Delete(Bytes.toBytes(rowKey); table.delete(deleteAll); System.out.println("all columns are deleted!"); /* * 刪除表 * * tableName 表名 */ public static void d
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 農(nóng)業(yè)文化遺產(chǎn)可視化知識(shí)圖譜構(gòu)建
- 煤氣化渣二氧化硅對聚丙烯除味性能的研究與應(yīng)用
- 公園生活垃圾管理辦法
- 十年職場經(jīng)驗(yàn)分享與職業(yè)規(guī)劃
- 江西涉密采購管理辦法
- 通信工程技術(shù)規(guī)范
- 積極心理學(xué)應(yīng)用:心理健康教育長效機(jī)制構(gòu)建
- 利率市場化改革對中小企業(yè)融資效率的影響機(jī)制研究
- 基于乘客決策行為的城市軌道交通系統(tǒng)韌性評(píng)估研究
- 2025年 重大安全事故
- TL4型彈性套柱銷聯(lián)軸器零件工藝規(guī)程及加工柱銷孔液動(dòng)夾具設(shè)計(jì)
- 中職《接觸器聯(lián)鎖正反轉(zhuǎn)控制線路》公開課PPT
- 05-衣之鏢-輔行訣湯液經(jīng)法用藥圖釋義
- LS/T 3240-2012湯圓用水磨白糯米粉
- GB/T 15298-1994電子設(shè)備用電位器第一部分:總規(guī)范
- 2023高中學(xué)業(yè)水平合格性考試歷史重點(diǎn)知識(shí)點(diǎn)歸納總結(jié)(復(fù)習(xí)必背)
- 自然指數(shù)NatureIndex(NI)收錄的68種自然科學(xué)類期刊
- 手術(shù)報(bào)告審批單
- 《專業(yè)導(dǎo)論光電信息科學(xué)與工程》教學(xué)大綱
- 廣東省湛江市各縣區(qū)鄉(xiāng)鎮(zhèn)行政村村莊村名明細(xì)
- 少兒美術(shù)國畫- 少兒希望 《紫藤課件》
評(píng)論
0/150
提交評(píng)論