@Override public void batch(final List<? extends Row> actions, final Object[] results) throws InterruptedException, IOException { int rpcTimeout = writeRpcTimeoutMs; boolean hasRead = false; boolean hasWrite = false; for (Row action : actions) { if (action instanceof Mutation) { hasWrite = true; } else { hasRead = true; } if (hasRead && hasWrite) { break; } } if (hasRead && !hasWrite) { rpcTimeout = readRpcTimeoutMs; } batch(actions, results, rpcTimeout); }
public void batchMutate(List<Mutation> mutations) throws Exception { Object[] result = new Object[mutations.size()]; try { table.batch(mutations, result); } catch (InterruptedException e) { LOG.warn("Error performing a mutation to HBase.", e); throw e; } catch (IOException e) { LOG.warn("Error performing a mutation to HBase.", e); throw e; } }
@Override public void put(final List<Put> puts) throws IOException { for (Put put : puts) { validatePut(put); } Object[] results = new Object[puts.size()]; try { batch(puts, results, writeRpcTimeoutMs); } catch (InterruptedException e) { throw (InterruptedIOException) new InterruptedIOException().initCause(e); } }
@Override public void delete(final List<Delete> deletes) throws IOException { Object[] results = new Object[deletes.size()]; try { batch(deletes, results, writeRpcTimeoutMs); } catch (InterruptedException e) { throw (InterruptedIOException)new InterruptedIOException().initCause(e); } finally { // TODO: to be consistent with batch put(), do not modify input list // mutate list so that it is empty for complete success, or contains only failed records // results are returned in the same order as the requests in list walk the list backwards, // so we can remove from list without impacting the indexes of earlier members for (int i = results.length - 1; i>=0; i--) { // if result is not null, it succeeded if (results[i] instanceof Result) { deletes.remove(i); } } } }
@Override public Result[] get(List<Get> gets) throws IOException { if (gets.size() == 1) { return new Result[]{get(gets.get(0))}; } try { Object[] r1 = new Object[gets.size()]; batch((List<? extends Row>)gets, r1, readRpcTimeoutMs); // Translate. Result [] results = new Result[r1.length]; int i = 0; for (Object obj: r1) { // Batch ensures if there is a failure we get an exception instead results[i++] = (Result)obj; } return results; } catch (InterruptedException e) { throw (InterruptedIOException)new InterruptedIOException().initCause(e); } }
@Override public boolean[] exists(List<Get> gets) throws IOException { if (gets.isEmpty()) return new boolean[]{}; if (gets.size() == 1) return new boolean[]{exists(gets.get(0))}; ArrayList<Get> exists = new ArrayList<>(gets.size()); for (Get g: gets){ Get ge = new Get(g); ge.setCheckExistenceOnly(true); exists.add(ge); } Object[] r1= new Object[exists.size()]; try { batch(exists, r1, readRpcTimeoutMs); } catch (InterruptedException e) { throw (InterruptedIOException)new InterruptedIOException().initCause(e); } // translate. boolean[] results = new boolean[r1.length]; int i = 0; for (Object o : r1) { // batch ensures if there is a failure we get an exception instead results[i++] = ((Result)o).getExists(); } return results; }
@Override public Void run() throws Exception { for (Row r : actions) { if (r instanceof Put) { ((Put) r).setWriteToWAL(enableWal); } // Newer versions of HBase - Increment implements Row. if (r instanceof Increment) { ((Increment) r).setWriteToWAL(enableWal); } } table.batch(actions); return null; } });
HTable ht = (HTable) con.getTable(DUMMY_TABLE, ap.service); ht.multiAp = ap; ht.batch(gets, null);
@Test public void testBatch() throws IOException, InterruptedException { ClusterConnection conn = new MyConnectionImpl(CONF); HTable ht = (HTable) conn.getTable(DUMMY_TABLE); ht.multiAp = new MyAsyncProcess(conn, CONF); List<Put> puts = new ArrayList<>(7); puts.add(createPut(1, true)); puts.add(createPut(1, true)); puts.add(createPut(1, true)); puts.add(createPut(1, true)); puts.add(createPut(1, false)); // <=== the bad apple, position 4 puts.add(createPut(1, true)); puts.add(createPut(1, false)); // <=== another bad apple, position 6 Object[] res = new Object[puts.size()]; try { ht.batch(puts, res); Assert.fail(); } catch (RetriesExhaustedException expected) { } Assert.assertEquals(success, res[0]); Assert.assertEquals(success, res[1]); Assert.assertEquals(success, res[2]); Assert.assertEquals(success, res[3]); Assert.assertEquals(failure, res[4]); Assert.assertEquals(success, res[5]); Assert.assertEquals(failure, res[6]); } @Test
@Override public void batch(final List<? extends Row> actions, final Object[] results) throws InterruptedException, IOException { int rpcTimeout = writeRpcTimeoutMs; boolean hasRead = false; boolean hasWrite = false; for (Row action : actions) { if (action instanceof Mutation) { hasWrite = true; } else { hasRead = true; } if (hasRead && hasWrite) { break; } } if (hasRead && !hasWrite) { rpcTimeout = readRpcTimeoutMs; } batch(actions, results, rpcTimeout); }
@Override public void put(final List<Put> puts) throws IOException { for (Put put : puts) { validatePut(put); } Object[] results = new Object[puts.size()]; try { batch(puts, results, writeRpcTimeoutMs); } catch (InterruptedException e) { throw (InterruptedIOException) new InterruptedIOException().initCause(e); } }
@Override public void delete(final List<Delete> deletes) throws IOException { Object[] results = new Object[deletes.size()]; try { batch(deletes, results, writeRpcTimeoutMs); } catch (InterruptedException e) { throw (InterruptedIOException)new InterruptedIOException().initCause(e); } finally { // TODO: to be consistent with batch put(), do not modify input list // mutate list so that it is empty for complete success, or contains only failed records // results are returned in the same order as the requests in list walk the list backwards, // so we can remove from list without impacting the indexes of earlier members for (int i = results.length - 1; i>=0; i--) { // if result is not null, it succeeded if (results[i] instanceof Result) { deletes.remove(i); } } } }
@Override public Result[] get(List<Get> gets) throws IOException { if (gets.size() == 1) { return new Result[]{get(gets.get(0))}; } try { Object[] r1 = new Object[gets.size()]; batch((List<? extends Row>)gets, r1, readRpcTimeoutMs); // Translate. Result [] results = new Result[r1.length]; int i = 0; for (Object obj: r1) { // Batch ensures if there is a failure we get an exception instead results[i++] = (Result)obj; } return results; } catch (InterruptedException e) { throw (InterruptedIOException)new InterruptedIOException().initCause(e); } }
@Override public boolean[] exists(List<Get> gets) throws IOException { if (gets.isEmpty()) return new boolean[]{}; if (gets.size() == 1) return new boolean[]{exists(gets.get(0))}; ArrayList<Get> exists = new ArrayList<>(gets.size()); for (Get g: gets){ Get ge = new Get(g); ge.setCheckExistenceOnly(true); exists.add(ge); } Object[] r1= new Object[exists.size()]; try { batch(exists, r1, readRpcTimeoutMs); } catch (InterruptedException e) { throw (InterruptedIOException)new InterruptedIOException().initCause(e); } // translate. boolean[] results = new boolean[r1.length]; int i = 0; for (Object o : r1) { // batch ensures if there is a failure we get an exception instead results[i++] = ((Result)o).getExists(); } return results; }
HTable ht = (HTable) con.getTable(DUMMY_TABLE, ap.service); ht.multiAp = ap; ht.batch(gets, null);
@Override public void batch(List<? extends Row> actions, Object[] results) throws IOException, InterruptedException { table.batch(actions, results); }
@Test public void testBatch() throws IOException, InterruptedException { ClusterConnection conn = new MyConnectionImpl(CONF); HTable ht = (HTable) conn.getTable(DUMMY_TABLE); ht.multiAp = new MyAsyncProcess(conn, CONF); List<Put> puts = new ArrayList<>(7); puts.add(createPut(1, true)); puts.add(createPut(1, true)); puts.add(createPut(1, true)); puts.add(createPut(1, true)); puts.add(createPut(1, false)); // <=== the bad apple, position 4 puts.add(createPut(1, true)); puts.add(createPut(1, false)); // <=== another bad apple, position 6 Object[] res = new Object[puts.size()]; try { ht.batch(puts, res); Assert.fail(); } catch (RetriesExhaustedException expected) { } Assert.assertEquals(success, res[0]); Assert.assertEquals(success, res[1]); Assert.assertEquals(success, res[2]); Assert.assertEquals(success, res[3]); Assert.assertEquals(failure, res[4]); Assert.assertEquals(success, res[5]); Assert.assertEquals(failure, res[6]); } @Test
public void batchMutate(List<Mutation> mutations) throws Exception { Object[] result = new Object[mutations.size()]; try { table.batch(mutations, result); } catch (InterruptedException e) { LOG.warn("Error performing a mutation to HBase.", e); throw e; } catch (IOException e) { LOG.warn("Error performing a mutation to HBase.", e); throw e; } }
@Override public void put(final List<Put> puts) throws IOException { for (Put put : puts) { validatePut(put); } Object[] results = new Object[puts.size()]; try { batch(puts, results, writeRpcTimeoutMs); } catch (InterruptedException e) { throw (InterruptedIOException) new InterruptedIOException().initCause(e); } }
@Override public void batch(List<? extends Row> actions, Object[] results) throws IOException, InterruptedException { // TODO Auto-generated method stub getTable().batch(actions, results); }