背景需求和現狀
目前的日誌系統還稱不上系統,只是在幾台伺服器上存著所有的日誌,依靠NFS共用資料,並運算,帶來的問題諸多:
a) 資料存放淩亂,缺乏系統的目錄管理;
b) 儲存空間有限,並且擴充非常麻煩;
c) CV/PV等日誌分散存放,合并不方便;
d) 媒體服務日誌資料集中存放,資料龐大而難以做到輕量級備份;
e) 遺失資料的情況時有發生,且無從恢複;
f) 資料抓取效能低下,時常成為運算瓶頸;
需求分析
a) 日誌採集&儲存
日誌名稱 每天日誌大小 備份 管理人
.............
b) 日誌預先處理
見附件: 唯寫了PV&CV日誌,沒有看到別的日誌格式。
Excel:****log.xlsx
c)
iv. 格式化Hadoop
錯誤如果出現:Error: JAVA_HOME is not set. 表示沒有配置java home。
我們把javahome配置為全域;
vi /etc/environment
增加jave_home和/usr/local/hadoop/bin:
JAVA_HOME="/usr/lib/jvm/java-6-sun"
v. 啟動Hadoop
start-all.sh
vi. 檢查Hadoop是否正常
Netstat –nl |more
tcp6 0 0 127.0.0.1:9000 :::* LISTEN
tcp6 0 0 127.0.0.1:9001 :::* LISTEN
tcp6 0 0 :::50090 :::* LISTEN
tcp6 0 0 :::50070 :::* LISTEN
vii. 測試
hadoop fs -put CHANGES.txt input/
hadoop fs -ls input
這個例子是計算有多少個單詞的
hadoop jar hadoop-*-examples.jar grep input output '[a-z.]+'
root@hadoop-desktop:/usr/local/hadoop# hadoop fs -cat output/* |more
cat: Source must be a file.
3828 .
1969 via
1375 to
viii.
l) Api介紹
見附件:Word: hadoop的API.docx
m)
Hive 安裝
n) 下載,去官方下載最新版,我就不寫了。
o) 解壓 ;
tar zxvf hive-0.5.0-bin.tar.gz ;
ln –s hive-0.5.0-bin hive
p) 配置hive環境
vi /etc/environment
HIVE_HOME="/usr/local/hive/"
q) 建立hive儲存
hadoop fs -mkdir /user/hive/warehouse
hadoop fs -chmod g+w /user/hive/warehouse
r) 啟動hive
hive
進入: hive> 標識符
建立pokes表。
hivr> CREATE TABLE pokes (foo INT, bar STRING);
載入測試資料,載入的檔案是2列。
hive> LOAD DATA LOCAL INPATH './examples/files/kv1.txt' OVERWRITE INTO TABLE pokes;
hive> select count(1) from pokes;
Total MapReduce jobs = 1
Launching Job 1 out of 1
Number of reduce tasks determined at compile time: 1
In order to change the average load for a reducer (in bytes):
set hive.exec.reducers.bytes.per.reducer=<number>
In order to limit the maximum number of reducers:
set hive.exec.reducers.max=<number>
In order to set a constant number of reducers:
set mapred.reduce.tasks=<number>
Starting Job = job_201004131133_0018, Tracking URL = http://localhost:50030/jobdetails.jsp?jobid=job_201004131133_0018
Kill Command = /usr/local/hadoop/bin/../bin/hadoop job -Dmapred.job.tracker=localhost:9001 -kill job_201004131133_0018
2010-04-13 16:32:12,188 Stage-1 map = 0%, reduce = 0%
2010-04-13 16:32:29,536 Stage-1 map = 100%, reduce = 0%
2010-04-13 16:32:38,768 Stage-1 map = 100%, reduce = 33%
2010-04-13 16:32:44,916 Stage-1 map = 100%, reduce = 100%
Ended Job = job_201004131133_0018
OK
500
Time taken: 38.379 seconds
hive> select count(bar),bar from pokes group by bar;
Total MapReduce jobs = 1
Launching Job 1 out of 1
Number of reduce tasks not specified. Estimated from input data size: 1
In order to change the average load for a reducer (in bytes):
set hive.exec.reducers.bytes.per.reducer=<number>
In order to limit the maximum number of reducers:
set hive.exec.reducers.max=<number>
In order to set a constant number of reducers:
set mapred.reduce.tasks=<number>
Starting Job = job_201004131133_0017, Tracking URL = http://localhost:50030/jobdetails.jsp?jobid=job_201004131133_0017
Kill Command = /usr/local/hadoop/bin/../bin/hadoop job -Dmapred.job.tracker=localhost:9001 -kill job_201004131133_0017
2010-04-13 16:26:55,791 Stage-1 map = 0%, reduce = 0%
2010-04-13 16:27:11,165 Stage-1 map = 100%, reduce = 0%
2010-04-13 16:27:20,268 Stage-1 map = 100%, reduce = 33%
2010-04-13 16:27:25,348 Stage-1 map = 100%, reduce = 100%
Ended Job = job_201004131133_0017
OK
3 val_0
1 val_10
……………
Time taken: 37.979 seconds
hive> load data local inpath 'ok.txt.gz' overwrite into table page_test2 partition(dt='2010-04-16');
Copying data from file:/usr/local/ok.txt.gz
Loading data to table page_test2 partition {dt=2010-04-16}
OK
Time taken: 3.649 seconds
下面是Hadoop儲存的hive表的檔案大小:
root@hadoop:/tmp/hadoop-root/dfs/data/current# du -ch blk_-945326243445352181
22M blk_-945326243445352181
22M total
w) 載入文字檔:
hive> load data local inpath 'ok.txt' overwrite into table page_test partition(dt='2010-04-17');
Copying data from file:/usr/local/ok.txt
Loading data to table page_test partition {dt=2010-04-17}
OK
Time taken: 41.593 seconds
下面是Hadoop儲存的hive表的檔案大小:
root@hadoop:/tmp/hadoop-root/dfs/data/current# du -ch blk_7538941016314062501
64M blk_7538941016314062501
64M total
x) 源檔案大小:
root@hadoop:/usr/local# du -ch ok.txt
196M ok.txt
196M total
root@hadoop:/usr/local# du -ch ok.txt.gz
22M ok.txt.gz
22M total
y) Hive查詢比較:
可以從結果看出壓縮的資料查詢速度比不壓縮的還快一點,奇怪了。
Gz檔案匯入並建立分區後使用hive查詢: hive> select count(1) from page_test2 a where a.dt='2010-04-16';
Total MapReduce jobs = 1
Launching Job 1 out of 1
Number of reduce tasks determined at compile time: 1
In order to change the average load for a reducer (in bytes):
set hive.exec.reducers.bytes.per.reducer=<number>
In order to limit the maximum number of reducers:
set hive.exec.reducers.max=<number>
In order to set a constant number of reducers:
set mapred.reduce.tasks=<number>
Starting Job = job_201004131133_0026, Tracking URL = http://localhost:50030/jobdetails.jsp?jobid=job_201004131133_0026
Kill Command = /usr/local/hadoop/bin/../bin/hadoop job -Dmapred.job.tracker=localhost:9001 -kill job_201004131133_0026
2010-04-16 13:43:39,435 Stage-1 map = 0%, reduce = 0%
2010-04-16 13:47:30,921 Stage-1 map = 100%, reduce = 100%
Ended Job = job_201004131133_0026
OK
17166483
Time taken: 239.447 seconds
Txt檔案匯入並建立分區後使用hive QL查詢: hive> select count(1) from page_test a where a.dt='2010-04-16';
Total MapReduce jobs = 1
Launching Job 1 out of 1
Number of reduce tasks determined at compile time: 1
In order to change the average load for a reducer (in bytes):
set hive.exec.reducers.bytes.per.reducer=<number>
In order to limit the maximum number of reducers:
set hive.exec.reducers.max=<number>
In order to set a constant number of reducers:
set mapred.reduce.tasks=<number>
Starting Job = job_201004131133_0025, Tracking URL = http://localhost:50030/jobdetails.jsp?jobid=job_201004131133_0025
Kill Command = /usr/local/hadoop/bin/../bin/hadoop job -Dmapred.job.tracker=localhost:9001 -kill job_201004131133_0025
2010-04-16 13:37:11,927 Stage-1 map = 0%, reduce = 0%
2010-04-16 13:42:01,382 Stage-1 map = 100%, reduce = 22%
2010-04-16 13:42:13,683 Stage-1 map = 100%, reduce = 100%
Ended Job = job_201004131133_0025
OK
17166483
Time taken: 314.291 seconds
Txt 沒有建立分區使用hive查詢 沒有記錄下來,是400多秒
z) a
Hive 開發
a) 開啟hive service:
在10000連接埠開啟 hive服務
HIVE_PORT=10000 ./bin/hive --service hiveserver
// load data into table
// NOTE: filepath has to be local to the hive server
// NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line
String filepath = "/tmp/a.txt";
sql = "load data local inpath '" + filepath + "' into table " + tableName;
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
// select * query
sql = "select * from " + tableName;
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
}
// regular hive query
sql = "select count(1) from " + tableName;
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1));
}
}
}