MapReduce读取HBase内容到hdfs

mapper 函数:

static class AnalyzMapper extends TableMapper<Text, Writable>{
		private static IntWritable ONE = new IntWritable(1);
		private JSONParser parser=new JSONParser();
		@Override
		protected void map(ImmutableBytesWritable row, Result columns, Context context)
				throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			
			String value=null;
			for(Cell ce: columns.listCells()){    //这里不使用 kv,kv浪费资源
				CellUtil cu= new CellUtil();
				cu.cloneQualifier(ce);      
//				value=Bytes.toStringBinary(cu.cloneQualifier(ce));     //得到的是列族中所有列的名称
				
				value=Bytes.toStringBinary(cu.cloneQualifier(ce));
				String value2=Bytes.toStringBinary(cu.cloneValue(ce));
				
//				String[] key=value.split(", ");
				try {
//					JSONObject json=(JSONObject) parser.parse(value);
//					String author= (String) json.get(json);
					context.write(new Text(value+value2), ONE);
//					context.write(new Text(key[0]), ONE);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
由上边的代码可知: Result columns 得到的是整个列族下的所有列,我们可以再main函数中的 scan 设置我们需要的列族和列

reducer 函数:
static class AnalyzReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
		@Override
		protected void reduce(Text key, Iterable<IntWritable> value,Context context) throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			int count=0;
			for (IntWritable one : value){
				count++;
			}
			context.write(key, new IntWritable(count));
		}		
	}

main 函数:
public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		
		System.out.print("ad");
		Configuration conf = HBaseConfiguration.create();
		conf.set("conf.column", "data");  
		//配置 conf
		conf.set("hbase.rootdir","hdfs://master:9000/hbase");
	    conf.set("hbase.master", "219.217.203.1:60000"); 
	    //使用eclipse时必须添加这个,否则无法定位  
	    conf.set("hbase.zookeeper.property.clientPort", "2181"); 
        conf.set("hbase.zookeeper.quorum", "219.217.203.24,219.217.203.25,219.217.203.26"); 
        
        Scan scan=new Scan();
        //scan.addColumn(Bytes.toBytes("data"), Bytes.toBytes("qual"));
        
        Job job=Job.getInstance(conf);
        job.setJarByClass(AnalyzFromHBase.class);
        //设置mapper函数,使用提供的库
        TableMapReduceUtil.initTableMapperJob("hbase-book", scan, AnalyzMapper.class, Text.class, IntWritable.class, job);
        
        //设置reduce函数,常规设置
        job.setReducerClass(AnalyzReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        job.setNumReduceTasks(1);
        
        String outpath="hdfs://master:9000/tim/hbase-book/json-test-out";
        FileOutputFormat.setOutputPath(job, new Path(outpath));
        
        System.exit(job.waitForCompletion(true) ? 0 :1);
//		
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值