问题分析
求名字以J开头的员工姓名机器所属部门名称,只需判断员工姓名是否以J开头。首先和问题1类似在Mapper的Setup阶段缓存部门数据,然后在Mapper阶段判断员工姓名是否以J开头,如果是抽取出员工姓名和员工所在部门编号,利用缓存部门数据把部门编号对应为部门名称,转换后输出结果。
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.net.URI; import java.util.HashMap; import java.util.Map; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.filecache.DistributedCache; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; import org.apache.hadoop.util.GenericOptionsParser; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; public class Q7NameDeptOfStartJ extends Configured implements Tool { public static class MapClass extends Mapper<LongWritable, Text, Text, Text> { private Map<String, String> deptMap = new HashMap<String, String>(); private String[] kv; @Override protected void setup(Context context) throws IOException, InterruptedException { BufferedReader in = null; try { URI[] paths = DistributedCache.getCacheFiles(context.getConfiguration()); String deptIdName = null; for (URI path : paths) { if (path.toString().contains("dept")) { in = new BufferedReader(new FileReader(path.toString())); while (null != (deptIdName = in.readLine())) { deptMap.put(deptIdName.split(",")[0], deptIdName.split(",")[1]); } } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } } public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { kv = value.toString().split(","); if (kv[1].toString().trim().startsWith("J")) { context.write(new Text(kv[1].trim()), new Text(deptMap.get(kv[7].trim()))); } } } @Override public int run(String[] args) throws Exception { Job job = new Job(getConf(), "Q7NameDeptOfStartJ"); job.setJobName("Q7NameDeptOfStartJ"); job.setJarByClass(Q7NameDeptOfStartJ.class); job.setMapperClass(MapClass.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); String[] otherArgs = new GenericOptionsParser(job.getConfiguration(), args).getRemainingArgs(); DistributedCache.addCacheFile(new Path(otherArgs[0]).toUri(), job.getConfiguration()); FileInputFormat.addInputPath(job, new Path(otherArgs[1])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[2])); job.waitForCompletion(true); return job.isSuccessful() ? 0 : 1; } public static void main(String[] args) throws Exception { int res = ToolRunner.run(new Configuration(), new Q7NameDeptOfStartJ(), args); System.exit(res); } }
用于计算的基础数据请参考:http://blog.ytso.com/post/17840.html
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/9814.html