JythonAbacus.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #
  2. # Copyright 2006 The Apache Software Foundation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from org.apache.hadoop.fs import Path
  17. from org.apache.hadoop.io import *
  18. from org.apache.hadoop.mapred import *
  19. from org.apache.hadoop.abacus import *
  20. from java.util import *;
  21. import sys
  22. class AbacusMapper(ValueAggregatorMapper):
  23. def map(self, key, value, output, reporter):
  24. ValueAggregatorMapper.map(self, key, value, output, reporter);
  25. class AbacusReducer(ValueAggregatorReducer):
  26. def reduce(self, key, values, output, reporter):
  27. ValueAggregatorReducer.reduce(self, key, values, output, reporter);
  28. class AbacusCombiner(ValueAggregatorCombiner):
  29. def reduce(self, key, values, output, reporter):
  30. ValueAggregatorCombiner.reduce(self, key, values, output, reporter);
  31. def printUsage(code):
  32. print "Abacus <input> <output> <numOfReducers> <inputformat> <specfile>"
  33. sys.exit(code)
  34. def main(args):
  35. if len(args) < 6:
  36. printUsage(1);
  37. inDir = args[1];
  38. outDir = args[2];
  39. numOfReducers = int(args[3]);
  40. theInputFormat = args[4];
  41. specFile = args[5];
  42. print "numOfReducers: ", numOfReducers, "theInputFormat: ", theInputFormat, "specFile: ", specFile
  43. conf = JobConf(AbacusMapper);
  44. conf.setJobName("recordcount");
  45. conf.addDefaultResource(Path(specFile));
  46. if theInputFormat=="textinputformat":
  47. conf.setInputFormat(TextInputFormat);
  48. else:
  49. conf.setInputFormat(SequenceFileInputFormat);
  50. conf.setOutputFormat(TextOutputFormat);
  51. conf.setMapOutputKeyClass(Text);
  52. conf.setMapOutputValueClass(Text);
  53. conf.setOutputKeyClass(Text);
  54. conf.setOutputValueClass(Text);
  55. conf.setNumMapTasks(1);
  56. conf.setNumReduceTasks(numOfReducers);
  57. conf.setMapperClass(AbacusMapper);
  58. conf.setCombinerClass(AbacusCombiner);
  59. conf.setReducerClass(AbacusReducer);
  60. conf.setInputPath(Path(args[1]))
  61. conf.setOutputPath(Path(args[2]))
  62. JobClient.runJob(conf);
  63. if __name__ == "__main__":
  64. main(sys.argv)