/** * Processes a List of Rows (Put, Delete) and writes them to an HTable instance in RegionServer * buckets via the htable.batch method. <br> * <br> * The benefit of submitting Puts in this manner is to minimize the number of RegionServer RPCs, * thus this will produce one RPC of Puts per RegionServer. <br> * <br> * Assumption #1: Regions have been pre-created for the table. If they haven't, then all of the * Puts will go to the same region, defeating the purpose of this utility method. See the Apache * HBase book for an explanation of how to do this. <br> * Assumption #2: Row-keys are not monotonically increasing. See the Apache HBase book for an * explanation of this problem. <br> * Assumption #3: That the input list of Rows is big enough to be useful (in the thousands or * more). The intent of this method is to process larger chunks of data. <br> * <br> * This method accepts a list of Row objects because the underlying .batch method accepts a list * of Row objects. <br> * <br> * * @param htable HTable instance for target HBase table * @param rows List of Row instances * @throws IOException if a remote or network exception occurs */ public static void bucketRsBatch(HTable htable, List<Row> rows) throws IOException { try { Map<String, List<Row>> rowMap = createRsRowMap(htable, rows); for (List<Row> rsRows : rowMap.values()) { htable.batch(rsRows); } } catch (InterruptedException e) { throw new IOException(e); } }
@Override public void tweet(User user, String tweetText) throws IOException { final long epoch = System.currentTimeMillis(); final long tranposeEpoch = Long.MAX_VALUE - epoch; final byte[] epochBytes = Bytes.toBytes(epoch); final byte[] tweetBytes = Bytes.toBytes(tweetText); byte[] nameBytes = Bytes.toBytes(user.getName()); /** put tweet into tweets */ Put tweetRowPut = new Put(generateTweetId(user)); tweetRowPut.add(_DEFAULT, _NAME, nameBytes); tweetRowPut.add(_DEFAULT, _MAIL, Bytes.toBytes(user.getEmail())); tweetRowPut.add(_DEFAULT, _TWEET, tweetBytes); tweetRowPut.add(_DEFAULT, _TIME, epochBytes); tweetsTable.put(tweetRowPut); /** put tweets for followers */ Scan followerScan = new Scan(); followerScan.setStartRow(Bytes.toBytes(user.getUserId() + "-")); followerScan.setStopRow(Bytes.toBytes((user.getUserId() + 1) + "-")); ResultScanner followerRS = followersTable.getScanner(followerScan); /** put users on tweet to her own tweetline */ Put put = new Put(Bytes.toBytes(user.getUserId() + "-" + tranposeEpoch + "-" + user.getUserId())); put.add(_DEFAULT, _NAME, nameBytes); put.add(_DEFAULT, _TWEET, tweetBytes); put.add(_DEFAULT, _TIME, epochBytes); List<Row> puts = new ArrayList<Row>(); puts.add(put); for (Result result : followerRS) { Long followerid = Bytes.toLong(result.getColumnLatest(_DEFAULT, _USERID).getValue()); put = new Put(Bytes.toBytes(followerid + "-" + tranposeEpoch + "-" + user.getUserId())); put.add(_DEFAULT, _NAME, nameBytes); put.add(_DEFAULT, _TWEET, tweetBytes); put.add(_DEFAULT, _TIME, epochBytes); puts.add(put); } followerRS.close(); try { tweetlineTable.batch(puts); } catch (InterruptedException e) { e.printStackTrace(); // @TODO log and handle properly. } }
/** * Execute the passed <code>mutations</code> against <code>.META.</code> table. * * @param ct CatalogTracker on whose back we will ride the edit. * @param mutations Puts and Deletes to execute on .META. * @throws IOException */ static void mutateMetaTable(final CatalogTracker ct, final List<Mutation> mutations) throws IOException { HTable t = MetaReader.getMetaHTable(ct); try { t.batch(mutations); } catch (InterruptedException e) { InterruptedIOException ie = new InterruptedIOException(e.getMessage()); ie.initCause(e); throw ie; } finally { t.close(); } }
public void write(List<Mutation> mutations) throws IOException, InterruptedException { Object[] results = new Object[mutations.size()]; table.batch(mutations, results); }