java调用mapreduce,从一个简单的java程序调用mapreduce工作

哦,请不要用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");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Java调用MapReduce,需要按照以下步骤进行操作: 1. 配置开发环境:首先,您需要安装Java开发环境(JDK)和Hadoop框架。确保您的系统已正确配置Java环境变量和Hadoop环境变量。 2. 创建MapReduce项目:使用Java的开发工具(如Eclipse、IntelliJ IDEA等)创建一个新的Java项目。 3. 编写Mapper和Reducer类:在项目中创建Mapper和Reducer类,这些类将定义Map和Reduce任务的逻辑。Mapper类负责将输入数据切分为键值对,并进行初步处理;Reducer类负责对Mapper输出的键值对进行聚合和处理。 4. 实现Driver类:创建一个Driver类来配置和执行MapReduce作业。在Driver类中,您需要设置输入和输出路径、配置Mapper和Reducer类、设置作业的其他相关参数。 5. 打包并部署作业:将Java项目打包成可执行的JAR文件,并将其上传到Hadoop集群的某个节点。确保Hadoop集群上已正确配置相关的输入和输出路径。 6. 运行MapReduce作业:使用Hadoop提供的命令行工具或API,提交您的MapReduce作业。命令行工具可以是`hadoop jar`命令,API可以是`JobClient.runJob()`方法。 7. 查看作业结果:在作业执行完成后,您可以通过Hadoop提供的命令行工具或API来查看作业的输出结果。命令行工具可以是`hadoop fs -cat`命令,API可以是从输出路径读取数据。 请注意,以上步骤是一个简单的概述,实际上在实施过程中可能需要更多的配置和处理步骤。此外,您还需要了解MapReduce框架的概念和原理,以更好地理解和调优您的作业。 希望以上介绍对您有所帮助,祝您成功使用Java调用MapReduce
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值