在上面一篇博客中,制作了hadoop 的 Eclipse 插件。现在我们使用Eclipse来重写wordcount统计中文关键词keywords.
1、环境准备。
1、1 准备好有hadoop 插件的Eclipse
1、2 启动好linux虚拟机
1、3 启动好虚拟机后,需要对 hdfs-site.xml 配置文件 添加一个新的属性:dfs.permissions 为 false。 如下:
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
<property>
<name>dfs.permissions</name>
<value>false</value>
</property>
</configuration>
保存并退出
1、4 start-all.sh 启动 hadoop。
1、5 禁用ipatbles 防火墙规则。
2、为linux 添加一个root权限的账户。 该账户名称为你登陆的windwos 的用户名。因为是远程调试,所以需要一个与windows一样的账户名称来运行hadoop。且该账户有足够的权限来运行hadoop。 我直接使用root权限。如下是添加账户示例:(PS: 我 windows 的用户名是 Caixen ,所以以下以 Caixen 为例)
[root@caixen-3 ~]# adduser Caixen //添加一个名为Caixen的用户
[root@caixen-3 ~]#passwd Caixen //修改密码
账户添加成功后。给Caixen附 root 权限。
修改 /etc/passwd 文件,找到如下行,把用户ID修改为 0 ,如下所示:
Caixen:x:0:500::/home/Caixen:/bin/bash
保存并退出。
测试Caixen 账户: logout 登出当前用户, 然后使用 Caixen 用户名和密码登陆即可。
3、在Eclipse 中新建一个 MapReduce Driver 项目,取名为:KeyWordCount
3、1 在hadoop-1.2.1 原解压文件中找到 WordCount.java 文件,并拷贝到 KeyWordCount 项目中。
WordCount.java 文件路径是:hadoop-1.2.1\src\examples\org\apache\hadoop\examples
3、2 新建一个Class: Ejob .java 整个类代码如下:
public class EJob {
// To declare global field
private static List<URL> classPath = new ArrayList<URL>();
// To declare method
public static File createTempJar(String root) throws IOException {
if (!new File(root).exists()) {
return null;
}
Manifest manifest = new Manifest();
manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
final File jarFile = File.createTempFile("EJob-", ".jar", new File(
System.getProperty("java.io.tmpdir")));
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
jarFile.delete();
}
});
JarOutputStream out = new JarOutputStream(
new FileOutputStream(jarFile), manifest);
createTempJarInner(out, new File(root), "");
out.flush();
out.close();
return jarFile;
}
private static void createTempJarInner(JarOutputStream out, File f,
String base) throws IOException {
if (f.isDirectory()) {
File[] fl = f.listFiles();
if (base.length() > 0) {
base = base + "/";
}
for (int i = 0; i < fl.length; i++) {
createTempJarInner(out, fl[i], base + fl[i].getName());
}
} else {
out.putNextEntry(new JarEntry(base));
FileInputStream in = new FileInputStream(f);
b