Hadoop java交叉编译

系统:ubuntu 14.04

HADOOP VERSION: 2.6.0 32bits

在装好hadoop并且开启dfs和yarn以后,用JPS检查能看到一下六个进程:

14779 DataNode
15322 NodeManager
14657 NameNode
15194 ResourceManager
17656 Jps
14979 SecondaryNameNode

接下来我们需要运行WordCout项目来验证是否安装正确。

WordCount.java:

package org.apache.hadoop.mapred;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * This is an example Hadoop Map/Reduce application.
 * It reads the text input files, breaks each line into words
 * and counts them. The output is a locally sorted list of words and the
 * count of how often they occurred.
 *
 * To run: bin/hadoop jar build/hadoop-examples.jar wordcount
 *            [-m <i>maps</i>] [-r <i>reduces</i>] <i>in-dir</i> <i>out-dir</i>
 */
public class WordCount extends Configured implements Tool {

  /**
   * Counts the words in each line.
   * For each line of input, break the line into words and emit them as
   * (<b>word</b>, <b>1</b>).
   */
  public static class MapClass extends MapReduceBase
    implements Mapper<LongWritable, Text, Text, IntWritable> {

    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(LongWritable key, Text value,
                    OutputCollector<Text, IntWritable> output,
                    Reporter reporter) throws IOException {
      String line = value.toString();
      StringTokenizer itr = new StringTokenizer(line);
      while (itr.hasMoreTokens()) {
        word.set(itr.nextToken());
        output.collect(word, one);
      }
    }
  }

  /**
   * A reducer class that just emits the sum of the input values.
   */
  public static class Reduce extends MapReduceBase
    implements Reducer<Text, IntWritable, Text, IntWritable> {

    public void reduce(Text key, Iterator<IntWritable> values,
                       OutputCollector<Text, IntWritable> output,
                       Reporter reporter) throws IOException {
      int sum = 0;
      while (values.hasNext()) {
        sum += values.next().get();
      }
      output.collect(key, new IntWritable(sum));
    }
  }

  static int printUsage() {
    System.out.println("wordcount [-m <maps>] [-r <reduces>] <input> <output>");
    ToolRunner.printGenericCommandUsage(System.out);
    return -1;
  }

  /**
   * The main driver for word count map/reduce program.
   * Invoke this method to submit the map/reduce job.
   * @throws IOException When there is communication problems with the
   *                     job tracker.
   */
  public int run(String[] args) throws Exception {
    JobConf conf = new JobConf(getConf(), WordCount.class);
    conf.setJobName("wordcount");

    // the keys are words (strings)
    conf.setOutputKeyClass(Text.class);
    // the values are counts (ints)
    conf.setOutputValueClass(IntWritable.class);

    conf.setMapperClass(MapClass.class);
    conf.setCombinerClass(Reduce.class);
    conf.setReducerClass(Reduce.class);

    List<String> other_args = new ArrayList<String>();
    for(int i=0; i < args.length; ++i) {
      try {
        if ("-m".equals(args[i])) {
          conf.setNumMapTasks(Integer.parseInt(args[++i]));
        } else if ("-r".equals(args[i])) {
          conf.setNumReduceTasks(Integer.parseInt(args[++i]));
        } else {
          other_args.add(args[i]);
        }
      } catch (NumberFormatException except) {
        System.out.println("ERROR: Integer expected instead of " + args[i]);
        return printUsage();
      } catch (ArrayIndexOutOfBoundsException except) {
        System.out.println("ERROR: Required parameter missing from " +
                           args[i-1]);
        return printUsage();
      }
    }
    // Make sure there are exactly 2 parameters left.
    if (other_args.size() != 2) {
      System.out.println("ERROR: Wrong number of parameters: " +
                         other_args.size() + " instead of 2.");
      return printUsage();
    }
    FileInputFormat.setInputPaths(conf, other_args.get(0));
    FileOutputFormat.setOutputPath(conf, new Path(other_args.get(1)));

    JobClient.runJob(conf);
    return 0;
  }

  public static void main(String[] args) throws Exception {
    int res = ToolRunner.run(new Configuration(), new WordCount(), args);
    System.exit(res);
  }

}

显然直接直接使用javac命令编译因为没有hadoop的jar包是会报很多错的。

查了一些资料,发现因为hadoop版本不同各种jar包的位置略有不同。

在hadoop2.6.0的安装包里面仔细查找可以发现需要的jar包都在hadoop-2.6.0/share/hadoop的各级子目录下面:

root@fd-ubuntu:/usr/hadoop/hadoop-2.6.0/share/hadoop# ls
common  hdfs  httpfs  kms  mapreduce  tools  yarn

于是我们可以首先在/etc/profile最后一行加入一个递归搜索此目录下jar文件的环境变量。

for X in find $HADOOP_DEV_HOME/share/hadoop -type d
do
    HADOOP_CLASSPATH=${HADOOP_CLASSPATH}:${X}
done

然后编写生成WordCount.jar的makefile:

jj = javac

WordCount.jar:org
    jar -cvf WordCount.jar org

org: WordCount.java
    $(jj) -cp $(HADOOP_CLASSPATH) WordCount.java -d .

clear:
    rm -rf org WordCount.jar

按照以上步骤就可以生成WordCount的可执行JAR,再放入HDFS执行即可。

时间: 2024-10-07 06:52:17

Hadoop java交叉编译的相关文章

hadoop java接口及常用api

# java接口及常用api package com.yting.hadoop.hdfs; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URI; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputS

第一次调用 Hadoop Java API

环境:Hadoop1.2.1 例:直接使用FileSystem以标准输出格式显示Hadoop文件系统中的文件 编写JAVA文件 import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; import java.io.InputStream; im

hadoop Java API、 hadoop Streaming 、hadoop Pipes 三者比较学习

1.hadoop  Java  API Hadoop的主要编程语言是Java,因而,Java API是最基本的对外编程接口. 2. hadoop    Streaming             1.概述 它是为方便非java用户编写Mapreduce程序而设计的工具包. Hadoop Streaming是Hadoop提供的一个编程工具,它允许用户使用任何可执行文件或者脚本文件作为Mapper和Reducer, 例如: 采用shell脚本语言中的一些命令作为mapper和reducer(cat作

Hadoop java 程序运行

1, 右击project,选择 maven build,产生jar文件. 2,产生输入文件: hadoop fs -put 输入文件路径 文件夹 example: hadoop fs -put $HADOOP_HOME/Hadoop-WordCount/input/ input hadoop fs -ls input 3, 运行java 文件: hadoop jar jar文件路径 package名称.文件名 input文件 输出文件 example: hadoop jar $HADOOP_HO

Hadoop Java Hdfs API 练习

1. 在本地文件系统生成一个文本文件,,读入文件,将其第101-120字节的内容写入HDFS成为一个新文件2. 在HDFS中生成文本文件,读入这个文件,将其第101-120字节的内容写入本地文件系统成为一个新文件 环境部署:http://www.cnblogs.com/dopeter/p/4630791.html FileBuilder.java 生成文件的工具类,包含在本地生成文件,在Hadoop生成文件,读取Hadoop指定目录的文件 1 package story; 2 3 import

CentOS7下安装配置 Hadoop 2.8.x, JDK安装, 免密码登录, Hadoop Java示例程序运行

01_note_Hadoop的源起与体系介绍:实施Hadoop集群:CDH家族 解压tar包安装JDK以及环境变量配置  tar -xzvf jdkxxx.tar.gz to /usr/app/ (自定义app用来存放安装后的app) java -version 查看目前系统java版本以及环境 rpm -qa | grep java 查看安装包以及依赖 yum -y remove xxxx (删除grep出来的每一个包) 配置环境变量 /etc/profile,配置完之后启用配置source

HADOOP :: java.lang.ClassNotFoundException: WordCount

I am using eclipse to export the jar file of a map-reduce program. When i am run the jar using command hadoop jar hadoop-prog.jar WordCount /home/temp/input /home/temp/output it always shows the error : Exception in thread "main" java.lang.Class

Hadoop java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils

commons-lang3-3.3.2.jar 学习好友推荐案例的时候,提交运行时报错找不到StringUtils java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils [root@node01 ~]# hadoop jar MyFOF.jar com.sxt.hadoop.mr.fof.MyFOF 19/03/30 00:25:37 WARN mapreduce.JobResourceUploader: H

使用NDK实现Android中C与Java交叉编译

android中的java依托于java虚拟机,运行效率是比较低的,最近在做高效大数乘法中,发现了JNI或者NDK可以实现C与Java混编,并且引用c的动态库,而且android中的C语言不需要依托java虚拟机,效率还是非常高的. 一.开发环境的搭建 开发java的话还是eclipse不变,除此之外下载 android-ndk-r9d-linux-x86_64.tar.bz2(linux),安装方法: $ cd android-ndk-r9d/ $ build/tools/make-stand