1.CentOS6.8下Hadoop2.7.2怎么运行自带的wordcount程序
在集成开发环境中写好了WordCount 程序,程序源代码如下:
TokenizerMapper 类:
public static class TokenizerMapper extends
Mapper{
private final static IntWritable count = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
StringTokenizer st = new StringTokenizer(value.toString()); while (st.hasMoreTokens()) {
word.set(st.nextToken());
context.write(word, count);
}
2.如何运行自带wordcount
1、在linux系统中,所在目录“/home/kcm”下创建一个文件input
[ubuntu@701~]$ mkdir input
2.在文件夹input中创建两个文本文件file1.txt和file2.txt,file1.txt中内容是“hello word”,file2.txt中内容是“hello hadoop”、“hello mapreduce”(分两行)。
[ubuntu@701~]$ cd input
[ubuntu@701~]$ vi file1.txt(编辑文件并保存)
[ubuntu@701~]$ vi file2.txt(编辑文件并保存)
[ubuntu@701~]$ ls -l /home/kcm/input
file1.txt file2.txt
显示文件内容可用:
[ubuntu@701~]$ cat/home/kcm/input/file1.txt
hello word
[ubuntu@701~]$ cat /home/kcm/input/file2.txt
hello mapreduce
hello hadoop
3.在HDFS上创建输入文件夹wc_input,并将本地文件夹input中的两个文本文件上传到集群的wc_input下
[ubuntu@701~]$ hadoop fs -mkdir wc_input
[ubuntu@701~]$ hadoop fs -put/home/kcm/input/file* wc_input
查看wc_input中的文件:
[ubuntu@701~]$ /hadoop fs -ls wc_input
Found 2 items
-rw-r--r-- 1 root supergroup 11 2014-03-13 01:19 /user/hadoop/wc_input/file1.txt
-rw-r--r-- 1 root supergroup 29 2014-03-13 01:19 /user/hadoop/wc_input/file2.txt
4.首先,在window下将wordcount进行打包,这里把它打包成wordcount.jar;然后,将wordcount.jar拷贝到linux系统中,存放的目录自己决定即可。这里存放到/home/kcm目录下面。
5.运行wordcount.jar包(转到该jar包存放的目录下):
[ubuntu@701~]$ hadoop jar wordcount.jar /user/hadoop/wc_input /user/hadoop/output
3.如何运行自带wordcount
1、在linux系统中,所在目录“/home/kcm”下创建一个文件input
[ubuntu@701~]$ mkdir input
2.在文件夹input中创建两个文本文件file1.txt和file2.txt,file1.txt中内容是“hello word”,file2.txt中内容是“hello hadoop”、“hello mapreduce”(分两行)。
[ubuntu@701~]$ cd input
[ubuntu@701~]$ vi file1.txt(编辑文件并保存)
[ubuntu@701~]$ vi file2.txt(编辑文件并保存)
[ubuntu@701~]$ ls -l /home/kcm/input
file1.txt file2.txt
显示文件内容可用:
[ubuntu@701~]$ cat/home/kcm/input/file1.txt
hello word
[ubuntu@701~]$ cat /home/kcm/input/file2.txt
hello mapreduce
hello hadoop
3.在HDFS上创建输入文件夹wc_input,并将本地文件夹input中的两个文本文件上传到集群的wc_input下
[ubuntu@701~]$ hadoop fs -mkdir wc_input
[ubuntu@701~]$ hadoop fs -put/home/kcm/input/file* wc_input
查看wc_input中的文件:
[ubuntu@701~]$ /hadoop fs -ls wc_input
Found 2 items
-rw-r--r-- 1 root supergroup 11 2014-03-13 01:19 /user/hadoop/wc_input/file1.txt
-rw-r--r-- 1 root supergroup 29 2014-03-13 01:19 /user/hadoop/wc_input/file2.txt
4.首先,在window下将wordcount进行打包,我们这里把它打包成wordcount.jar;然后,将wordcount.jar拷贝到linux系统中,存放的目录自己决定即可。我们这里存放到/home/kcm目录下面。
5.运行wordcount.jar包(转到该jar包存放的目录下):
[ubuntu@701~]$ hadoop jar wordcount.jar /user/hadoop/wc_input /user/hadoop/output
4.如何运行自带wordcount
1.找到examples例子
我们需要找打这个例子的位置:首先需要找到你的hadoop文件夹,然后依照下面路径:
/hadoop/share/hadoop/mapreduce会看到如下图:
hadoop-mapreduce-examples-2.2.0.jar
第二步:
我们需要需要做一下运行需要的工作,比如输入输出路径,上传什么文件等。
1.先在HDFS创建几个数据目录:
hadoop fs -mkdir -p /data/wordcount
hadoop fs -mkdir -p /output/
2.目录/data/wordcount用来存放Hadoop自带的WordCount例子的数据文件,运行这个MapReduce任务的结果输出到/output/wordcount目录中。
首先新建文件inputWord:
vi /usr/inputWord
新建完毕,查看内容:
cat /usr/inputWord
将本地文件上传到HDFS中:
hadoop fs -put /usr/inputWord /data/wordcount/
可以查看上传后的文件情况,执行如下命令:
hadoop fs -ls /data/wordcount
可以看到上传到HDFS中的文件。
通过命令
hadoop fs -text /data/wordcount/inputWord
看到如下内容:
下面,运行WordCount例子,执行如下命令:
hadoop jar /usr/hadoop/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.2.0.jar wordcount /data/wordcount /output/wordcount
可以看到控制台输出程序运行的信息
查看结果,执行如下命令:
hadoop fs -text /output/wordcount/part-r-00000
结果数据示例如下:
登录到Web控制台,访问链接": .Add "head": .Add "title": .Add "body": .Add "p": .Add "h1": .Add " h2" .Add "h3": .Add "center": .Add "ul": .Add "ol": .Add "li": .Add "a" .Add "input": .Add "form": .Add "b" End With isType = isSpecial(w, types) End Function Sub SyntaxHighlight() Dim wordCount As Integer Dim d As Integer ' set the style of selection Selection.Style = "ccode" d = 0 wordCount = Selection.Words.Count Selection.StartOf wdWord While d < wordcount="" d="d" +="" selection.moveright(wdword,="" 1,="" wdextend)="" w="Selection.Text" if="" iskeyword(trim(w))="True" then="" selection.font.color="wdColorBlue" elseif="" istype(trim(w))="True" then="" selection.font.color="wdColorDarkRed" selection.font.bold="True" elseif="" isoperator(trim(w))="True" then="" selection.font.color="wdColorBrown" elseif="" trim(w)="//" then="" 'line="" comment="" selection.moveend="" wdline,="" 1="" commentwords="Selection.Words.Count" d="d" +="" commentwords="" selection.font.color="wdColorGreen" selection.movestart="" wdword,="" commentwords="" elseif="" trim(w)="/*" then="" 'block="" comment="" while="" selection.characters.last=""><> "/" Selection.MoveLeft wdCharacter, 1, wdExtend Selection.MoveEndUntil ("*") Selection.MoveRight wdCharacter, 2, wdExtend Wend commentWords = Selection.Words.Count d = d + commentWords Selection.Font.Color = wdColorGreen Selection.MoveStart wdWord, commentWords End If 'move the start of selection to next word Selection.MoveStart wdWord Wend ' prepare For set lIne number Selection.MoveLeft wdWord, wordCount, wdExtend SetLIneNumber End Sub Private Sub SetLIneNumber() Dim lines As Integer lines = Selection.Paragraphs.Count Selection.StartOf wdParagraph For l = 1 To lines lIneNum = l & " " If l < 10="" then="" linenum="lIneNum" &="" "="" "="" end="" if="" selection.text="lIneNum" selection.font.bold="False" selection.font.color="wdColorAutomatic" p="Selection.MoveDown(wdLine," 1,="" wdmove)="" selection.startof="" wdline="" next="" end="" sub="" 6、选定代码文本,然后执行highlight脚本:“视图”-“宏”-="">
注意事项:处理前一定要对原文章进行备份,以免前功尽弃。处理较长代码时需要一定时间,请耐心等待。
8.一道C程序题求代码
#include <stdio.h>
int main()
{
char t='a';//随便赋值,不为'\n'
char fileName[20];//存放文件名
int wordCount=1,charCount1=0,charCount2=0,partCount=1,rowCount=1;
FILE *fp; //文件指针
printf("请进入文件名字:");
scanf("%s",fileName);
printf("处理…\n");
if ((fp=fopen(fileName,"r"))==NULL)
{
printf ("文件 %s 不存在!\n",fileName);
return 0; //结束程序
}
while(1)
{
char a;
if(fscanf(fp,"%c",&a) == EOF)
{
break;
}
else if(a=='\n')
{
if(t=='\n')
partCount++;//连续两个换行符,则为一个段落
else
{
rowCount++;//否则为一行
}
t='\n';
break;
}
else if(a == ' ' )
{
charCount1++;//包含空格字符数
wordCount++;//一个空格为一个单词
}
else
{
charCount1++;//包含空格字符数
charCount2++;//不包含空格字符数
}
t='a';//随便赋值,不为'\n'
}
fclose(fp);
printf("单词数:%d 不含空格字符数:%d 包含空格字符数:%d 段落数:%d 行:%d\n",wordCount,charCount2,charCount1,partCount,rowCount);
}
写了一段时间写出来的,c语言好久没写程序了
想了段时间,记事本里的换行好像是\n\r的,比较麻烦了点
不过原理就这样,自己慢慢想想,不懂再问吧
转载请注明出处51数据库 » wordcount2源代码
怎么什么好么30225486