極簡python教程02:基礎變數,刪繁就簡
阿新 • • 發佈:2020-12-06
一、hive用本地檔案進行詞頻統計
1.準備本地txt檔案
mkdir wc
cd wc
echo "hadoop hbase" > f1.txt
echo "hadoop hive" > f2.txt
2.啟動hadoop,啟動hive
start-all.sh
hive
3.建立資料庫,建立文字表
use hive;
create table if not exists wctext(line string);
show tables;
4.對映本地檔案的資料到文字表中
load data local inpath '/home/hadoop/wc/f1.txt' into table wctext;
load data local inpath '/home/hadoop/wc/f2.txt' into table wctext;
select * from wctext;
5.hql語句進行詞頻統計交將結果儲存到結果表中
SELECT word, count(1) AS count FROM (SELECT explode(split(line, ‘ ’)) AS word FROM wctext) w GROUP BY word ORDER BY word;
create table wc as SELECT word, count(1) AS count FROM (SELECT explode(split(line, ' ')) AS word FROM wctext) w GROUP BY word ORDER BY word;
6.檢視統計結果
select * from wc;
二、hive用HDFS上的檔案進行詞頻統計
1.準備電子書或其它大的文字檔案
2.將文字檔案上傳到HDFS上
hdfs dfs -put ~/wc/f3.txt /input
3.建立文字表
create table if not exists docs(line string);
show tables;
4.對映HDFS中的檔案資料到文字表中
load data inpath '/input/f3.txt' into table docs;
5.hql語句進行詞頻統計交將結果儲存到結果表中
create table wctest as SELECT word, count(1) AS count FROM (SELECT explode(split(line, ' ')) AS word FROM docs) w GROUP BY word ORDER BY word;
SELECT word, count(1) AS count FROM (SELECT explode(split(line, ‘ ’)) AS word FROM docs) w GROUP BY word ORDER BY word;
6.檢視統計結果
select * from wctest;