UserNamePermission.java 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package testjar;
  19. import java.io.IOException;
  20. import java.util.Iterator;
  21. import org.apache.hadoop.conf.Configuration;
  22. import org.apache.hadoop.fs.Path;
  23. import org.apache.hadoop.io.LongWritable;
  24. import org.apache.hadoop.io.Text;
  25. import org.apache.hadoop.mapreduce.Job;
  26. import org.apache.hadoop.mapreduce.Mapper;
  27. import org.apache.hadoop.mapreduce.Reducer;
  28. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  29. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;
  32. public class UserNamePermission
  33. {
  34. private static final Logger LOG =
  35. LoggerFactory.getLogger(UserNamePermission.class);
  36. //This mapper will read the user name and pass in to the reducer
  37. public static class UserNameMapper extends Mapper<LongWritable,Text,Text,Text>
  38. {
  39. Text key1 = new Text("UserName");
  40. public void map(LongWritable key, Text value, Context context)
  41. throws IOException,InterruptedException {
  42. Text val = new Text(System.getProperty("user.name").toString());
  43. context.write(key1, val);
  44. }
  45. }
  46. //The reducer is responsible for writing the user name to the file
  47. //which will be validated by the testcase
  48. public static class UserNameReducer extends Reducer<Text,Text,Text,Text>
  49. {
  50. public void reduce(Text key, Iterator<Text> values,
  51. Context context) throws IOException,InterruptedException {
  52. LOG.info("The key "+key);
  53. if(values.hasNext())
  54. {
  55. Text val = values.next();
  56. LOG.info("The value "+val);
  57. context.write(key,new Text(System.getProperty("user.name")));
  58. }
  59. }
  60. }
  61. public static void main(String [] args) throws Exception
  62. {
  63. Path outDir = new Path("output");
  64. Configuration conf = new Configuration();
  65. Job job = Job.getInstance(conf, "user name check");
  66. job.setJarByClass(UserNamePermission.class);
  67. job.setMapperClass(UserNamePermission.UserNameMapper.class);
  68. job.setCombinerClass(UserNamePermission.UserNameReducer.class);
  69. job.setMapOutputKeyClass(Text.class);
  70. job.setMapOutputValueClass(Text.class);
  71. job.setReducerClass(UserNamePermission.UserNameReducer.class);
  72. job.setNumReduceTasks(1);
  73. job.setInputFormatClass(TextInputFormat.class);
  74. TextInputFormat.addInputPath(job, new Path("input"));
  75. FileOutputFormat.setOutputPath(job, outDir);
  76. System.exit(job.waitForCompletion(true) ? 0 : 1);
  77. }
  78. }