如何运行自带wordcount
1、在linux系统中,所在目录“/home/kcm”下创建一个文件input[ubuntu@701~]$ mkdir input2.在文件夹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/inputfile1.txt file2.txt显示文件内容可用:[ubuntu@701~]$ cat/home/kcm/input/file1.txthello word[ubuntu@701~]$ cat /home/kcm/input/file2.txthello mapreducehello hadoop3.在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_inputFound 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.txt4.首先,在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
hadoop运行wordcount实例执行到$ bin/hadoop jar hadoop
展开全部 1.找到examples例子我们需要找打这个例子的位置:首先需要找到你的hadoop文件夹,然后依照下面路径:/hadoop/share/hadoop/mapreduce第二步:我们需要需要做一下运行需要的工作,比如输入输出路径,上传什么文件等。
1.先在HDFS创建几个数据目录:1.hadoop fs -mkdir -p /data/wordcount2.hadoop fs -mkdir -p /output/2.目录/data/wordcount用来存放Hadoop自带的WordCount例子的数据文件,运行这个MapReduce任务的结果输出到/output/wordcount目录中。
首先新建文件inputWord:1.vi /usr/inputWord新建完毕,查看内容:将本地文件上传到HDFS中:可以查看上传后的文件情况,执行如下命令:1.hadoop fs -ls /data/wordcount可以看到上传到HDFS中的文件。
登录到Web控制台,访问链接可以看到任务记录情况。
...
Hadoop中Wordcount例子中,为什么reduce方法和combiner方法调用...
1. 创建本地的示例数据文件:依次进入【Home】-【hadoop】-【hadoop-1.2.1】创建一个文件夹file用来存储本地原始数据。
并在这个目录下创建2个文件分别命名为【myTest1.txt】和【myTest2.txt】或者你想要的任何文件名。
分别在这2个文件中输入下列示例语句:2. 在HDFS上创建输入文件夹呼出终端,输入下面指令:bin/hadoop fs -mkdir hdfsInput执行这个命令时可能会提示类似安全的问题,如果提示了,请使用bin/hadoop dfsadmin -safemode leave来退出安全模式。
当分布式文件系统处于安全模式的情况下,文件系统中的内容不允许修改也不允许删除,直到安全模式结 束。
安全模式主要是为了系统启动的时候检查各个DataNode上数据块的有效性,同时根据策略必要的复制或者删除部分数据块。
运行期通过命令也可以进入 安全模式。
意思是在HDFS远程创建一个输入目录,我们以后的文件需要上载到这个目录里面才能执行。
3. 上传本地file中文件到集群的hdfsInput目录下在终端依次输入下面指令:cd hadoop-1.2.1bin/hadoop fs -put file/myTest*.txt hdfsInput4. 运行例子:在终端输入下面指令:bin/hadoop jar hadoop-examples-1.2.1.jar wordcount hdfsInput hdfsOutput注意,这里的示例程序是1.2.1版本的,可能每个机器有所不一致,那么请用*通配符代替版本号bin/hadoop jar hadoop-examples-*.jar wordcount hdfsInput hdfsOutput应该出现下面结果:Hadoop命令会启动一个JVM来运行这个MapReduce程序,并自动获得Hadoop的配置,同时把类的路径(及其依赖关系)加入到Hadoop的库中。
以上就是Hadoop Job的运行记录,从这里可以看到,这个Job被赋予了一个ID号:job_201202292213_0002,而且得知输入文件有两个(Total input paths to process : 2),同时还可以了解map的输入输出记录(record数及字节数),以及reduce输入输出记录。
查看HDFS上hdfsOutput目录内容:在终端输入下面指令:bin/hadoop fs -ls hdfsOutput从上图中知道生成了三个文件,我们的结果在"part-r-00000"中。
使用下面指令查看结果输出文件内容bin/hadoop fs -cat output/part-r-00000
请教Spark Streaming的例子NetworkWordCount在yarn上面运行时的问...
在这个实例中,我将会向大家介绍如何使用Python 为 Hadoop编写一个简单的MapReduce程序。
尽管Hadoop 框架是使用Java编写的但是我们仍然需要使用像C++、Python等语言来实现Hadoop程序。
尽管Hadoop官方网站给的示例程序是使用Jython编写并打包成Jar文件,这样显然造成了不便,其实,不一定非要这样来实现,我们可以使用Python与Hadoop 关联进行编程,看看位于/src/examples/python/WordCount.py 的例子,你将了解到我在说什么。
我们想要做什么?我们将编写一个简单的 MapReduce 程序,使用的是C-Python,而不是Jython编写后打包成jar包的程序。
我们的这个例子将模仿 WordCount 并使用Python来实现,例子通过读取文本文件来统计出单词的出现次数。
结果也以文本形式输出,每一行包含一个单词和单词出现的次数,两者中间使用制表符来想间隔。
先决条件编写这个程序之前,你学要架设好Hadoop 集群,这样才能不会在后期工作抓瞎。
如果你没有架设好,那么在后面有个简明教程来教你在Ubuntu Linux 上搭建(同样适用于其他发行版linux、unix)如何使用Hadoop Distributed File System (HDFS)在Ubuntu Linux 建立单节点的 Hadoop 集群如何使用Hadoop Distributed File System (HDFS)在Ubuntu Linux 建立多节点的 Hadoop 集群Python的MapReduce代码使用Python编写MapReduce代码的技巧就在于我们使用了 HadoopStreaming 来帮助我们在Map 和 Reduce间传递数据通过STDIN (标准输入)和STDOUT (标准输出).我们仅仅使用Python的sys.stdin来输入数据,使用sys.stdout输出数据,这样做是因为HadoopStreaming会帮我们办好其他事。
这是真的,别不相信!Map: mapper.py将下列的代码保存在/home/hadoop/mapper.py中,他将从STDIN读取数据并将单词成行分隔开,生成一个列表映射单词与发生次数的关系:注意:要确保这个脚本有足够权限(chmod +x /home/hadoop/mapper.py)。
#!/usr/bin/env pythonimport sys# input comes from STDIN (standard input)for line in sys.stdin: # remove leading and trailing whitespace line = line.strip() # split the line into words words = line.split() # increase counters for word in words: # write the results to STDOUT (standard output); # what we output here will be the input for the # Reduce step, i.e. the input for reducer.py # # tab-delimited; the trivial word count is 1 print '%s\\t%s' % (word, 1)在这个脚本中,并不计算出单词出现的总数,它将输出 " 1" 迅速地,尽管可能会在输入中出现多次,计算是留给后来的Reduce步骤(或叫做程序)来实现。
当然你可以改变下编码风格,完全尊重你的习惯。
Reduce: reducer.py将代码存储在/home/hadoop/reducer.py 中,这个脚本的作用是从mapper.py 的STDIN中读取结果,然后计算每个单词出现次数的总和,并输出结果到STDOUT。
同样,要注意脚本权限:chmod +x /home/hadoop/reducer.py#!/usr/bin/env pythonfrom operator import itemgetterimport sys# maps words to their countsword2count = {}# input comes from STDINfor line in sys.stdin: # remove leading and trailing whitespace line = line.strip() # parse the input we got from mapper.py word, count = line.split('\\t', 1) # convert count (currently a string) to int try: count = int(count) word2count[word] = word2count.get(word, 0) + count except ValueError: # count was not a number, so silently # ignore/discard this line pass# sort the words lexigraphically;## this step is NOT required, we just do it so that our# final output will look more like the official Hadoop# word count examplessorted_word2count = sorted(word2count.items(), key=itemgetter(0))# write the results to STDOUT (standard output)for word, count in sorted_word2count: print '%s\\t%s'% (word, count)测试你的代码(cat data | map | sort | reduce)我建议你在运行MapReduce job测试前尝试手工测试你的mapper.py 和 reducer.py脚本,以免得不到任何返回结果这里有一些建议,关于如何测试你的Map和Reduce的功能:——————————————————————————————————————————————\r\n # very basic test hadoop@ubuntu:~$ echo "foo foo quux labs foo bar quux" | /home/hadoop/mapper.py foo 1 foo 1 quux 1 labs 1 foo 1 bar 1—————————————————————————————————————————————— hadoop@ubuntu:~$ echo "foo foo quux labs foo bar quux" | /home/hadoop/mapper.py | sort | /home/hadoop/reducer.py bar 1 foo 3 labs 1—————————————————————————————————————————————— # using one of the ebooks as example input # (see below on where to get the ebooks) hadoop@ubuntu:~$ cat /tmp/gutenberg/20417-8.txt | /home/hadoop/mapper.py The 1 Project 1 Gutenberg 1 EBook 1 of 1 [...] (you get the idea) quux 2 quux 1 ————————————————————————————...
转载请注明出处51数据库 » wordcount实例