一、准备数据
在HDFS文件系统创建一个input目录,并从本地上传任意一个文件到目录中,为后面的Pig单词统计准备数据:
hdfs dfs -mkdir /input
hdfs dfs -ls /
我们就任意分析一个数据,比如Hadoop下的README.txt文件,将它上传至HDFS
cd /usr/cstor/hadoop
cat README.txt
hdfs dfs -put README.txt /input
hdfs dfs -ls /input
二、启动 Grunt shell
cd /root/soft/pig
pig -x local
读取并转换数据
a = load 'hdfs://master:8020/pig/README.txt' as (line: chararray) ; 按行读取HDFS文件
b = foreach a generate flatten(TOKENIZE(line,'\t ,.'))as word; 将每行单词用tab、逗号,句号以及空格分隔单词
dump b ;
c = group b by word ; 按单词分组,将相同的单词归并到一起
dump c ;
d = foreach c generate group, COUNT(b) as count ; 统计每个单词的个数
dump d ;
e = order d by count desc ; 将统计出的次数来排序(降序)
dump e ;
ok,分析完成,出现最多的是the 出现了8次。