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())); } } }
Map<byte[], String> readBulkLoadedFiles(String backupId) throws IOException { Scan scan = BackupSystemTable.createScanForBulkLoadedFiles(backupId); try (Table table = connection.getTable(bulkLoadTableName); ResultScanner scanner = table.getScanner(scan)) { Result res = null; Map<byte[], String> map = new TreeMap<>(Bytes.BYTES_COMPARATOR); while ((res = scanner.next()) != null) { res.advance(); byte[] row = CellUtil.cloneRow(res.listCells().get(0)); for (Cell cell : res.listCells()) { if (CellUtil.compareQualifiers(cell, BackupSystemTable.PATH_COL, 0, BackupSystemTable.PATH_COL.length) == 0) { map.put(row, Bytes.toString(CellUtil.cloneValue(cell))); } } } return map; } }
public static boolean matchingColumn(final Cell left, final byte[] fam, final byte[] qual) { if (!matchingFamily(left, fam)) return false; return matchingQualifier(left, qual); }
/** * Constructor from KeyValue * @param cell */ public CellModel(org.apache.hadoop.hbase.Cell cell) { this(CellUtil.cloneFamily(cell), CellUtil.cloneQualifier(cell), cell.getTimestamp(), CellUtil .cloneValue(cell)); }
/**************** equals ****************************/ public static boolean equals(Cell a, Cell b) { return matchingRows(a, b) && matchingFamily(a, b) && matchingQualifier(a, b) && matchingTimestamp(a, b) && PrivateCellUtil.matchingType(a, b); }
Table ht = TEST_UTIL.createTable(tableName, FAMILY); byte[][] ROWS = makeN(ROW, 10); byte[][] QUALIFIERS = { Bytes.toBytes("col0-<d2v1>-<d3v2>"), Bytes.toBytes("col1-<d2v1>-<d3v2>"), Bytes.toBytes("col2-<d2v1>-<d3v2>"), Bytes.toBytes("col3-<d2v1>-<d3v2>"), Bytes.toBytes("col4-<d2v1>-<d3v2>"), Put put = new Put(ROWS[i]); put.addColumn(FAMILY, QUALIFIERS[i], VALUE); ht.put(put); Scan scan = new Scan(); scan.setReversed(true); scan.addFamily(FAMILY); Filter filter = new KeyOnlyFilter(true); scan.setFilter(filter); ResultScanner scanner = ht.getScanner(scan); int count = 0; for (Result result : ht.getScanner(scan)) { assertEquals(1, result.size()); assertEquals(Bytes.SIZEOF_INT, result.rawCells()[0].getValueLength()); assertEquals(VALUE.length, Bytes.toInt(CellUtil.cloneValue(result.rawCells()[0]))); count++;
Put put = new Put(Bytes.toBytes("row")); put.addColumn(FAMILY, QUALIFIER, VALUE); table.put(put); Scan scan = new Scan(); scan.setFilter(new FilterList()); try (ResultScanner scanner = table.getScanner(scan)) { for (Result r : scanner) { scanResults.add(r); Get g = new Get(Bytes.toBytes("row")); g.setFilter(new FilterList()); Result getResult = table.get(g); Result scanResult = scanResults.get(0); assertEquals(scanResult.rawCells().length, getResult.rawCells().length); for (int i = 0; i != scanResult.rawCells().length; ++i) { Cell scanCell = scanResult.rawCells()[i]; Cell getCell = getResult.rawCells()[i]; assertEquals(0, Bytes.compareTo(CellUtil.cloneRow(scanCell), CellUtil.cloneRow(getCell))); assertEquals(0, Bytes.compareTo(CellUtil.cloneFamily(scanCell), CellUtil.cloneFamily(getCell))); assertEquals(0, Bytes.compareTo(CellUtil.cloneQualifier(scanCell), CellUtil.cloneQualifier(getCell))); assertEquals(0, Bytes.compareTo(CellUtil.cloneValue(scanCell), CellUtil.cloneValue(getCell)));
public void testKeepDeletedCells() throws Exception { final TableName tableName = TableName.valueOf(name.getMethodName()); final byte[] FAMILY = Bytes.toBytes("family"); final byte[] C0 = Bytes.toBytes("c0"); final byte[] T1 = Bytes.toBytes("T1"); Get g = new Get(T1); g.setTimeRange(0, ts + 3); Result r = h.get(g); assertArrayEquals(T2, r.getValue(FAMILY, C0)); Scan s = new Scan(T1); s.setTimeRange(0, ts + 3); s.setMaxVersions(); ResultScanner scanner = h.getScanner(s); Cell[] kvs = scanner.next().rawCells(); assertArrayEquals(T2, CellUtil.cloneValue(kvs[0])); assertArrayEquals(T1, CellUtil.cloneValue(kvs[1])); scanner.close(); kvs = scanner.next().rawCells(); assertTrue(PrivateCellUtil.isDeleteFamily(kvs[0])); assertArrayEquals(T3, CellUtil.cloneValue(kvs[1])); assertTrue(CellUtil.isDelete(kvs[2])); assertArrayEquals(T2, CellUtil.cloneValue(kvs[3])); assertArrayEquals(T1, CellUtil.cloneValue(kvs[4]));
@Test public void testPut() throws IOException { final byte [] CONTENTS_FAMILY = Bytes.toBytes("contents"); final byte [] SMALL_FAMILY = Bytes.toBytes("smallfam"); final byte [] row1 = Bytes.toBytes("row1"); final byte [] row2 = Bytes.toBytes("row2"); final byte [] value = Bytes.toBytes("abcd"); Put put = new Put(row1); put.addColumn(CONTENTS_FAMILY, null, value); table.put(put); assertTrue(Bytes.equals(CellUtil.cloneFamily(kv), CONTENTS_FAMILY)); assertTrue(Bytes.equals(CellUtil.cloneQualifier(kv), new byte[0])); assertTrue(Bytes.equals(CellUtil.cloneValue(kv), value)); table.put(put); Scan scan = new Scan(); scan.addColumn(CONTENTS_FAMILY, null); ResultScanner scanner = table.getScanner(scan); for (Result r : scanner) { for(Cell key : r.rawCells()) { System.out.println(Bytes.toString(r.getRow()) + ": " + key.toString());
@Test public void testMultiColumns() throws Exception { final byte [] TABLE = Bytes.toBytes(name.getMethodName()); byte [] FAMILY = Bytes.toBytes("event_log"); byte [][] FAMILIES = new byte[][] { FAMILY }; Put p = new Put(Bytes.toBytes("row")); p.addColumn(FAMILY, Bytes.toBytes("column0"), 3L, Bytes.toBytes("value0-3")); p.addColumn(FAMILY, Bytes.toBytes("column1"), 3L, Bytes.toBytes("value1-3")); p.addColumn(FAMILY, Bytes.toBytes("column4"), 2L, Bytes.toBytes("value4-2")); p.addColumn(FAMILY, Bytes.toBytes("column4"), 3L, Bytes.toBytes("value4-3")); ht.put(p); TimestampsFilter filter = new TimestampsFilter(timestamps); Result result = ht.get(g); for (Cell kv : result.listCells()) { System.out.println("found row " + Bytes.toString(CellUtil.cloneRow(kv)) + ", column " + Bytes.toString(CellUtil.cloneQualifier(kv)) + ", value " + Bytes.toString(CellUtil.cloneValue(kv))); assertEquals(2, result.listCells().size()); assertTrue(CellUtil.matchingValue(result.listCells().get(0), Bytes.toBytes("value2-3"))); assertTrue(CellUtil.matchingValue(result.listCells().get(1), Bytes.toBytes("value4-3"))); ht.close();
private void verify(final Table table) throws IOException { Scan scan = new Scan(); scan.addColumn(FAMILY_NAME, COLUMN_NAME); scan.setMaxVersions(1); ResultScanner scanner = table.getScanner(scan); for (Result r: scanner) { for (Cell kv : r.listCells()) { log.debug(Bytes.toString(r.getRow()) + "\t" + Bytes.toString(CellUtil.cloneFamily(kv)) + "\t" + Bytes.toString(CellUtil.cloneQualifier(kv)) + "\t" + kv.getTimestamp() + "\t" + Bytes.toBoolean(CellUtil.cloneValue(kv))); org.junit.Assert.assertEquals(TIMESTAMP.get(kv.getTimestamp()), Bytes.toBoolean(CellUtil.cloneValue(kv))); } } scanner.close(); }
private void runScanner(Table hTable, int expectedSize, Filter... filters) throws IOException { String cf = "f"; Scan scan = new Scan(); scan.addFamily(cf.getBytes()); FilterList filterList = new FilterList(filters); scan.setFilter(filterList); ResultScanner scanner = hTable.getScanner(scan); List<Cell> results = new ArrayList<>(); Result result; long timeBeforeScan = System.currentTimeMillis(); while ((result = scanner.next()) != null) { for (Cell kv : result.listCells()) { LOG.info("Got rk: " + Bytes.toStringBinary(CellUtil.cloneRow(kv)) + " cq: " + Bytes.toStringBinary(CellUtil.cloneQualifier(kv))); results.add(kv); } } long scanTime = System.currentTimeMillis() - timeBeforeScan; scanner.close(); LOG.info("scan time = " + scanTime + "ms"); LOG.info("found " + results.size() + " results"); assertEquals(expectedSize, results.size()); } }
/** * Do a small loading into a table, make sure the data is really the same, then run the * VerifyReplication job to check the results. Do a second comparison where all the cells are * different. */ @Test public void testVerifyRepJob() throws Exception { // Populate the tables, at the same time it guarantees that the tables are // identical since it does the check runSmallBatchTest(); String[] args = new String[] { PEER_ID, tableName.getNameAsString() }; runVerifyReplication(args, NB_ROWS_IN_BATCH, 0); Scan scan = new Scan(); ResultScanner rs = htable2.getScanner(scan); Put put = null; for (Result result : rs) { put = new Put(result.getRow()); Cell firstVal = result.rawCells()[0]; put.addColumn(CellUtil.cloneFamily(firstVal), CellUtil.cloneQualifier(firstVal), Bytes.toBytes("diff data")); htable2.put(put); } Delete delete = new Delete(put.getRow()); htable2.delete(delete); runVerifyReplication(args, 0, NB_ROWS_IN_BATCH); }
private void verifyRows(Table t, byte[] family, byte[] column) throws IOException { for (int i = 0; i < 10; i++) { byte[] row = Bytes.toBytes("row" + i); Get g = new Get(row).addFamily(family); Result r = t.get(g); Assert.assertNotNull(r); Assert.assertEquals(1, r.size()); Cell cell = r.rawCells()[0]; Assert.assertTrue(CellUtil.matchingQualifier(cell, column)); Assert.assertEquals(Bytes.compareTo(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength(), row, 0, row.length), 0); } }
private void verifyData(Region newReg, int startRow, int numRows, byte[] qf, byte[]... families) throws IOException { for (int i = startRow; i < startRow + numRows; i++) { byte[] row = Bytes.toBytes("" + i); Get get = new Get(row); for (byte[] family : families) { get.addColumn(family, qf); } Result result = newReg.get(get); Cell[] raw = result.rawCells(); assertEquals(families.length, result.size()); for (int j = 0; j < families.length; j++) { assertTrue(CellUtil.matchingRows(raw[j], row)); assertTrue(CellUtil.matchingFamily(raw[j], families[j])); assertTrue(CellUtil.matchingQualifier(raw[j], qf)); } } }
/** * Assert that the passed in Cell has expected contents for the specified row, * column & timestamp. */ private void checkOneCell(Cell kv, byte[] cf, int rowIdx, int colIdx, long ts) { String ctx = "rowIdx=" + rowIdx + "; colIdx=" + colIdx + "; ts=" + ts; assertEquals("Row mismatch which checking: " + ctx, "row:" + rowIdx, Bytes.toString(CellUtil.cloneRow(kv))); assertEquals("ColumnFamily mismatch while checking: " + ctx, Bytes.toString(cf), Bytes.toString(CellUtil.cloneFamily(kv))); assertEquals("Column qualifier mismatch while checking: " + ctx, "column:" + colIdx, Bytes.toString(CellUtil.cloneQualifier(kv))); assertEquals("Timestamp mismatch while checking: " + ctx, ts, kv.getTimestamp()); assertEquals("Value mismatch while checking: " + ctx, "value-version-" + ts, Bytes.toString(CellUtil.cloneValue(kv))); }
setUp(0L, tableName); long ts = System.currentTimeMillis(); byte[] value1 = Bytes.toBytes("value1"); Put put1 = new Put(row1); put1.addColumn(family, qf1, ts, value1); table.put(put1); Put put2 = new Put(row2); byte[] value2 = Bytes.toBytes("value2"); put2.addColumn(family, qf1, ts, value2); table.put(put2); Scan scan = new Scan(); scan.setCaching(1); ResultScanner rs = table.getScanner(scan); Result result = rs.next(); Put put3 = new Put(row1); byte[] value3 = Bytes.toBytes("value3"); put3.addColumn(family, qf1, ts, value3); table.put(put3); table.put(put4); Cell cell = result.getColumnLatestCell(family, qf1); Assert.assertArrayEquals(value1, CellUtil.cloneValue(cell)); cell = result.getColumnLatestCell(family, qf1); Assert.assertArrayEquals(value2, CellUtil.cloneValue(cell));
private void assertNResult(Result result, byte [] row, byte [] family, byte [] qualifier, long [] stamps, byte [][] values, int start, int end) throws IOException { assertTrue("Expected row [" + Bytes.toString(row) + "] " + "Got row [" + Bytes.toString(result.getRow()) +"]", equals(row, result.getRow())); int expectedResults = end - start + 1; assertEquals(expectedResults, result.size()); Cell[] keys = result.rawCells(); for (int i=0; i<keys.length; i++) { byte [] value = values[end-i]; long ts = stamps[end-i]; Cell key = keys[i]; assertTrue("(" + i + ") Expected family [" + Bytes.toString(family) + "] " + "Got family [" + Bytes.toString(CellUtil.cloneFamily(key)) + "]", CellUtil.matchingFamily(key, family)); assertTrue("(" + i + ") Expected qualifier [" + Bytes.toString(qualifier) + "] " + "Got qualifier [" + Bytes.toString(CellUtil.cloneQualifier(key))+ "]", CellUtil.matchingQualifier(key, qualifier)); assertTrue("Expected ts [" + ts + "] " + "Got ts [" + key.getTimestamp() + "]", ts == key.getTimestamp()); assertTrue("(" + i + ") Expected value [" + Bytes.toString(value) + "] " + "Got value [" + Bytes.toString(CellUtil.cloneValue(key)) + "]", CellUtil.matchingValue(key, value)); } }
Table table = TEST_UTIL.createTable(tableName, FAMILY); final byte[] row = Bytes.toBytes("p"); Put p = new Put(row); p.addColumn(FAMILY, QUALIFIER, VALUE); table.put(p); try (ResultScanner scanner = table.getScanner(new Scan())) { Result result = scanner.next(); assertNotNull(result); CellScanner cs = result.cellScanner(); assertTrue(cs.advance()); Cell c = cs.current(); assertTrue(CellUtil.isPut(c)); assertFalse(CellUtil.isDelete(c)); assertFalse(cs.advance()); assertNull(scanner.next()); table.delete(d); assertTrue("Cell should be a Delete: " + c, CellUtil.isDelete(c)); assertFalse("Cell should not be a Put: " + c, CellUtil.isPut(c)); assertFalse("Cell should not be a Delete: " + c, CellUtil.isDelete(c)); assertTrue("Cell should be a Put: " + c, CellUtil.isPut(c));
private void migrateNamespaceTable() throws IOException { try (Table nsTable = masterServices.getConnection().getTable(TableName.NAMESPACE_TABLE_NAME); ResultScanner scanner = nsTable.getScanner( new Scan().addFamily(TableDescriptorBuilder.NAMESPACE_FAMILY_INFO_BYTES).readAllVersions()); BufferedMutator mutator = masterServices.getConnection().getBufferedMutator(TableName.META_TABLE_NAME)) { for (Result result;;) { result = scanner.next(); if (result == null) { break; } Put put = new Put(result.getRow()); result .getColumnCells(TableDescriptorBuilder.NAMESPACE_FAMILY_INFO_BYTES, TableDescriptorBuilder.NAMESPACE_COL_DESC_BYTES) .forEach(c -> put.addColumn(HConstants.NAMESPACE_FAMILY, HConstants.NAMESPACE_COL_DESC_QUALIFIER, c.getTimestamp(), CellUtil.cloneValue(c))); mutator.mutate(put); } } // schedule a disable procedure instead of block waiting here, as when disabling a table we will // wait until master is initialized, but we are part of the initialization... masterServices.getMasterProcedureExecutor().submitProcedure( new DisableTableProcedure(masterServices.getMasterProcedureExecutor().getEnvironment(), TableName.NAMESPACE_TABLE_NAME, false)); }