当前位置:   article > 正文

MapReduce中实现对HBase中表的操作一_org.apache.hadoop.hbase.mapreduce.tableoutputforma

org.apache.hadoop.hbase.mapreduce.tableoutputformat - created table instance

1. 上传数据到hdfs中

2. 写Map\Reduce过程

3. 输出结果到hbase中

Tips:

1. 因为map是从hdfs中取数据,因此没有太大变化;而reduce需要输出结果到hbase中,所以这里继承了TableReduce<keyin,valuein,keyout>,这里没有valueout,但是规定TableReduce的valueout必须是Put或者Delete实例。

2. 已经确定了输入输出路径,所以不用在eclipse中配置Run Configuration了。

  1. import java.io.IOException;
  2. import java.util.StringTokenizer;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.fs.Path;
  5. import org.apache.hadoop.hbase.HBaseConfiguration;
  6. import org.apache.hadoop.hbase.HColumnDescriptor;
  7. import org.apache.hadoop.hbase.HTableDescriptor;
  8. import org.apache.hadoop.hbase.client.HBaseAdmin;
  9. import org.apache.hadoop.hbase.client.Put;
  10. import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
  11. import org.apache.hadoop.hbase.mapreduce.TableReducer;
  12. import org.apache.hadoop.hbase.util.Bytes;
  13. import org.apache.hadoop.io.IntWritable;
  14. import org.apache.hadoop.io.LongWritable;
  15. import org.apache.hadoop.io.NullWritable;
  16. import org.apache.hadoop.io.Text;
  17. import org.apache.hadoop.mapreduce.Job;
  18. import org.apache.hadoop.mapreduce.Mapper;
  19. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  20. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  21. public class WordCountHBase {
  22. /**
  23. * @param args
  24. * @throws IOException
  25. * @throws ClassNotFoundException
  26. * @throws InterruptedException
  27. */
  28. public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
  29. // TODO Auto-generated method stub
  30. String tablename = "wordcount";
  31. Configuration conf = new Configuration();
  32. conf.set(TableOutputFormat.OUTPUT_TABLE, tablename);
  33. createHBaseTable(tablename);
  34. Job job = new Job(conf,"WordCount table");
  35. job.setJarByClass(WordCountHBase.class);
  36. job.setNumReduceTasks(3);
  37. job.setMapperClass(Map.class);
  38. job.setReducerClass(Reduce.class);
  39. job.setMapOutputKeyClass(Text.class);
  40. job.setMapOutputValueClass(IntWritable.class);
  41. job.setInputFormatClass(TextInputFormat.class);
  42. job.setOutputFormatClass(TableOutputFormat.class);
  43. // 设置输入目录
  44. FileInputFormat.addInputPath(job, new Path("hdfs://master:9000/input/wordcount/*"));
  45. System.exit(job.waitForCompletion(true)?0:1);
  46. }
  47. public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
  48. private final static IntWritable one = new IntWritable(1);
  49. private Text text = new Text();
  50. @Override
  51. public void map(LongWritable key, Text value, Context context) throws IOException,InterruptedException{
  52. String s = value.toString();
  53. StringTokenizer st = new StringTokenizer(s);
  54. while(st.hasMoreTokens()) {
  55. text.set(st.nextToken());
  56. context.write(text, one);
  57. }
  58. }
  59. }
  60. public static class Reduce extends TableReducer<Text, IntWritable, NullWritable> {
  61. @Override
  62. public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException,InterruptedException{
  63. int sum = 0;
  64. for(IntWritable i:values) {
  65. sum+=i.get();
  66. }
  67. Put put = new Put(Bytes.toBytes(key.toString()));
  68. // row,columnFamily:column,value = word,content:count,sum
  69. put.add(Bytes.toBytes("content"),Bytes.toBytes("count"),Bytes.toBytes(String.valueOf(sum)));
  70. context.write(NullWritable.get(), put);
  71. }
  72. }
  73. /**
  74. * create a table
  75. * @param tablename
  76. * @throws IOException
  77. */
  78. public static void createHBaseTable(String tablename) throws IOException {
  79. HTableDescriptor htd = new HTableDescriptor(tablename);
  80. HColumnDescriptor col = new HColumnDescriptor("content");
  81. htd.addFamily(col);
  82. Configuration cfg = HBaseConfiguration.create();
  83. HBaseAdmin admin = new HBaseAdmin(cfg);
  84. if(admin.tableExists(tablename)) {
  85. System.out.println("table exists,trying recreate table!");
  86. admin.disableTable(tablename);
  87. admin.deleteTable(tablename);
  88. admin.createTable(htd);
  89. }
  90. else {
  91. System.out.println("create new table:"+tablename);
  92. admin.createTable(htd);
  93. }
  94. }
  95. }

结果:


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/855215
推荐阅读
相关标签
  

闽ICP备14008679号