public void run() throws Exception { Cluster cluster = new Cluster(); cluster.add(host, port); Client restClient = new Client(cluster, conf.getBoolean(Constants.REST_SSL_ENABLED, false)); try (RemoteHTable remoteTable = new RemoteHTable(restClient, conf, "example")) { // Write data to the table String rowKey = "row1"; Put p = new Put(rowKey.getBytes()); p.addColumn("family1".getBytes(), "qualifier1".getBytes(), "value1".getBytes()); remoteTable.put(p); // Get the data from the table Get g = new Get(rowKey.getBytes()); Result result = remoteTable.get(g); Preconditions.checkArgument(result != null, Bytes.toString(remoteTable.getTableName()) + " should have a row with key as " + rowKey); System.out.println("row = " + new String(result.getRow())); for (Cell cell : result.rawCells()) { System.out.print("family = " + Bytes.toString(CellUtil.cloneFamily(cell)) + "\t"); System.out.print("qualifier = " + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t"); System.out.print("value = " + Bytes.toString(CellUtil.cloneValue(cell)) + "\t"); System.out.println("timestamp = " + Long.toString(cell.getTimestamp())); } } }
@Override public void delete(List<Delete> deletes) throws IOException { for (Delete delete: deletes) { delete(delete); } }
/** * Test RemoteHable.Scanner.iterator method */ @Test public void testIteratorScaner() throws IOException { List<Put> puts = new ArrayList<>(4); Put put = new Put(ROW_1); put.addColumn(COLUMN_1, QUALIFIER_1, VALUE_1); puts.add(put); put = new Put(ROW_2); put.addColumn(COLUMN_1, QUALIFIER_1, VALUE_1); puts.add(put); put = new Put(ROW_3); put.addColumn(COLUMN_1, QUALIFIER_1, VALUE_1); puts.add(put); put = new Put(ROW_4); put.addColumn(COLUMN_1, QUALIFIER_1, VALUE_1); puts.add(put); remoteTable.put(puts); ResultScanner scanner = remoteTable.getScanner(new Scan()); Iterator<Result> iterator = scanner.iterator(); assertTrue(iterator.hasNext()); int counter = 0; while (iterator.hasNext()) { iterator.next(); counter++; } assertEquals(4, counter); }
@Override public void put(Put put) throws IOException { CellSetModel model = buildModelFromPut(put); StringBuilder sb = new StringBuilder(); sb.append('/'); sb.append(Bytes.toString(name)); sb.append('/'); sb.append(toURLEncodedBytes(put.getRow())); for (int i = 0; i < maxRetries; i++) { Response response = client.put(sb.toString(), Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput()); int code = response.getCode(); switch (code) { case 200: return; case 509: try { Thread.sleep(sleepTime); } catch (InterruptedException e) { throw (InterruptedIOException)new InterruptedIOException().initCause(e); } break; default: throw new IOException("put request failed with " + code); } } throw new IOException("put request timed out"); }
@Override public Result[] get(List<Get> gets) throws IOException { byte[][] rows = new byte[gets.size()][]; int maxVersions = 1; int count = 0; for(Get g:gets) { if ( count == 0 ) { maxVersions = g.getMaxVersions(); } else if (g.getMaxVersions() != maxVersions) { LOG.warn("MaxVersions on Gets do not match, using the first in the list ("+maxVersions+")"); } if (g.getFilter() != null) { LOG.warn("filters not supported on gets"); } rows[count] = g.getRow(); count ++; } String spec = buildMultiRowSpec(rows, maxVersions); return getResults(spec); }
@Override public void addOrUpdateRows(String tableName, List<Row> rows) { RemoteHTable table = null; synchronized (mutex) { table = new RemoteHTable(client, tableName); } try { table.put(convertRowsToPuts(rows)); } catch (IOException e) { throw new ModifyTableException(e); } finally { try { table.close(); } catch (IOException e) { LOGGER.warn(EXCEPTION_DURING_CLOSING_H_BASE_TABLE, e); } } }
private Result[] getRawRows(String tableName, Row startRow, int numberOfRows) { Result[] result = new Result[] {}; RemoteHTable table = null; synchronized (mutex) { table = new RemoteHTable(client, tableName); } try { Scan s=startRow==null?new Scan():new Scan(startRow.getId()); ResultScanner resultScanner = table.getScanner(s); result = resultScanner.next(numberOfRows); } catch (IOException e) { throw new ReadTableException("Exception during fetching rows", e); } finally { try { table.close(); } catch (IOException e) { LOGGER.warn(EXCEPTION_DURING_CLOSING_H_BASE_TABLE, e); } } return result; }
@Override public void deleteRow(String tableName, Row row) { final RemoteHTable table; synchronized (mutex) { table = new RemoteHTable(client, tableName); } try { table.delete(new Delete(row.getId())); } catch (IOException e) { throw new ModifyTableException(EXCEPTION_DURING_DELETING_ROW, e); } finally { try { table.close(); } catch (IOException e) { throw new ModifyTableException(EXCEPTION_DURING_CLOSING_TABLE, e); } } }
@Test public void testCheckAndDelete() throws IOException { Get get = new Get(ROW_1); Result result = remoteTable.get(get); byte[] value1 = result.getValue(COLUMN_1, QUALIFIER_1); byte[] value2 = result.getValue(COLUMN_2, QUALIFIER_2); assertNotNull(value1); assertTrue(Bytes.equals(VALUE_1, value1)); assertNull(value2); assertTrue(remoteTable.exists(get)); assertEquals(1, remoteTable.existsAll(Collections.singletonList(get)).length); Delete delete = new Delete(ROW_1); remoteTable.checkAndMutate(ROW_1, COLUMN_1).qualifier(QUALIFIER_1) .ifEquals(VALUE_1).thenDelete(delete); assertFalse(remoteTable.exists(get)); Put put = new Put(ROW_1); put.addColumn(COLUMN_1, QUALIFIER_1, VALUE_1); remoteTable.put(put); assertTrue(remoteTable.checkAndMutate(ROW_1, COLUMN_1).qualifier(QUALIFIER_1) .ifEquals(VALUE_1).thenPut(put)); assertFalse(remoteTable.checkAndMutate(ROW_1, COLUMN_1).qualifier(QUALIFIER_1) .ifEquals(VALUE_2).thenPut(put)); }
put.addColumn(COLUMN_3, QUALIFIER_1, VALUE_1); put.addColumn(COLUMN_3, QUALIFIER_2, VALUE_2); remoteTable.put(put); get.addFamily(COLUMN_2); get.addFamily(COLUMN_3); Result result = remoteTable.get(get); byte[] value1 = result.getValue(COLUMN_1, QUALIFIER_1); byte[] value2 = result.getValue(COLUMN_2, QUALIFIER_2); remoteTable.delete(delete); result = remoteTable.get(get); value1 = result.getValue(COLUMN_1, QUALIFIER_1); value2 = result.getValue(COLUMN_2, QUALIFIER_2); remoteTable.delete(delete); result = remoteTable.get(get); value1 = result.getValue(COLUMN_1, QUALIFIER_1); value2 = result.getValue(COLUMN_2, QUALIFIER_2); remoteTable.delete(delete); result = remoteTable.get(get); value3 = result.getValue(COLUMN_3, QUALIFIER_1); value4 = result.getValue(COLUMN_3, QUALIFIER_2); remoteTable.delete(delete);
public static void main(String[] args) throws IOException { RemoteHTable table = new RemoteHTable( new Client( new Cluster().add("localhost", 8080)), "test"); Person john = Person.newBuilder().setId(1234).setName("John Doe").setEmail("jdoe@example.com").addPhone( Person.PhoneNumber.newBuilder().setNumber("555-4321").setType(Person.PhoneType.HOME)).build(); byte[] row = Bytes.toBytes("row3"); table.delete(new Delete(row)); Put put = new Put(row); byte[] family = Bytes.toBytes("data"); byte[] qualifier = Bytes.toBytes("3"); byte[] value = john.toByteArray(); put.add(family, qualifier, value); table.put(put); table.close(); } }
@SuppressWarnings("resource") public void init(final String tableName, final String family1st, final String qualifier1st) throws IOException { scan.addColumn(Bytes.toBytes(family1st), Bytes.toBytes(qualifier1st)); resultScanner = new RemoteHTable(client, tableName).getScanner(scan); }
@Override public boolean exists(Get get) throws IOException { LOG.warn("exists() is really get(), just use get()"); Result result = get(get); return (result != null && !(result.isEmpty())); }
Put put = new Put(ROW_3); put.addColumn(COLUMN_1, QUALIFIER_1, VALUE_1); remoteTable.put(put); Result result = remoteTable.get(get); byte[] value = result.getValue(COLUMN_1, QUALIFIER_1); assertNotNull(value); put.addColumn(COLUMN_2, QUALIFIER_2, VALUE_2); puts.add(put); remoteTable.put(puts); result = remoteTable.get(get); value = result.getValue(COLUMN_2, QUALIFIER_2); assertNotNull(value); assertTrue(Bytes.equals(VALUE_2, value)); get = new Get(ROW_4); result = remoteTable.get(get); value = result.getValue(COLUMN_1, QUALIFIER_1); assertNotNull(value); assertTrue(Bytes.equals(VALUE_2, value)); assertTrue(Bytes.equals(Bytes.toBytes("TestRemoteTable" + VALID_TABLE_NAME_CHARS), remoteTable.getTableName()));
RemoteHTable table = new RemoteHTable(client, "testtable"); // co RestExample-3-Table Create a remote table instance, wrapping the REST access into a familiar interface. Result result1 = table.get(get); scan.setStopRow(Bytes.toBytes("row-15")); scan.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("col-5")); ResultScanner scanner = table.getScanner(scan); // co RestExample-5-Scan Scan the table, again, the same approach as if using the native Java API.
put.addColumn(COLUMN_1, QUALIFIER_1, VALUE_1); puts.add(put); remoteTable.put(puts); ResultScanner scanner = remoteTable.getScanner(new Scan()); scanner.close(); scanner = remoteTable.getScanner(COLUMN_1); results = scanner.next(4); assertNotNull(results); scanner = remoteTable.getScanner(COLUMN_1,QUALIFIER_1); results = scanner.next(4); assertNotNull(results); assertTrue(Bytes.equals(ROW_4, results[3].getRow())); scanner.close(); assertTrue(remoteTable.isAutoFlush());
@Before public void setup() throws Exception { client = mock(Client.class); Response response = new Response(509); when(client.get(anyString(), anyString())).thenReturn(response); when(client.delete(anyString())).thenReturn(response); when(client.put(anyString(), anyString(), any())).thenReturn( response); when(client.post(anyString(), anyString(), any())).thenReturn( response); Configuration configuration = TEST_UTIL.getConfiguration(); configuration.setInt("hbase.rest.client.max.retries", RETRIES); configuration.setInt("hbase.rest.client.sleep", SLEEP_TIME); remoteTable = new RemoteHTable(client, TEST_UTIL.getConfiguration(), "MyTable"); }
@Override public void addOrUpdateRows(String tableName, List<Row> rows) { RemoteHTable table = new RemoteHTable(client, tableName); List<Put> puts = convertRowsToPuts(rows); try { table.batch(puts); } catch (IOException e) { throw new ModifyTableException(e); } finally { try { table.close(); } catch (IOException e) { LOGGER.warn(EXCEPTION_MESSAGE, e); } } }
@Override public void run() throws Exception { remoteTable.put(new Put(Bytes.toBytes("Row"))); } });
@Override public TableDescriptor getDescriptor() throws IOException { return getTableDescriptor(); }