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

本篇内容介绍了“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 extendsTableReducer<ImmutableBytesWritable, ImmutableBytesWritable, ImmutableBytesWritable> {@Overrideprotected 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中怎么将已知表移植到另一张表中”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注西部数码网站,小编将为大家输出更多高质量的实用文章!

更多关于云服务器域名注册虚拟主机的问题,请访问西部数码官网:www.west.cn

赞(0)
声明:本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-62778877-8306;邮箱:fanjiao@west.cn。本站原创内容未经允许不得转载,或转载时需注明出处:西部数码知识库 » HBase中怎么将已知表移植到另一张表中

登录

找回密码

注册