您好,登錄后才能下訂單哦!
本篇內容介紹了“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中怎么將已知表移植到另一張表中”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。