温馨提示  2024年 6月我们已经停止开发者板块文章内容更新,谢谢来访。存档数据
知识 2023-08-25 39 次阅读

hbase中怎么把数据写到hdfs文件中

   

本文主要讲解了“hbase中怎么把数据写到hdfs文件中”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“hbase中怎么把数据写到hdfs文件中”吧!

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

/** * 选定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, nicknamescan.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中的所行键的所有数据@overrideprotected 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>{@overrideprotected 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()));}}}

输出结构

hadoopberg-oschina,bergberghbaseoschina,bergbergzookeeperoschina,bergberg

感谢各位的阅读,以上就是“hbase中怎么把数据写到hdfs文件中”的内容了,经过本文的学习后,相信大家对hbase中怎么把数据写到hdfs文件中这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是云,小编将为大家推送更多相关知识点的文章,欢迎关注!