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