HBase(八):HBaseAndMapReduce(二)

本文介绍了一种使用HBase和MapReduce进行数据处理的方法。通过选择HBase中的特定表和列,如blog表中的tags和nickname列,并将这些数据导入到HDFS中进行进一步的处理。该过程涉及到了TableMapper和Reducer的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HBaseAndMapReduce(二):

    首先从HBase中选定某张表,比如blog,然后限定表中的某些列,比如昵称nickname,标签tags,将这些列的数据内容写入到HDFS文件中去。

    本文的HBase数据表blog结构来自于 http://my.oschina.net/gently/blog/678232这篇博文中的测试数据。。。。。。。。。

/**
 * 选定HBase中的某张表,限定列,然后将其内容数据写入到HDFS文件中。
 * */
public class HBaseAndMapReduce2 {

	public static void main(String[] args) throws Exception {
		System.exit(run());
	}

	public static int run() throws Exception {
		Configuration conf = new Configuration();
		conf = HBaseConfiguration.create(conf);
		conf.set("hbase.zookeeper.quorum", "192.168.226.129");

		Job job = Job.getInstance(conf, "findFriend");
		job.setJarByClass(HBaseAndMapReduce2.class);


		Scan scan = new Scan();
		//取对业务有用的数据 tags, nickname
		scan.addColumn(Bytes.toBytes("article"), Bytes.toBytes("tags"));
		scan.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname"));

		//ImmutableBytesWritable来自hbase数据的类型
		/**
		 * public static void initTableMapperJob(String table, Scan scan,
		 * 		Class<? extends TableMapper> mapper,
		 * 		Class<?> outputKeyClass,
		 * 		Class<?> outputValueClass, Job job)
		 * */
		//确保 blog 表存在, 且表结构与本文一样。 
		TableMapReduceUtil.initTableMapperJob("blog", scan, FindFriendMapper.class, 
				Text.class,  Text.class, job);
		DateFormat df = new SimpleDateFormat( "yyyyMMddHHmmssS" );
		
		FileOutputFormat.setOutputPath(job, new Path("hdfs://192.168.226.129:9000/hbasemapreduce1/" +df.format( new Date() )));

		job.setReducerClass(FindFriendReducer.class);
		
		return job.waitForCompletion(true) ? 0 : 1;
	}
    
    // 输入输出的键值
	public static class FindFriendMapper extends TableMapper<Text, Text>{
		//key是hbase中的行键
		//value是hbase中的所行键的所有数据
		@Override
		protected void map(
				ImmutableBytesWritable key,
				Result value,
				Mapper<ImmutableBytesWritable, Result,Text, Text>.Context context)
						throws IOException, InterruptedException {

			Text v = null;
			String[] kStrs = null;
			List<Cell> cs = value.listCells();
			for (Cell cell : cs) {
				System.out.println( "Cell--->: "+cell );
				if ("tags".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))){
					kStrs = Bytes.toString(CellUtil.cloneValue(cell)).split(",");
				}else if ("nickname".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))){
					v = new Text(CellUtil.cloneValue(cell));
				}
			}

			for (String kStr : kStrs) {
				context.write(new Text(kStr.toLowerCase()), v);
			}

		}
	}

	public static class FindFriendReducer extends Reducer<Text, Text, Text, Text>{
		@Override
		protected void reduce(Text key, Iterable<Text> values,
				Reducer<Text, Text, Text, Text>.Context context)
						throws IOException, InterruptedException {
			System.out.println(  "key--->"+key);
			StringBuilder sb = new StringBuilder();
			for (Text text : values) {
				System.out.println(  "value-->"+text);
				sb.append((sb.length() > 0 ? ",":"") + text.toString());
			}
			context.write(key, new Text(sb.toString()));
		}
	}
}

 

输出结构:

hadoop	Berg-OSChina,BergBerg
hbase	OSChina,BergBerg
zookeeper	OSChina,BergBerg

 

转载于:https://my.oschina.net/gently/blog/678244

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值