- 浏览: 21422 次
- 性别:
- 来自: 长春
最近访客 更多访客>>
文章分类
最新评论
-
xxjjyy2008:
你的代码我运行报错。。。11/12/08 12:48:39 I ...
Hadoop The Definitive Guide 2nd Edition 读书笔记1 -
dongtianzhe:
恩,谢谢提醒,笔误leibnitz 写道很好,这么快就看2nd ...
Hadoop The Definitive Guide 2nd Edition 读书笔记4 -
leibnitz:
很好,这么快就看2nd了!确认一下,上面的描述中:能力调度算法 ...
Hadoop The Definitive Guide 2nd Edition 读书笔记4
看hadoop也有一段时间了,今天花了一些时间把整个开发环境搭起来了,期间遇到了不小的麻烦,经过查阅大量资料,终于搞定了!
由于我的电脑配置不好,所以在实验室ubuntu服务器上搭建了单机的环境,然后再我的电脑用eclipse上传编写好的程序。
1.安装JDK6
这个不用多说,下一个bin文件,修改一下权限,配置一下环境变量就可以了。
2. 配置SSH
新增hadoop组及同名用户:
$ sudo addgroup hadoop
$ sudo adduser --ingroup hadoop hadoop
接下来做些特别的工作:
$ su
$ chmod u+x /etc/sudoers
$ vim /etc/sudoers
在 root ALL=(ALL)的下一行加上:
hadoop ALL=(ALL)
$ chmod u-x /etc/sudoers
$ exit
安装ssh-server:
$ sudo apt-get install openssh-server
简历SSH KEY:
$ su - hadoop
$ ssh-keygen -t rsa -P ""
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hadoop/.ssh/id_rsa):
Created directory '/home/hadoop/.ssh'.
Your identification has been saved in /home/hadoop/.ssh/id_rsa.
Your public key has been saved in /home/hadoop/.ssh/id_rsa.pub.
The key fingerprint is:
9d:47:ab:d7:22:54:f0:f9:b9:3b:64:93:12:75:81:27 hadoop@ubuntu
让其不输入密码就能登录:
hadoop@ubuntu:~$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
$ sudo /etc/init.d/ssh reload
使用:
$ ssh localhost
看看是不是直接ok了。
3.安装Hadoop0.20.2
将包中内容解压到/usr/local/hadoop,并改变其所有者:
$ sudo chown -R hadoop:hadoop hadoop
配置Hadoop:
$ cd /usr/local/hadoop
$ vim conf/core-site.xml
将内容改为:
其中fs.default.name指定namenode的ip和端口;dfs.replication指定文件块在hdfs中的备份数目,默认为3;hadoop.tmp.dir指定临时目录。
$ vim conf/mapred-site.xml
内容改为:
格式化namenode:
hadoop@guohe-desktop:/usr/local/hadoop$ ./bin/hadoop namenode -format
启动hadoop
hadoop@guohe-desktop:/usr/local/hadoop$ ./bin/start-all.sh
注意:0.20.2启动后默认会在safemode,所以要退出安全模式。具体原因未知,我装0.21的时候没有这种情况。
hadoop@guohe-desktop:/usr/local/hadoop$ bin/hadoop dfsadmin -safemode leave
运行jps看看是否启动了5个进程。
可以跑一个wordcount程序来验证一下hadoop是否正确启动,具体方法网上有很多,这里不再叙述。
好了,hadoop已经在服务器上运行起来了。为了在我的电脑上能用eclipse向服务器发布hadoop程序,要配置一下eclipse。
hadoop包中自带mapreduce插件可以使我们很容易的使用eclipse调试运行程序,但是这个插件对eclipse的版本要求很高。0.21中自带的插件与我电脑中的任何一个eclipse版本都不兼容,所以这里才使用0.20.2。0.20中的插件可以与eclipse-java-europa-winter这个版本很好的兼容。
首先要下载安装eclipse-java-europa-winter,然后将根目录里,contrib\eclipse-plugin 文件夹下有,Hadoop 在Eclipse 的插件hadoop-0.20.2-eclipse-plugin.jar 。将其拷贝到Eclipse 的plugins 目录下。
启动eclipse,我们可以看到以下界面:
设置 Hadoop 主目录
点击 Eclipse 主菜单上 Windows->Preferences, 然后在左侧选择 Hadoop Home Directory, 设定你的 Hadoop 解压后的目录,这是为了编写程序的时候将库自动加载进来, 如图所示:
在 Eclipse 设置 Hadoop Location
打开 Map/Reduce location中右键,选择New Hadoop Location
在General中设置:
先点击finish,然后再出现的hadoopserver点右键选择edit hadoop location,这样做的原因是选edit后会新出现很多选项:
主要将hadoop.tmp.dir设置成/home/hadoop/tmp
将一些0.0.0.0都换成服务器的ip59.72.109.206
将hadoop.job.ugi第一个逗号前改为hadoop
这样在左侧 Project Explorer 内, DFS Locations 内可以查看 HDFS 文件系统内的文件,可以进行新增,删除等操作。
然后我们新建一个测试工程test,并编写一个测试得类,代码如下:
运行前设置命令行参数:
注意:这两个路径指的是服务器上hdfs上的路径,如test-in就代表hdfs根目录下的test-in
运行程序就可以得到结果啦!
至此,一个简单的开发运行环境搭建完毕了,还是蛮有成就感的,呵呵,明天就开始学习hadoop下的程序设计了。
由于我的电脑配置不好,所以在实验室ubuntu服务器上搭建了单机的环境,然后再我的电脑用eclipse上传编写好的程序。
1.安装JDK6
这个不用多说,下一个bin文件,修改一下权限,配置一下环境变量就可以了。
2. 配置SSH
新增hadoop组及同名用户:
$ sudo addgroup hadoop
$ sudo adduser --ingroup hadoop hadoop
接下来做些特别的工作:
$ su
$ chmod u+x /etc/sudoers
$ vim /etc/sudoers
在 root ALL=(ALL)的下一行加上:
hadoop ALL=(ALL)
$ chmod u-x /etc/sudoers
$ exit
安装ssh-server:
$ sudo apt-get install openssh-server
简历SSH KEY:
$ su - hadoop
$ ssh-keygen -t rsa -P ""
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hadoop/.ssh/id_rsa):
Created directory '/home/hadoop/.ssh'.
Your identification has been saved in /home/hadoop/.ssh/id_rsa.
Your public key has been saved in /home/hadoop/.ssh/id_rsa.pub.
The key fingerprint is:
9d:47:ab:d7:22:54:f0:f9:b9:3b:64:93:12:75:81:27 hadoop@ubuntu
让其不输入密码就能登录:
hadoop@ubuntu:~$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
$ sudo /etc/init.d/ssh reload
使用:
$ ssh localhost
看看是不是直接ok了。
3.安装Hadoop0.20.2
将包中内容解压到/usr/local/hadoop,并改变其所有者:
$ sudo chown -R hadoop:hadoop hadoop
配置Hadoop:
$ cd /usr/local/hadoop
$ vim conf/core-site.xml
将内容改为:
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <!-- Put site-specific property overrides in this file. --> <configuration> <property> <name>fs.default.name</name> <value>hdfs://59.72.109.206:9000</value> </property> <property> <name>dfs.replication</name> <value>1</value> </property> <property> <name>hadoop.tmp.dir</name> <value>/home/hadoop/tmp</value> </property> </configuration>
其中fs.default.name指定namenode的ip和端口;dfs.replication指定文件块在hdfs中的备份数目,默认为3;hadoop.tmp.dir指定临时目录。
$ vim conf/mapred-site.xml
内容改为:
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <!-- Put site-specific property overrides in this file. --> <configuration> <property> <name>mapred.job.tracker</name> <value>59.72.109.206:9001</value> </property> </configuration>
格式化namenode:
hadoop@guohe-desktop:/usr/local/hadoop$ ./bin/hadoop namenode -format
启动hadoop
hadoop@guohe-desktop:/usr/local/hadoop$ ./bin/start-all.sh
注意:0.20.2启动后默认会在safemode,所以要退出安全模式。具体原因未知,我装0.21的时候没有这种情况。
hadoop@guohe-desktop:/usr/local/hadoop$ bin/hadoop dfsadmin -safemode leave
运行jps看看是否启动了5个进程。
可以跑一个wordcount程序来验证一下hadoop是否正确启动,具体方法网上有很多,这里不再叙述。
好了,hadoop已经在服务器上运行起来了。为了在我的电脑上能用eclipse向服务器发布hadoop程序,要配置一下eclipse。
hadoop包中自带mapreduce插件可以使我们很容易的使用eclipse调试运行程序,但是这个插件对eclipse的版本要求很高。0.21中自带的插件与我电脑中的任何一个eclipse版本都不兼容,所以这里才使用0.20.2。0.20中的插件可以与eclipse-java-europa-winter这个版本很好的兼容。
首先要下载安装eclipse-java-europa-winter,然后将根目录里,contrib\eclipse-plugin 文件夹下有,Hadoop 在Eclipse 的插件hadoop-0.20.2-eclipse-plugin.jar 。将其拷贝到Eclipse 的plugins 目录下。
启动eclipse,我们可以看到以下界面:
设置 Hadoop 主目录
点击 Eclipse 主菜单上 Windows->Preferences, 然后在左侧选择 Hadoop Home Directory, 设定你的 Hadoop 解压后的目录,这是为了编写程序的时候将库自动加载进来, 如图所示:
在 Eclipse 设置 Hadoop Location
打开 Map/Reduce location中右键,选择New Hadoop Location
在General中设置:
先点击finish,然后再出现的hadoopserver点右键选择edit hadoop location,这样做的原因是选edit后会新出现很多选项:
主要将hadoop.tmp.dir设置成/home/hadoop/tmp
将一些0.0.0.0都换成服务器的ip59.72.109.206
将hadoop.job.ugi第一个逗号前改为hadoop
这样在左侧 Project Explorer 内, DFS Locations 内可以查看 HDFS 文件系统内的文件,可以进行新增,删除等操作。
然后我们新建一个测试工程test,并编写一个测试得类,代码如下:
package examples; import java.io.IOException; import java.util.Random; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.io.WritableComparator; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.Reducer.Context; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser; public class AdvancedWordCount { public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); private String pattern = "[^\\w]"; @Override protected void map(Object key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); System.out.println("-------line todo: " + line); line = line.replaceAll(pattern, " "); System.out.println("-------line done: " + line); StringTokenizer itr = new StringTokenizer(line.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } } } public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable result = new IntWritable(); @Override protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { // TODO Auto-generated method stub int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } public static class MyInverseMapper extends Mapper<Object, Text, IntWritable, Text> { @Override protected void map(Object key, Text value, Context context) throws IOException, InterruptedException { String[] keyAndValue = value.toString().split("\t"); System.out.println("---------------->" + value); System.out.println("--------0------->" + keyAndValue[0]); System.out.println("--------1------->" + keyAndValue[1]); context.write(new IntWritable(Integer.parseInt(keyAndValue[1])), new Text(keyAndValue[0])); } } public static class IntWritableDecreasingComparator extends IntWritable.Comparator { @Override public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { // TODO Auto-generated method stub return -super.compare(b1, s1, l1, b2, s2, l2); } public int compare(WritableComparator a, WritableComparator b) { // TODO Auto-generated method stub return -super.compare(a, b); } } public static boolean countingJob(Configuration conf, Path in, Path out) throws IOException, InterruptedException, ClassNotFoundException { Job job = new Job(conf, "wordcount"); job.setJarByClass(AdvancedWordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, in); FileOutputFormat.setOutputPath(job, out); return job.waitForCompletion(true); } public static boolean sortingJob(Configuration conf, Path in, Path out) throws IOException, InterruptedException, ClassNotFoundException { Job job = new Job(conf, "sort"); job.setJarByClass(AdvancedWordCount.class); job.setMapperClass(MyInverseMapper.class); job.setOutputKeyClass(IntWritable.class); job.setOutputValueClass(Text.class); job.setSortComparatorClass(IntWritableDecreasingComparator.class); FileInputFormat.addInputPath(job, in); FileOutputFormat.setOutputPath(job, out); return job.waitForCompletion(true); } public static void main(String[] args) { Configuration conf = new Configuration(); String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); Path temp = new Path("wordcount-temp-" + Integer.toString(new Random().nextInt(Integer.MAX_VALUE))); boolean a = false, b = false; Path in = new Path(otherArgs[0]); Path out = new Path(otherArgs[1]); if(otherArgs.length != 2) System.exit(2); try { a = AdvancedWordCount.countingJob(conf, in, temp); b = AdvancedWordCount.sortingJob(conf, temp, out); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { FileSystem.get(conf).delete(temp, true); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (!a || !b) try { FileSystem.get(conf).delete(out, true); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
运行前设置命令行参数:
注意:这两个路径指的是服务器上hdfs上的路径,如test-in就代表hdfs根目录下的test-in
运行程序就可以得到结果啦!
至此,一个简单的开发运行环境搭建完毕了,还是蛮有成就感的,呵呵,明天就开始学习hadoop下的程序设计了。
发表评论
-
Hadoop The Definitive Guide 2nd Edition 读书笔记5
2010-12-17 14:30 2298之前我们学习了MapReduce ... -
Hadoop The Definitive Guide 2nd Edition 读书笔记4
2010-12-15 14:05 2316MapReduce工作流程: job ... -
Hadoop The Definitive Guide 2nd Edition 读书笔记3
2010-12-04 20:11 1404第四章是介绍Hadoop的IO ... -
Hadoop The Definitive Guide 2nd Edition 读书笔记2
2010-12-03 21:01 1405第三章介绍的是Hadoop的分布式文件系统HDFS相关的内容。 ... -
Hadoop The Definitive Guide 2nd Edition 读书笔记1
2010-12-02 13:34 1821Hadoop The Definitive Guide 2nd ... -
MapReduce执行过程
2010-11-19 13:21 1406在研究hadoop之前,有必要将hadoop两个核心技术HDF ...
相关推荐
ubuntu 9.04 postfix+postfixadmin+courier+mysql+squirrermail+mysql-sasl验证
在ubuntu 9.04上安装VMWareTools步骤 上海嵌入式家园-开发板商城 联系人:贺工(嵌入式高级系统工程师) 电话: 13917077026 QQ: 844698468 MSN: embedclub@hotmail.com Email:embedclub@vip.qq.com 网址:...
pl/sql developer 9.04+注册机 2012年1月为止最新版本,附注册机,已测试可用。
对于学习ubuntu 9.04的童鞋很有用滴
Ubuntu 9.04 ISPconfig设置
SimpleScalar是一款经典的CPU模拟器,本文介绍了如何在Ubuntu9.04下安装SimpleScalar。安装中会出现许多问题,文中给出了解答。
ubuntu9.04下搭建Qt4.5开发环境.pdf
Ubuntu-9.04-1gb.7z,是colinux下的Ubuntu 9.04的镜像文件~
ubuntu9.04下搭建Qt4.5开发环境[定义].pdf
新手windows下硬盘安装Ubuntu 9.04 新手windows下硬盘安装Ubuntu 9.04 新手windows下硬盘安装Ubuntu 9.04 新手windows下硬盘安装Ubuntu 9.04 新手windows下硬盘安装Ubuntu 9.04新手windows下硬盘安装Ubuntu 9.04 ...
汉化系统界面及菜单显示 ...dpkg -i language-pack-gnome-zh_1%3a9.04+20090413_all.deb language-pack-gnome-zh-base_1%3a9.04+20090413_all.deb ubuntu9.10界面汉化请访问http://sdsxlj.download.csdn.net/下载
OpenERP Installing Ubuntu9.04
NULL 博文链接:https://yuanqixun.iteye.com/blog/427715
这个资源是为了给大家提供更快的更新源而上传。其中包含了中科大的貌似最快的源。
此教程收录了各个论坛的精华帖子,绝对给你打造一个完美的Ubuntu
ubuntu9.04下的LAMP安装 ubuntu9.04下的LAMP安装 验证OK
NULL 博文链接:https://beyondjhf-2008.iteye.com/blog/403046
ubuntu 9.04 安装配置优化全记录
NULL 博文链接:https://zywang.iteye.com/blog/407055
对Ubuntu 9.04进行极其详细的介绍...