全局变量:
马克-to-win @ 马克java社区:如想传递变量,程序可以在main函数中,利用Congfiguraion类的set函数将一些简单的数据结构放到到Congfiguraion中,map或reduce task任务启动的过程中(比如setup函数)通过Configuration类的get函数读取即可。
一切的代码和上面都一样,只是加了一个全局变量的特性。
package com;
import java.io.File;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class TopNTestMark_to_win {
public static class TokenizerMapper extends Mapper<Object, Text, Text, DoubleWritable> {
String name;
protected void setup(Context context)
throws IOException, InterruptedException {
//从全局配置获取配置参数
Configuration conf = context.getConfiguration();
name = conf.get("name"); //这样就拿到了,但不能设置。想设置用其他方法。这里不深究了
}
/*
o1,p2,250.0
o2,p3,500.0
o2,p4,100.0
o2,p5,700.0
o3,p1,150.0
o1,p1,200.0
*/
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
System.out.println("name is "+name+"key is " + key.toString() + " value is " + value.toString());
String line = value.toString();
String[] fields = line.split(",");
String orderId = fields[0];
更多请见:https://blog.csdn.net/qq_44594249/article/details/97626871
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。