![非關(guān)系型數(shù)據(jù)庫大作業(yè)_第1頁](http://file1.renrendoc.com/fileroot_temp2/2021-2/25/5c06b0e1-02eb-4137-99b3-7d419e0dea86/5c06b0e1-02eb-4137-99b3-7d419e0dea861.gif)
![非關(guān)系型數(shù)據(jù)庫大作業(yè)_第2頁](http://file1.renrendoc.com/fileroot_temp2/2021-2/25/5c06b0e1-02eb-4137-99b3-7d419e0dea86/5c06b0e1-02eb-4137-99b3-7d419e0dea862.gif)
![非關(guān)系型數(shù)據(jù)庫大作業(yè)_第3頁](http://file1.renrendoc.com/fileroot_temp2/2021-2/25/5c06b0e1-02eb-4137-99b3-7d419e0dea86/5c06b0e1-02eb-4137-99b3-7d419e0dea863.gif)
![非關(guān)系型數(shù)據(jù)庫大作業(yè)_第4頁](http://file1.renrendoc.com/fileroot_temp2/2021-2/25/5c06b0e1-02eb-4137-99b3-7d419e0dea86/5c06b0e1-02eb-4137-99b3-7d419e0dea864.gif)
![非關(guān)系型數(shù)據(jù)庫大作業(yè)_第5頁](http://file1.renrendoc.com/fileroot_temp2/2021-2/25/5c06b0e1-02eb-4137-99b3-7d419e0dea86/5c06b0e1-02eb-4137-99b3-7d419e0dea865.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、.實驗三HBase環(huán)境搭建、sehll操作及Java API編程實驗步驟:1.搭建Zookeeper和HBase1.ntp時間同步服務(wù)器搭建與使用安裝ntp服務(wù)端(master)# apt-get install ntp啟動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命令,如果不存在這個命令,則先安裝apt-get install ntp同步服務(wù)器時間# /usr/sbin/ntpd
2、ate 72設(shè)置定時同步# vim /etc/crontab1.3 ulimit 和 nproc設(shè)置(集群均配置)HBase是數(shù)據(jù)庫,會在同一時間使用很多的文件句柄。大多數(shù)Ubuntu系統(tǒng)使用的默認值1024是不能滿足的,所以你需要修改你的最大文件句柄限制。可以設(shè)置到10k. 你還需要修改 hbase 用戶的 nproc,如果過低會造成 OutOfMemoryError異常。需要澄清的,這兩個設(shè)置是針對操作系統(tǒng)的,不是Hbase本身的。有一個常見的錯誤是Hbase運行的用戶,和設(shè)置最大值的用戶不是一個用戶。在Hbase啟動的時候,第一行日志會現(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配置文件進入到zookeeper的conf目錄下將zoo_sample.cfg文件拷貝一份,命名為為zoo.cfgvim zoo.cfg修改內(nèi)容如下:配置文件中server.id=host:por
4、t:port中的第一個port是從機器(follower)連接到主機器(leader)的端口號,第二個port是進行l(wèi)eadership選舉的端口號。接下來在dataDir所指定的目錄下創(chuàng)建一個文件名為myid的文件,文件中的內(nèi)容只有一行,為本主機對應(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)同時創(chuàng)建log目錄# mkdir log4)修改環(huán)境變量# vim /etc/profile# source /etc/profile# scp -r /root/zo
5、okeeper-3.4.5/ cc-slaver1:/root/# scp -r /root/zookeeper-3.4.5/ cc-slaver2:/root/在對應(yīng)slaver節(jié)點同時修改profile文件,添加export ZOOKEEPER_HOME=/root/zookeeper-3.4.5export CLASSPATH=.:$ZOOKEEPER_HOME/lib:$CLASSPATHexport PATH=$ZOOKEEPER_HOME/bin:$ZOOKEEPER_HOME/conf:$PATH同時:在slaver1節(jié)點中# pwd/soft/zookeeper# mkdir
6、data# echo 2 myid在slaver2節(jié)點中# pwd/soft/zookeeper# mkdir data# echo 3 myid1.5 啟動zookeeper集群在ZooKeeper集群的每個結(jié)點上,執(zhí)行啟動ZooKeeper服務(wù)的腳本:# zkServer.sh start如下圖所示:其中,QuorumPeerMain是zookeeper進程,啟動正常。出現(xiàn)錯誤可以通過# tail -f /soft/zookeeper/zookeeper.out如上依次啟動了所有機器上的Zookeeper之后可以通過ZooKeeper的腳本來查看啟動狀態(tài),包括集群中各個結(jié)點的角色(或是Le
7、ader,或是Follower),如下所示,是在ZooKeeper集群中的每個結(jié)點上查詢的結(jié)果:通過上面狀態(tài)查詢結(jié)果可見,cc-slaver1是集群的Leader,其余的兩個結(jié)點是Follower。另外,可以通過客戶端腳本,連接到ZooKeeper集群上。對于客戶端來說,ZooKeeper是一個整體(ensemble),連接到ZooKeeper集群實際上感覺在獨享整個集群的服務(wù),所以,你可以在任何一個結(jié)點上建立到服務(wù)集群的連接,例如:# zkCli.sh -server cc-slaver2:21811.6 停止zookeeper進程在ZooKeeper集群的每個結(jié)點上,執(zhí)行停止ZooKeep
8、er服務(wù)的腳本:# zkServer.sh stop至此,Zookeeper集群安裝完成。1.7HBase的安裝和配置# tar -zxvf hbase-1.2.4.tar.gz1)配置conf/hbase-env.sh修改內(nèi)容如下:一個分布式運行的Hbase依賴一個zookeeper集群。所有的節(jié)點和客戶端都必須能夠訪問zookeeper。默認的情況下Hbase會管理一個zookeep集群,即Hbase默認自帶一個zookeep集群。這個集群會隨著Hbase的啟動而啟動。而在實際的商業(yè)項目中通常自己管理一個zookeeper集群更便于優(yōu)化配置提高集群工作效率,但需要配置Hbase。需要修改c
9、onf/hbase-env.sh里面的HBASE_MANAGES_ZK 來切換。這個值默認是true的,作用是讓Hbase啟動的時候同時也啟動zookeeper.在本實驗中,我們采用獨立運行zookeeper集群的方式,故將其屬性值改為false。2)配置conf/hbase-site.xml配置如下:hbase.rootdirhdfs:/72:9000/hbasehbase.masterhdfs:/72:60000.port60030hbase.zook
10、perty.dataDirhbase.cluster.distributedtruehbase.zookeeper.quorumcc-master,cc-slaver1,cc-slaver23)配置conf/regionservers寫入:cc-slaver1、cc-slaver2在這里列出了你希望運行的全部 HRegionServer,一行寫一個host (就像Hadoop里面的 slaver 一樣). 列在這里的server會隨著集群的啟動而啟動,集群的停止而停止。4)hadoop配置文件拷入# cp /hadoop-2.6.5/etc/hadoop/hdfs-site.
11、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)境變量# vim /etc/profile在末尾添加如下內(nèi)容6)運行和測試在master上執(zhí)行:(1)# start-all.sh (2)# zkServer.sh start(各個節(jié)點均執(zhí)行)(3)# start-hbase.sh (涵蓋
12、web管理界面的啟動)使用jps查看進程通過瀏覽器查看60010,60030端口查看:60010/瀏覽器主節(jié)點信息7)多節(jié)點啟動HMaster# hbase-daemon.sh start master在其他子節(jié)點同時啟動HMaster可以做等待備份作用;2.使用HBase shell命令進行表的創(chuàng)建,增加刪除修改操作。Hbase腳本啟動:hbaseshell創(chuàng)建表在member表中添加幾條數(shù)據(jù)修改表數(shù)據(jù)刪除數(shù)據(jù)3使用Java API進行表的創(chuàng)建,增加刪除修改操作。向表中添加數(shù)據(jù):更新表updateTable:刪除所有列deleteAllColumn:刪除列deleteColumn:刪除表de
13、leteTable:刪除所有表getAllTables:獲取結(jié)果getResult:獲取某一列的值getResultByColumn:查詢某列的多版本getResultByVersion:遍歷查詢getResultByScann:具體代碼如下:import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.Cell;import org.apache.hado
14、op.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.KeyValue;import org.apache.hadoop.hbase.MasterNotRunningException;import org.apache.hadoop.hbase.TableNa
15、me;import org.apache.hadoop.hbase.ZooKeeperConnectionException;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.apa
16、che.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 Hbase / 聲明靜態(tài)配置 static Configuration conf = null; static conf = HBaseConfiguration.create(); conf.set(hadoop.home.dir,hd
17、fs:/72:9000/hbase); /conf.set(hadoop.home.dir, hdfs:/ns/hbas); conf.set(perty.clientPort, 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.d
18、ir, hdfs:/ns/hbase); / Hbase獲取所有的表信息 public static List getAllTables() throws MasterNotRunningException, ZooKeeperConnectionException, IOException HBaseAdmin admin = new HBaseAdmin(conf); List tables = null; if (admin != null) try HTableDescriptor allTable = admin.listTables(); if (allTable.length 0
19、) tables = new ArrayList(); for (HTableDescriptor hTableDescriptor : allTable) tables.add(hTableDescriptor.getNameAsString(); System.out.println(hTableDescriptor.getNameAsString(); catch (IOException e) e.printStackTrace(); return tables; /* * 創(chuàng)建表 * * tableName 表名 * * family 列族列表 */ public static vo
20、id 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 = 0; i family.length; i+)
21、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!); /* * 為表添加數(shù)據(jù)(適合知道有多少列族的固定表) * * rowKey rowKey * * tableName 表名 * * column1
22、 第一個列族列表 * * value1 第一個列的值的列表 * * column2 第二個列族列表 * * value2 第二個列的值的列表 */ public static void addData(String rowKey, String tableName, String column1, String value1, String column2, String value2) throws IOException Put put = new Put(Bytes.toBytes(rowKey);/ 設(shè)置rowkey HTable table = new HTable(conf, By
23、tes.toBytes(tableName); / HTabel負責(zé)跟記錄相關(guān)的操作如增刪改查等 / 獲取所有的列族 HColumnDescriptor columnFamilies = table.getTableDescriptor() .getColumnFamilies(); for (int i = 0; i columnFamilies.length; i+) String familyName = columnFamiliesi.getNameAsString(); / 獲取列族名 if (familyName.equals(article) / article列族put數(shù)據(jù) f
24、or (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.length; j+) put.add(Bytes.toBytes(familyName), Bytes.toBytes(column2j), Bytes.toBytes(value2j); table.put(p
25、ut); System.out.println(add data Success!); /* * 根據(jù)rwokey查詢 * * rowKey rowKey * * tableName 表名 */ public static Result getResult(String tableName, String rowKey) throws IOException Get get = new Get(Bytes.toBytes(rowKey); HTable table = new HTable(conf, Bytes.toBytes(tableName);/ 獲取表/ Result result
26、= 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(value: + Bytes.toString(kv.getValue();/ System.out.println(Timestamp: + kv.getTimestamp();/ Syste
27、m.out.println(-);/ Result result = table.get(get); for (Cell cell : result.rawCells() System. out .println( Rowkey : +Bytes. toString (result.getRow()+ Familiy:Quilifier : +Bytes.toString (CellUtil.cloneQualifier (cell)+ Value : +Bytes.toString (CellUtil.cloneValue (cell) + Timestamp: + cell.getTime
28、stamp() ); return result; /* * 遍歷查詢hbase表 * * tableName 表名 */ public 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 (Ke
29、yValue kv : r.list() System.out.println(row: + Bytes.toString(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.getTi
30、mestamp(); 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.toByt
31、es(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.toString(kv.getFamily(); Syste
32、m.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(); /* * 查詢表中的某一列 * * tableName 表名 * * rowKey rowKey */ public static void getResultByCol
33、umn(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(familyName), Bytes.toBytes(columnName); / 獲取指定列族和列修飾符對應(yīng)的列 Result result = table
34、.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(value: + Bytes.toString(kv.getValue(); System.out.println(Timestamp: + kv.getTimestamp(); System.out.println(-
35、); table.close(); /* * 更新表中的某一列 * * tableName 表名 * * rowKey rowKey * * familyName 列族名 * * columnName 列名 * * value 更新后的值 */ public static void updateTable(String tableName, String rowKey, String familyName, String columnName, String value) throws IOException HTable table = new HTable(conf, Bytes.toBy
36、tes(tableName); Put put = new Put(Bytes.toBytes(rowKey); put.add(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes.toBytes(value); table.put(put); System.out.println(update table Success!); /* * 查詢某列數(shù)據(jù)的多個版本 * * tableName 表名 * * rowKey rowKey * * familyName 列族名 * * columnName 列名 */ public s
37、tatic void getResultByVersion(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(familyName), Bytes.toBytes(columnName); get.setMaxVer
38、sions(5); 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(value: + Bytes.toString(kv.getValue(); System.out.println(Timestamp: + kv.getTim
39、estamp(); System.out.println(-); /* * 刪除指定的列 * * tableName 表名 * * rowKey rowKey * * familyName 列族名 * * 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.toBytes(falilyName), Bytes.toBytes(columnName); table.delete(deleteColumn); System.out.println(falilyName + : + columnName + is deleted!); /* * 刪除指定的列 * * tableName 表名 * * rowKey
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度農(nóng)業(yè)機械購置擔(dān)保合同糾紛起訴狀撰寫要領(lǐng)
- 散伙協(xié)議書(20篇)
- 2025年債權(quán)轉(zhuǎn)讓協(xié)議綜述
- 2025年公司變革資產(chǎn)接收合同模板
- 2025年度實習(xí)生接收單位協(xié)議格式
- 2025年軟泡聚醚項目申請報告模范
- 2025年物流服務(wù)商戰(zhàn)略聯(lián)盟策劃協(xié)議
- 2025年公司職員車輛共享合同
- 2025年社交APP項目規(guī)劃申請報告
- 2025年兒科用藥項目提案報告模范
- 紅色喜慶大氣軍令狀2022頒獎誓師大會動態(tài)PPT模板
- 綠化養(yǎng)護工作計劃15篇
- 部編版語文八年級下冊第六單元名著導(dǎo)讀《鋼鐵是怎樣煉成的》問答題 (含答案)
- 基于課程標(biāo)準(zhǔn)的教學(xué) 學(xué)習(xí)目標(biāo)的分解、敘寫與評價 課件
- 古詩詞誦讀《虞美人》課件-統(tǒng)編版高中語文必修上冊
- 病原微生物實驗室標(biāo)準(zhǔn)操作規(guī)程sop文件
- 制作拉線課件
- 2019統(tǒng)編版高中生物必修2遺傳與進化教學(xué)計劃含教學(xué)進度表
- 電子產(chǎn)品設(shè)計生產(chǎn)工藝流程課件
- 溫室大棚、花卉苗圃采暖方案(空氣源熱泵)
- 即興口語(姜燕)-課件-即興口語第五章PPT-中國傳媒大學(xué)
評論
0/150
提交評論