Window下Eclipse远程访问伪分布式hadoop1.2.1测试WordCount

来源:互联网 发布:js scroll属性 编辑:程序博客网 时间:2024/05/21 19:28

hadoop时遇到遇到ERROR security.UserGroupInformation: PriviledgedActionException as:xxxx cause:java.io.I

目前网上找到的资料

Map/Reduce Master
参看hadoop 安装路径下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://master.hadoop:9000</value> -->        <value>hdfs://192.168.73.100:9000</value></property><property>    <name>hadoop.tmp.dir</name> <value>/root/hadoop/hadoop-1.2.1/tmp</value></property></configuration>

DFS Master 的配置路径在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>master.hadoop:9001</value>  -->        <value>192.168.73.100:9001</value></property><property>        <name>dfs.permissions</name>        <value>false</value></property></configuration>

这里写图片描述

User name 为hadoop 访问用户名: 网上教程大多为hadoop,master, 我是在CentOS7 root用户下直接创建的hadoop 这里我直接用root

hadoop.tmp.dir 所指向的地址路径也在core-site.xml 中 上面有我配置的core-site.xml 可以参看

这里写图片描述

在hadoop-1.2.1\src\examples\org\apache\hadoop\examples 路径下 有官方 WordCount 例子
将例子进行修改

这里写图片描述

要注意

public static void main(String[] args) throws Exception {    Configuration conf = new Configuration();    conf.set("mapred.job.tracker", "192.168.73.100:9001");    conf.set("fs.default.name", "hdfs://192.168.73.100:9000");    // in 为你所指向的 上传地址,我这里是in ,可能你那里是input     String[] ars = new String[]{"in", "newout"};    //注意下面的ars  已经不再是args    String[] otherArgs = new GenericOptionsParser(conf, ars).getRemainingArgs();    if (otherArgs.length != 2) {      System.err.println("Usage: wordcount <in> <out>");      System.exit(2);    }    Job job = new Job(conf, "word count");    File jarFile = EJob.createTempJar("bin");    //EJob 是其他类, 在下面链接中会给出    EJob.addClasspath("/root/hadoop/conf");    ClassLoader classLoader = EJob.getClassLoader();    Thread.currentThread().setContextClassLoader(classLoader);    ((JobConf) job.getConfiguration()).setJar(jarFile.toString());     //job.setJarByClass(WordCount.class);    job.setMapperClass(TokenizerMapper.class);    job.setCombinerClass(IntSumReducer.class);    job.setReducerClass(IntSumReducer.class);    job.setOutputKeyClass(Text.class);    job.setOutputValueClass(IntWritable.class);    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));    System.exit(job.waitForCompletion(true) ? 0 : 1);  }

in 中一定要有测试文件 我的是
这里写图片描述

最后用hadoop 运行 。控制台日志如下图

这里写图片描述

贴下EJob 代码 。当然如果你有积分的话也可以去下载

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.net.URL;import java.net.URLClassLoader;import java.util.ArrayList;import java.util.List;import java.util.jar.JarEntry;import java.util.jar.JarOutputStream;import java.util.jar.Manifest;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);            byte[] buffer = new byte[1024];            int n = in.read(buffer);            while (n != -1) {                out.write(buffer, 0, n);                n = in.read(buffer);            }            in.close();        }    }    public static ClassLoader getClassLoader() {        ClassLoader parent = Thread.currentThread().getContextClassLoader();        if (parent == null) {            parent = EJob.class.getClassLoader();        }        if (parent == null) {            parent = ClassLoader.getSystemClassLoader();        }        return new URLClassLoader(classPath.toArray(new URL[0]), parent);    }    public static void addClasspath(String component) {        if ((component != null) && (component.length() > 0)) {            try {                File f = new File(component);                if (f.exists()) {                    URL key = f.getCanonicalFile().toURL();                    if (!classPath.contains(key)) {                        classPath.add(key);                    }                }            } catch (IOException e) {            }        }    }}

下载链接
http://download.csdn.net/download/sinat_32867867/10029950

原创粉丝点击