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())); } } }
@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"); }
@Before public void before() throws Exception { Admin admin = TEST_UTIL.getAdmin(); if (admin.tableExists(TABLE)) { if (admin.isTableEnabled(TABLE)) admin.disableTable(TABLE); admin.deleteTable(TABLE); } HTableDescriptor htd = new HTableDescriptor(TABLE); htd.addFamily(new HColumnDescriptor(COLUMN_1).setMaxVersions(3)); htd.addFamily(new HColumnDescriptor(COLUMN_2).setMaxVersions(3)); htd.addFamily(new HColumnDescriptor(COLUMN_3).setMaxVersions(3)); admin.createTable(htd); try (Table table = TEST_UTIL.getConnection().getTable(TABLE)) { Put put = new Put(ROW_1); put.addColumn(COLUMN_1, QUALIFIER_1, TS_2, VALUE_1); table.put(put); put = new Put(ROW_2); put.addColumn(COLUMN_1, QUALIFIER_1, TS_1, VALUE_1); put.addColumn(COLUMN_1, QUALIFIER_1, TS_2, VALUE_2); put.addColumn(COLUMN_2, QUALIFIER_2, TS_2, VALUE_2); table.put(put); } remoteTable = new RemoteHTable( new Client(new Cluster().add("localhost", REST_TEST_UTIL.getServletPort())), TEST_UTIL.getConfiguration(), TABLE.toBytes()); }
RemoteHTable table = new RemoteHTable(client, "testtable"); // co RestExample-3-Table Create a remote table instance, wrapping the REST access into a familiar interface.
@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); }
public static HashMap<String, List<String>> readPdfsFromDocumentMetadataHBase(String remoteHost, int remotePort , String remoteTable) throws IOException{ final RemoteHTable table = new RemoteHTable(new Client(new Cluster().add(remoteHost, remotePort)), remoteTable); final ResultScanner scanner = table.getScanner(Bytes.toBytes(HBaseConstant.FAMILY_CONTENT) , Bytes.toBytes(HBaseConstant.FAMILY_CONTENT_QUALIFIER_PROTO)); final HashMap<String, List<String>> rowAuthorsMap = new HashMap<String, List<String>>(); try { for (final Result scannerResult : scanner) { final String rowId = new String(scannerResult.getRow()); final ArrayList<String> names = new ArrayList<String>(); if(scannerResult.getValue(Bytes.toBytes(HBaseConstant.FAMILY_CONTENT) , Bytes.toBytes(HBaseConstant.FAMILY_CONTENT_QUALIFIER_PROTO)) != null) { final MediaContainer mc = MediaContainer.parseFrom(scannerResult.value()); for(final Media media : mc.getMediaList()){ names.add(media.getMediaType()); } rowAuthorsMap.put(rowId, names); }else { System.out.println("Parsing problem occured on row "+rowId); } } } finally { scanner.close(); } return rowAuthorsMap; }
@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); } } }
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; }
public static void deleteRowFromHBase(String remoteHost, int remotePort, String remoteTable, List<String> rowsToDelete) throws IOException { RemoteHTable table = new RemoteHTable( new Client( new Cluster().add(remoteHost, remotePort)), remoteTable); List<byte[]> rowBytes = new ArrayList<byte[]>(); for (String s : rowsToDelete) { rowBytes.add(Bytes.toBytes(s)); } for (byte[] b : rowBytes) { table.delete(new Delete(b)); } table.close(); } }
@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); } } }
@Override public void addOrUpdateRow(String tableName, Row row) { Put p = Row2HBasePut.translate(row); RemoteHTable table = null; synchronized (mutex) { table = new RemoteHTable(client, tableName); } try { table.put(p); } catch (IOException e) { throw new ModifyTableException(e); } finally { try { table.close(); } catch (IOException e) { LOGGER.warn(EXCEPTION_DURING_CLOSING_H_BASE_TABLE, e); } } }
public static void deleteRowFromHBase(String remoteHost, int remotePort, String remoteTable, String rowToDelete) throws IOException { RemoteHTable table = new RemoteHTable( new Client( new Cluster().add(remoteHost, remotePort)), remoteTable); byte[] row3 = Bytes.toBytes(rowToDelete); table.delete(new Delete(row3)); table.close(); }
@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); } } }
@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"); }
public void fetchBwmetaAndSendItToHBase() { final BwmetaToHBaseFetcherImpl bwmetaToHBaseFetcherImpl = new BwmetaToHBaseFetcherImpl(store , new RemoteHTable(new Client(new Cluster().add(remoteHost, remotePort)) , remoteTable), storeListRecordsLimit); bwmetaToHBaseFetcherImpl.setRecordBwmetaExtractor(recordBwmetaExtractor); bwmetaToHBaseFetcherImpl.setBwmetaToHBaseTransformer(bwmetaToHBaseTransformer); bwmetaToHBaseFetcherImpl.setDocumentDTO2HBasePutTransformer(documentDTO2HBasePutTransformer); bwmetaToHBaseFetcher = bwmetaToHBaseFetcherImpl; bwmetaToHBaseFetcher.fetchBwmetaAndSendItToHBase(timestampFrom, timestampTo, collection); }
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(); } }
public static void importBwmetaAndSendToHBase(String zipDirPath, String collectionName, String remoteHost, int remotePort, String remoteTable) throws IOException { RemoteHTable table = new RemoteHTable(new Client(new Cluster().add(remoteHost, remotePort)), remoteTable); ZipDirToDocumentDTOIterator zdtp = new ZipDirToDocumentDTOIterator(zipDirPath, collectionName); for (DocumentDTO doc : zdtp) { try { table.put(DocumentDTO2HBasePut.translate(doc)); } catch (IOException e) { LOGGER.error(ERROR_STRING, e); } } table.close(); } }
public static void importBwmetaAndSendToHBase(String zipDirPath, String remoteHost, int remotePort, String remoteTable, String collectionName) throws IOException { RemoteHTable table = new RemoteHTable( new Client( new Cluster().add(remoteHost, remotePort)), remoteTable); Iterable<DocumentDTO> zdtp = new PdfZipDirToDocumentDTOIterable(zipDirPath, collectionName); for (DocumentDTO doc : zdtp) { try { table.put(DocumentDTO2HBasePut.translate(doc)); } catch (IOException e) { LOGGER.error(ERROR_STRING, e); } } table.close(); } }
public static void importBwmetaAndSendToHBase(String dirPath, String remoteHost, int remotePort, String remoteTable, String collectionName) throws IOException { RemoteHTable table = new RemoteHTable( new Client( new Cluster().add(remoteHost, remotePort)), remoteTable); Iterable<DocumentDTO> zdtp; if (collectionName != null) { zdtp = new OAIPMHDirToDocumentDTOIterator(dirPath, collectionName); } else { zdtp = new OAIPMHDirToDocumentDTOIterator(dirPath); } for (DocumentDTO doc : zdtp) { try { table.put(DocumentDTO2HBasePut.translate(doc)); } catch (IOException e) { LOGGER.error(ERROR_STRING, e); } } table.close(); } }
@Before public void before() throws Exception { Admin admin = TEST_UTIL.getAdmin(); if (admin.tableExists(TABLE)) { if (admin.isTableEnabled(TABLE)) admin.disableTable(TABLE); admin.deleteTable(TABLE); } HTableDescriptor htd = new HTableDescriptor(TABLE); htd.addFamily(new HColumnDescriptor(COLUMN_1).setMaxVersions(3)); htd.addFamily(new HColumnDescriptor(COLUMN_2).setMaxVersions(3)); htd.addFamily(new HColumnDescriptor(COLUMN_3).setMaxVersions(3)); admin.createTable(htd); try (Table table = TEST_UTIL.getConnection().getTable(TABLE)) { Put put = new Put(ROW_1); put.addColumn(COLUMN_1, QUALIFIER_1, TS_2, VALUE_1); table.put(put); put = new Put(ROW_2); put.addColumn(COLUMN_1, QUALIFIER_1, TS_1, VALUE_1); put.addColumn(COLUMN_1, QUALIFIER_1, TS_2, VALUE_2); put.addColumn(COLUMN_2, QUALIFIER_2, TS_2, VALUE_2); table.put(put); } remoteTable = new RemoteHTable( new Client(new Cluster().add("localhost", REST_TEST_UTIL.getServletPort())), TEST_UTIL.getConfiguration(), TABLE.toBytes()); }