小试牛刀,将mapreduce的输出结果保存到大型分布式数据库中HBase中,一个例子,求各url的访问pv数据,由于用到rcfile格式需要导入hive-exce包,还需要加载hbase包,如果这两个包都已经被集群管理员放到各节点的Hadoop/lib下那就可以省去这一步,废话不说,干货,看代码:

  1. package test.hbase;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.HashSet;  
  5. import java.util.Set;  
  6.   
  7. import org.apache.hadoop.conf.Configuration;  
  8. import org.apache.hadoop.fs.FileSystem;  
  9. import org.apache.hadoop.fs.Path;  
  10. import org.apache.hadoop.hbase.HBaseConfiguration;  
  11. import org.apache.hadoop.hbase.HColumnDescriptor;  
  12. import org.apache.hadoop.hbase.HTableDescriptor;  
  13. import org.apache.hadoop.hbase.client.HBaseAdmin;  
  14. import org.apache.hadoop.hbase.client.Put;  
  15. import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;  
  16. import org.apache.hadoop.hbase.mapreduce.TableReducer;  
  17. import org.apache.hadoop.hbase.util.Bytes;  
  18. import org.apache.hadoop.hive.serde2.columnar.BytesRefArrayWritable;  
  19. import org.apache.hadoop.io.IntWritable;  
  20. import org.apache.hadoop.io.LongWritable;  
  21. import org.apache.hadoop.io.NullWritable;  
  22. import org.apache.hadoop.io.Text;  
  23. import org.apache.hadoop.mapreduce.Job;  
  24. import org.apache.hadoop.mapreduce.Mapper;  
  25. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  26.   
  27. import com.sohu.tv.dm.common.RCFileInputFormat;  
  28.   
  29. public class URLCountHbase {  
  30.     public static class HBaseMap extends  
  31.             Mapper<LongWritable, BytesRefArrayWritable, Text, IntWritable> {  
  32.   
  33.         private IntWritable i = new IntWritable(1);  
  34.   
  35.         @Override  
  36.         protected void map(LongWritable key, BytesRefArrayWritable value,  
  37.                 Context context) throws IOException, InterruptedException {  
  38.             byte[] url = value.get(4).getBytesCopy();  
  39.             context.write(new Text(url), i);  
  40.         }  
  41.   
  42.     }  
  43.   
  44.     public static class HBaseReduce extends  
  45.             TableReducer<Text, IntWritable, NullWritable> {  
  46.   
  47.         @Override  
  48.         protected void reduce(Text key, Iterable<IntWritable> values,  
  49.                 Context context) throws IOException, InterruptedException {  
  50.             int sum = 0;  
  51.             for (IntWritable i : values) {  
  52.                 sum += i.get();  
  53.             }  
  54.             Put put = new Put(Bytes.toBytes(key.toString()));  
  55.             put.add(Bytes.toBytes("type"), Bytes.toBytes("count"),  
  56.                     Bytes.toBytes(String.valueOf(sum)));  
  57.             context.write(NullWritable.get(), put);  
  58.         }  
  59.   
  60.     }  
  61.   
  62.     public static void createHbaseTable(String tablename) throws IOException {  
  63.         HTableDescriptor htd = new HTableDescriptor(tablename);  
  64.         HColumnDescriptor col = new HColumnDescriptor("type");  
  65.         htd.addFamily(col);  
  66.         HBaseConfiguration config = new HBaseConfiguration();  
  67.         HBaseAdmin admin = new HBaseAdmin(config);  
  68.         if (admin.tableExists(tablename)) {  
  69.             System.out.println("table exists, trying recreate table");  
  70.             admin.disableTable(tablename);  
  71.             admin.deleteTable(tablename);  
  72.         }  
  73.         System.out.println("create new table:" + tablename);  
  74.         admin.createTable(htd);  
  75.   
  76.     }  
  77.   
  78.     public static void main(String args[]) throws Exception {  
  79.         String tablename = "urlcount";  
  80.         Configuration conf = new Configuration();  
  81.         final FileSystem fs = FileSystem.getLocal(conf);  
  82.         final HashSet<String> localfiles = new HashSet<String>();  
  83.         localfiles.add("/opt/hadoop/hive-0.8.1/lib/hive-exec-0.8.1.jar");  
  84.         localfiles.add("/opt/hadoop/hbase/hbase-0.92.1.jar");  
  85.         final HashSet<String> files = new HashSet<String>();  
  86.         for (String s : localfiles) {  
  87.             files.add(URLCountHbase.convertPath(s, fs));  
  88.         }  
  89.         URLCountHbase.cacheJars(conf, files);  
  90.         conf.set(TableOutputFormat.OUTPUT_TABLE, tablename);  
  91.         createHbaseTable(tablename);  
  92.         Job job = new Job(conf, "WordCount table with " + args[0]);  
  93.         job.setJarByClass(URLCountHbase.class);  
  94.         job.setNumReduceTasks(3);  
  95.         job.setReducerClass(HBaseReduce.class);  
  96.         job.setMapperClass(HBaseMap.class);  
  97.         job.setMapOutputKeyClass(Text.class);  
  98.         job.setMapOutputValueClass(IntWritable.class);  
  99.         job.setOutputFormatClass(TableOutputFormat.class);  
  100.         job.setInputFormatClass(RCFileInputFormat.class);  
  101.         FileInputFormat.setInputPaths(job, new Path(args[0]));  
  102.         System.exit(job.waitForCompletion(true) ? 0 : 1);  
  103.   
  104.     }  
  105.       
  106.      private static String convertPath(String path, FileSystem fs) {  
  107.             final Path p = new Path(path);  
  108.             return p.makeQualified(fs).toString();  
  109.      }  
  110.   
  111.      private static void cacheJars(Configuration job, Set<String> localUrls) throws IOException {  
  112.          if (localUrls.isEmpty()) {  
  113.                 return;  
  114.             }  
  115.             final String tmpjars = job.get("tmpjars");  
  116.             final StringBuilder sb = new StringBuilder();  
  117.             if (null != tmpjars) {  
  118.                 sb.append(tmpjars);  
  119.                 sb.append(",");  
  120.             }  
  121.             sb.append(org.apache.hadoop.util.StringUtils.arrayToString(localUrls.toArray(new String[0])));  
  122.             job.set("tmpjars", sb.toString());  
  123.       }  
  124. }