亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從簡單的Java程序調用mapreduce作業

從簡單的Java程序調用mapreduce作業

手掌心 2019-11-27 14:42:41
我一直試圖從同一程序包中的簡單Java程序調用mapreduce作業。.我試圖在java程序中引用mapreduce jar文件,并runJar(String args[])通過傳遞mapreduce作業的輸入和輸出路徑,使用該方法調用它..但是該程序可以正常工作..我如何運行這樣的程序,在該程序中,我只使用傳遞輸入,輸出和jar路徑的主要方法?是否可以通過它運行mapreduce作業(jar)?我想要這樣做是因為我要一個接一個地運行多個mapreduce作業,其中我的Java程序vl通過引用其jar文件來調用每個此類作業。如果可能的話,我不妨只使用一個簡單的servlet進行此類調用并出于圖形目的參考其輸出文件。/* * To change this template, choose Tools | Templates * and open the template in the editor. *//** * * @author root */import org.apache.hadoop.util.RunJar;import java.util.*;public class callOther {    public static void main(String args[])throws Throwable    {        ArrayList arg=new ArrayList();        String output="/root/Desktp/output";        arg.add("/root/NetBeansProjects/wordTool/dist/wordTool.jar");        arg.add("/root/Desktop/input");        arg.add(output);        RunJar.main((String[])arg.toArray(new String[0]));    }}
查看完整描述

3 回答

?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

哦,請不要使用runJar,Java API非常好。


了解如何從常規代碼開始工作:


// create a configuration

Configuration conf = new Configuration();

// create a new job based on the configuration

Job job = new Job(conf);

// here you have to put your mapper class

job.setMapperClass(Mapper.class);

// here you have to put your reducer class

job.setReducerClass(Reducer.class);

// here you have to set the jar which is containing your 

// map/reduce class, so you can use the mapper class

job.setJarByClass(Mapper.class);

// key/value of your reducer output

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(Text.class);

// this is setting the format of your input, can be TextInputFormat

job.setInputFormatClass(SequenceFileInputFormat.class);

// same with output

job.setOutputFormatClass(TextOutputFormat.class);

// here you can set the path of your input

SequenceFileInputFormat.addInputPath(job, new Path("files/toMap/"));

// this deletes possible output paths to prevent job failures

FileSystem fs = FileSystem.get(conf);

Path out = new Path("files/out/processed/");

fs.delete(out, true);

// finally set the empty out path

TextOutputFormat.setOutputPath(job, out);


// this waits until the job completes and prints debug out to STDOUT or whatever

// has been configured in your log4j properties.

job.waitForCompletion(true);

如果您使用的是外部群集,則必須通過以下方式將以下信息放入配置中:


// this should be like defined in your mapred-site.xml

conf.set("mapred.job.tracker", "jobtracker.com:50001"); 

// like defined in hdfs-site.xml

conf.set("fs.default.name", "hdfs://namenode.com:9000");

當hadoop-core.jar位于您的應用程序容器類路徑中時,這應該沒問題。但是我認為您應該在網頁上放置某種進度指示器,因為完成一項Hadoop工作可能需要幾分鐘到幾小時;)


對于YARN(> Hadoop 2)


對于YARN,需要設置以下配置。


// this should be like defined in your yarn-site.xml

conf.set("yarn.resourcemanager.address", "yarn-manager.com:50001"); 


// framework is now "yarn", should be defined like this in mapred-site.xm

conf.set("mapreduce.framework.name", "yarn");


// like defined in hdfs-site.xml

conf.set("fs.default.name", "hdfs://namenode.com:9000");


查看完整回答
反對 回復 2019-11-27
?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

因為映射和減少在不同機器上的運行,所以所有引用的類和jar必須在機器之間移動。


如果您有程序包jar,并且在您的桌面上運行,則@ThomasJungblut的答案是可以的。但是,如果您在Eclipse中運行,請右鍵單擊您的類并運行,它不起作用。


代替:


job.setJarByClass(Mapper.class);

使用:


job.setJar("build/libs/hdfs-javac-1.0.jar");

同時,您的jar清單必須包含Main-Class屬性,這是您的主類。


對于gradle用戶,可以將這些行放在build.gradle中:


jar {

manifest {

    attributes("Main-Class": mainClassName)

}}


查看完整回答
反對 回復 2019-11-27
  • 3 回答
  • 0 關注
  • 879 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號