温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

HBase中怎么将已知表移植到另一张表中

发布时间:2021-12-09 10:26:10 来源:亿速云 阅读:153 作者:iii 栏目:云计算

本篇内容介绍了“HBase中怎么将已知表移植到另一张表中”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

    将已经存在某张表,比如 blog,将此表中的数据“移植”到另外一张新表中。

/**  * 将HBase中一张表的数据移植到另一张表中。  * */ public class HBaseAndMapReduce4 {	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(HBaseAndMapReduce4.class);	//实例化scan对象。	Scan scan = new Scan();	scan.addColumn(Bytes.toBytes("article"), Bytes.toBytes("tags"));	scan.addColumn(Bytes.toBytes("author"), Bytes.toBytes("nickname"));	TableMapReduceUtil.initTableMapperJob("blog", scan, FindFriendMapper.class, 	ImmutableBytesWritable.class, ImmutableBytesWritable.class,job);	TableMapReduceUtil.initTableReducerJob("friend02", FindFriendReducer.class, job);	checkTable(conf);	return job.waitForCompletion(true) ? 0 : 1;	}	private static void checkTable(Configuration conf) throws Exception {	Connection con = ConnectionFactory.createConnection(conf);	Admin admin = con.getAdmin();	TableName tn = TableName.valueOf("friend02");	if (!admin.tableExists(tn)){	HTableDescriptor htd = new HTableDescriptor(tn);	HColumnDescriptor hcd = new HColumnDescriptor("person");	htd.addFamily(hcd);	admin.createTable(htd);	System.out.println("表不存在,新创建表成功....");	}	}	public static class FindFriendMapper extends TableMapper<ImmutableBytesWritable, ImmutableBytesWritable>{	@Override	//key是hbase中的行键	//value是hbase中的所行键的所有数据	protected void map(	ImmutableBytesWritable key,	Result value,	Mapper<ImmutableBytesWritable, Result,ImmutableBytesWritable, ImmutableBytesWritable>.Context context)	throws IOException, InterruptedException {	ImmutableBytesWritable v = null;	String[] kStrs = null;	List<Cell> cs = value.listCells();	for (Cell cell : cs) {	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 ImmutableBytesWritable(CellUtil.cloneValue(cell));	}	}	for (String kStr : kStrs) {	context.write(new ImmutableBytesWritable(Bytes.toBytes(kStr.toLowerCase())), v);	}	}	}	public static class FindFriendReducer extends	TableReducer<ImmutableBytesWritable, ImmutableBytesWritable, ImmutableBytesWritable> {	@Override	protected void reduce(	ImmutableBytesWritable key,	Iterable<ImmutableBytesWritable> values,	Reducer<ImmutableBytesWritable, ImmutableBytesWritable, ImmutableBytesWritable, Mutation>.Context context)	throws IOException, InterruptedException {	System.out.println(  "key--->"+key.get() );	Put put = new Put(key.get());	StringBuilder vStr = new StringBuilder();	for (ImmutableBytesWritable value : values) {	System.out.println( "value-->"+value.get() );	vStr.append((vStr.length() > 0 ? ",":"") + Bytes.toString(value.get()));	}	put.addColumn(Bytes.toBytes("person"), Bytes.toBytes("nickname"),Bytes.toBytes(vStr.toString()));	context.write(key, put);	}	} }

“HBase中怎么将已知表移植到另一张表中”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI