private static Long latestVersion(Hashtable<String, String> config, dbutil db_util) throws Exception { if (!config.get("timestamp_stop").equals(Integer.toString(Integer.MAX_VALUE))) { return new Long(config.get("timestamp_stop")); } String rowName = config.get("file_id") + config.get("run_id") + "_"; if (config.get("task_id") != "") { try { rowName = rowName + String.format("%04d", new Integer(config.get("task_id"))); } catch (NumberFormatException E) { rowName = rowName + config.get("task_id"); } } Get timestampGet = new Get(rowName.getBytes()); timestampGet.addColumn("d".getBytes(), "update".getBytes()); Result timestampResult = db_util.doGet(config.get("db_name_updates"), timestampGet); KeyValue tsKv = timestampResult.getColumnLatest("d".getBytes(), "update".getBytes()); if (tsKv == null) { rowName = config.get("file_id") + "_"; timestampGet = new Get(rowName.getBytes()); timestampGet.addColumn("d".getBytes(), "update".getBytes()); timestampResult = db_util.doGet(config.get("db_name_updates"), timestampGet); tsKv = timestampResult.getColumnLatest("d".getBytes(), "update".getBytes()); } if (tsKv == null) { return new Long(Integer.MAX_VALUE); } Long latestVersion = new Long(tsKv.getTimestamp()); return latestVersion; }
@Test public void testSaveJsonFormat() throws AvroBaseException, IOException { AvroBase<User, byte[]> userHAB = AvroBaseFactory.createAvroBase(new HABModule(), HAB.class, AvroFormat.JSON); User saved = new User(); saved.firstName = $("Sam"); saved.lastName = $("Pullara"); saved.birthday = $("1212"); saved.gender = GenderType.MALE; saved.email = $("*****@*****.**"); saved.description = $("CTO of RightTime, Inc. and one of the founders of BagCheck"); saved.title = $("Engineer"); saved.image = $("http://farm1.static.flickr.com/1/buddyicons/[email protected]"); saved.location = $("Los Altos, CA"); saved.mobile = $("4155551212"); saved.password = ByteBuffer.wrap($("").getBytes()); byte[] row = Bytes.toBytes("spullara"); userHAB.put(row, saved); Row<User, byte[]> loaded = userHAB.get(row); assertEquals(saved, loaded.value); HTablePool pool = new HTablePool(); HTableInterface table = pool.getTable(TABLE); try { Get get = new Get(row); byte[] DATA = Bytes.toBytes("d"); get.addColumn(COLUMN_FAMILY, DATA); Result result = table.get(get); assertTrue(Bytes.toString(result.getValue(COLUMN_FAMILY, DATA)).startsWith("{")); } finally { pool.putTable(table); } }
@SuppressWarnings("deprecation") @Override public DResult get(Get get, long startId) throws IOException { if (get.hasFamilies()) get.addFamily(DominoConst.INNER_FAMILY); get.setTimeRange(0, startId + 1); // [x, y) get.setMaxVersions(); Result preRead = region.get(get); List<KeyValue> status = preRead.getColumn(DominoConst.INNER_FAMILY, DominoConst.STATUS_COL); if (status == null || status.size() == 0) { Result ret = MVCC.handleResult(this, getTrxMetaTable(), preRead, startId, null); return new DResult(ret, null); } Integer lockId = region.getLock(null, get.getRow(), true); try { Result r = MVCC.handleResult(this, getTrxMetaTable(), region.get(get, lockId), startId, lockId); return new DResult(r, null); } catch (TransactionOutOfDateException oode) { return new DResult(null, oode.getMessage()); } catch (InvalidRowStatusException e) { return new DResult(null, e.getMessage()); } finally { region.releaseRowLock(lockId); } }
public static String get(String keyspace, String rowKey, String column, long timestamp) throws Exception { String columnValue = null; HTable htable = new HTable(keyspace); Get get = new Get(rowKey.getBytes()); get = get.setTimeStamp(timestamp); get = get.setMaxVersions(); Result res = htable.get(get); KeyValue[] data = res.raw(); for (int i = 0; i < data.length; i++) { KeyValue d = data[i]; String family = new String(data[i].getFamily()); String qualifier = new String(data[i].getQualifier()); if (qualifier.toLowerCase().equals(column.toLowerCase())) { columnValue = new String(d.getValue()); System.out.println( data[i].toString() + " Family:" + family + " Qualifier:" + qualifier + " Value:" + columnValue); break; } } return columnValue; }
public boolean fetchReadAllFieldLine(List<Line> lines, LineSender sender) throws IOException { if (null == this.rs) { throw new IllegalStateException("HBase Client try to fetch data failed ."); } for (Result result = rs.next(); result != null; result = rs.next()) { Get get = new Get(result.getRow()); for (int i = 0; i < this.families.length; i++) { get.addColumn(this.families[i].getBytes(), this.columns[i].getBytes()); } gets.add(get); if (gets.size() > this.BUFFER_LINE) { Result[] getResults = this.htable.get(gets); for (Result resu : getResults) { if (null != resu) { Line line = sender.createLine(); for (int i = 0; i < this.families.length; i++) { byte[] value = resu.getValue(this.families[i].getBytes(), this.columns[i].getBytes()); if (null == value) { line.addField(null); } else { line.addField(new String(value, encode)); } } line.addField(new String(resu.getRow(), encode)); } } return true; } } return false; }
// Retrieves the row from the table and check if it exists and has not been flagged as deleted protected Result getRow( RecordId recordId, Long version, int numberOfVersions, List<FieldType> fields) throws RecordException { Result result; Get get = new Get(recordId.toBytes()); get.setFilter(REAL_RECORDS_FILTER); try { // Add the columns for the fields to get addFieldsToGet(get, fields); if (version != null) get.setTimeRange(0, version + 1); // Only retrieve data within this timerange get.setMaxVersions(numberOfVersions); // Retrieve the data from the repository result = recordTable.get(get); if (result == null || result.isEmpty()) throw new RecordNotFoundException(recordId); } catch (IOException e) { throw new RecordException( "Exception occurred while retrieving record '" + recordId + "' from HBase table", e); } return result; }
// Retrieves the row from the table and check if it exists and has not been flagged as deleted protected Map<RecordId, Result> getRows(List<RecordId> recordIds, List<FieldType> fields) throws RecordException { Map<RecordId, Result> results = new HashMap<RecordId, Result>(); try { List<Get> gets = new ArrayList<Get>(); for (RecordId recordId : recordIds) { Get get = new Get(recordId.toBytes()); // Add the columns for the fields to get addFieldsToGet(get, fields); get.setMaxVersions(1); // Only retrieve the most recent version of each field gets.add(get); } // Retrieve the data from the repository int i = 0; for (Result result : recordTable.get(gets)) { if (result == null || result.isEmpty()) { i++; // Skip this recordId (instead of throwing a RecordNotFoundException) continue; } // Check if the record was deleted byte[] deleted = recdec.getLatest(result, RecordCf.DATA.bytes, RecordColumn.DELETED.bytes); if ((deleted == null) || (Bytes.toBoolean(deleted))) { i++; // Skip this recordId (instead of throwing a RecordNotFoundException) continue; } results.put(recordIds.get(i++), result); } } catch (IOException e) { throw new RecordException( "Exception occurred while retrieving records '" + recordIds + "' from HBase table", e); } return results; }
private static VesselTrackInfo getTrackInfo(Table TrackInfo_Table, String IMO_str) throws IOException { Get get = new Get(Bytes.toBytes(IMO_str)); get.addColumn(details, lastlocation); get.addColumn(details, firstrecordtime); get.addColumn(details, lastrecordtime); Result result = TrackInfo_Table.get(get); byte[] last_location = result.getValue(details, lastlocation); byte[] fist_recordtime = result.getValue(details, firstrecordtime); byte[] last_recordtime = result.getValue(details, lastrecordtime); VesselTrackInfo trackinfo = new VesselTrackInfo(); trackinfo.LastLocation = last_location; if (fist_recordtime != null) { trackinfo.FirstRecordTime = DateTime.parse(Bytes.toString(fist_recordtime), rawformatter).getMillis(); } if (last_recordtime != null) { trackinfo.LastRecordTime = DateTime.parse(Bytes.toString(last_recordtime), rawformatter).getMillis(); } return trackinfo; }
/** * Gets the region info and assignment for the specified region. * * @param catalogTracker * @param regionName Region to lookup. * @return Location and HRegionInfo for <code>regionName</code> * @throws IOException */ public static Pair<HRegionInfo, ServerName> getRegion( CatalogTracker catalogTracker, byte[] regionName) throws IOException { Get get = new Get(regionName); get.addFamily(HConstants.CATALOG_FAMILY); Result r = get(getCatalogHTable(catalogTracker, regionName), get); return (r == null || r.isEmpty()) ? null : parseCatalogResult(r); }
public static int getDynamicTable(Configuration config) { /** Connection to the cluster. A single connection shared by all application threads. */ Connection connection = null; /** A lightweight handle to a specific table. Used from a single thread. */ Table table = null; try { connection = ConnectionFactory.createConnection(config); table = connection.getTable(TABLE_NAME1); Get get = new Get(Bytes.toBytes("cloudera")); get.addFamily(CF); get.setMaxVersions(Integer.MAX_VALUE); Result result = table.get(get); NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result.getMap(); for (Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> columnFamilyEntry : map.entrySet()) { NavigableMap<byte[], NavigableMap<Long, byte[]>> columnMap = columnFamilyEntry.getValue(); for (Entry<byte[], NavigableMap<Long, byte[]>> columnEntry : columnMap.entrySet()) { NavigableMap<Long, byte[]> cellMap = columnEntry.getValue(); for (Entry<Long, byte[]> cellEntry : cellMap.entrySet()) { System.out.println( String.format( "Key : %s, Value :%s", Bytes.toString(columnEntry.getKey()), Bytes.toString(cellEntry.getValue()))); } } } } catch (IOException e) { e.printStackTrace(); } return 0; }
@Override public void setLogPosition(String queueId, String filename, long position) { try { byte[] rowKey = queueIdToRowKey(queueId); // Check that the log exists. addLog() must have been called before setLogPosition(). Get checkLogExists = new Get(rowKey); checkLogExists.addColumn(CF_QUEUE, Bytes.toBytes(filename)); if (!replicationTable.exists(checkLogExists)) { String errMsg = "Could not set position of non-existent log from queueId=" + queueId + ", filename=" + filename; abortable.abort(errMsg, new ReplicationException(errMsg)); return; } // Update the log offset if it exists Put walAndOffset = new Put(rowKey); walAndOffset.addColumn(CF_QUEUE, Bytes.toBytes(filename), Bytes.toBytes(position)); safeQueueUpdate(walAndOffset); } catch (IOException | ReplicationException e) { String errMsg = "Failed writing log position queueId=" + queueId + "filename=" + filename + " position=" + position; abortable.abort(errMsg, e); } }
public static void readTest(String tableStr, String row) { try { Configuration conf = HBaseConfiguration.create(); byte[] tableName = Bytes.toBytes(tableStr); HConnection hConnection = HConnectionManager.createConnection(conf); HTableInterface table = hConnection.getTable(tableName); byte[] rowkey = Bytes.toBytes(row); Get get = new Get(rowkey); get.addFamily(f0); Result result = table.get(get); NavigableMap<byte[], byte[]> m = result.getFamilyMap(f0); if (m == null || m.isEmpty()) { System.err.println("Empty." + m); return; } for (Map.Entry<byte[], byte[]> entry : m.entrySet()) { String qualifier = Bytes.toString(entry.getKey()); String value = Bytes.toString(entry.getValue()); System.out.println(qualifier + ":" + value); } table.close(); // very important } catch (IOException e) { e.printStackTrace(); } }
public void verifyInvocationResults(Integer[] selectQualifiers, Integer[] expectedQualifiers) throws Exception { Get get = new Get(ROW_BYTES); for (int i = 0; i < selectQualifiers.length; i++) { get.addColumn(FAMILY_NAME_BYTES, Bytes.toBytes(QUALIFIER_PREFIX + selectQualifiers[i])); } get.setFilter(new InvocationRecordFilter()); List<KeyValue> expectedValues = new ArrayList<KeyValue>(); for (int i = 0; i < expectedQualifiers.length; i++) { expectedValues.add( new KeyValue( ROW_BYTES, FAMILY_NAME_BYTES, Bytes.toBytes(QUALIFIER_PREFIX + expectedQualifiers[i]), expectedQualifiers[i], Bytes.toBytes(VALUE_PREFIX + expectedQualifiers[i]))); } Scan scan = new Scan(get); List<Cell> actualValues = new ArrayList<Cell>(); List<Cell> temp = new ArrayList<Cell>(); InternalScanner scanner = this.region.getScanner(scan); while (scanner.next(temp)) { actualValues.addAll(temp); temp.clear(); } actualValues.addAll(temp); Assert.assertTrue( "Actual values " + actualValues + " differ from the expected values:" + expectedValues, expectedValues.equals(actualValues)); }
@Before public void setUp() throws Exception { super.setUp(); row1 = Bytes.toBytes("row1"); row2 = Bytes.toBytes("row2"); row3 = Bytes.toBytes("row3"); fam1 = Bytes.toBytes("fam1"); fam2 = Bytes.toBytes("fam2"); col1 = Bytes.toBytes("col1"); col2 = Bytes.toBytes("col2"); col3 = Bytes.toBytes("col3"); col4 = Bytes.toBytes("col4"); col5 = Bytes.toBytes("col5"); data = Bytes.toBytes("data"); // Create Get get = new Get(row1); get.addFamily(fam1); get.addColumn(fam2, col2); get.addColumn(fam2, col4); get.addColumn(fam2, col5); this.scan = new Scan(get); rowComparator = CellComparator.COMPARATOR; }
public List<Query> getQueries(final String creator) throws IOException { if (null == creator) { return null; } List<Query> queries = new ArrayList<Query>(); HTableInterface htable = null; try { htable = HBaseConnection.get(hbaseUrl).getTable(userTableName); Get get = new Get(Bytes.toBytes(creator)); get.addFamily(Bytes.toBytes(USER_QUERY_FAMILY)); Result result = htable.get(get); Query[] query = querySerializer.deserialize( result.getValue(Bytes.toBytes(USER_QUERY_FAMILY), Bytes.toBytes(USER_QUERY_COLUMN))); if (null != query) { queries.addAll(Arrays.asList(query)); } } finally { IOUtils.closeQuietly(htable); } return queries; }
/** * Read a record from the database. Each field/value pair from the result will be stored in a * HashMap. * * @param table The name of the table * @param key The record key of the record to read. * @param fields The list of fields to read, or null for all of them * @param result A HashMap of field/value pairs for the result * @return Zero on success, a non-zero error code on error */ public Status read( String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) { // if this is a "new" table, init HTable object. Else, use existing one if (!tableName.equals(table)) { currentTable = null; try { getHTable(table); tableName = table; } catch (IOException e) { System.err.println("Error accessing HBase table: " + e); return Status.ERROR; } } Result r = null; try { if (debug) { System.out.println("Doing read from HBase columnfamily " + columnFamily); System.out.println("Doing read for key: " + key); } Get g = new Get(Bytes.toBytes(key)); if (fields == null) { g.addFamily(columnFamilyBytes); } else { for (String field : fields) { g.addColumn(columnFamilyBytes, Bytes.toBytes(field)); } } r = currentTable.get(g); } catch (IOException e) { if (debug) { System.err.println("Error doing get: " + e); } return Status.ERROR; } catch (ConcurrentModificationException e) { // do nothing for now...need to understand HBase concurrency model better return Status.ERROR; } if (r.isEmpty()) { return Status.NOT_FOUND; } while (r.advance()) { final Cell c = r.current(); result.put( Bytes.toString(CellUtil.cloneQualifier(c)), new ByteArrayByteIterator(CellUtil.cloneValue(c))); if (debug) { System.out.println( "Result for field: " + Bytes.toString(CellUtil.cloneQualifier(c)) + " is: " + Bytes.toString(CellUtil.cloneValue(c))); } } return Status.OK; }
public Result get( final TransactionState transactionState, final Get get, final boolean bool_addLocation) throws IOException { if (LOG.isTraceEnabled()) LOG.trace("Enter TransactionalTable.get"); if (bool_addLocation) addLocation(transactionState, super.getRegionLocation(get.getRow())); final String regionName = super.getRegionLocation(get.getRow()).getRegionInfo().getRegionNameAsString(); Batch.Call<TrxRegionService, GetTransactionalResponse> callable = new Batch.Call<TrxRegionService, GetTransactionalResponse>() { ServerRpcController controller = new ServerRpcController(); BlockingRpcCallback<GetTransactionalResponse> rpcCallback = new BlockingRpcCallback<GetTransactionalResponse>(); @Override public GetTransactionalResponse call(TrxRegionService instance) throws IOException { org.apache.hadoop.hbase.coprocessor.transactional.generated.TrxRegionProtos .GetTransactionalRequest.Builder builder = GetTransactionalRequest.newBuilder(); builder.setGet(ProtobufUtil.toGet(get)); builder.setTransactionId(transactionState.getTransactionId()); builder.setRegionName(ByteString.copyFromUtf8(regionName)); instance.get(controller, builder.build(), rpcCallback); return rpcCallback.get(); } }; GetTransactionalResponse result = null; try { int retryCount = 0; boolean retry = false; do { Iterator<Map.Entry<byte[], TrxRegionProtos.GetTransactionalResponse>> it = super.coprocessorService(TrxRegionService.class, get.getRow(), get.getRow(), callable) .entrySet() .iterator(); if (it.hasNext()) { result = it.next().getValue(); retry = false; } if (result == null || result.getException().contains("closing region")) { Thread.sleep(TransactionalTable.delay); retry = true; transactionState.setRetried(true); retryCount++; } } while (retryCount < TransactionalTable.retries && retry == true); } catch (Throwable e) { if (LOG.isErrorEnabled()) LOG.error("ERROR while calling getTransactional ", e); throw new IOException("ERROR while calling getTransactional ", e); } if (result == null) throw new IOException(retryErrMsg); else if (result.hasException()) throw new IOException(result.getException()); return ProtobufUtil.toResult(result.getResult()); }
@Test public void testTTL() throws Exception { TableName tableName = TableName.valueOf("testTTL"); if (TEST_UTIL.getHBaseAdmin().tableExists(tableName)) { TEST_UTIL.deleteTable(tableName); } HTableDescriptor desc = new HTableDescriptor(tableName); HColumnDescriptor hcd = new HColumnDescriptor(F).setMaxVersions(10).setTimeToLive(1); desc.addFamily(hcd); TEST_UTIL.getHBaseAdmin().createTable(desc); Table t = new HTable(new Configuration(TEST_UTIL.getConfiguration()), tableName); long now = EnvironmentEdgeManager.currentTime(); ManualEnvironmentEdge me = new ManualEnvironmentEdge(); me.setValue(now); EnvironmentEdgeManagerTestHelper.injectEdge(me); // 2s in the past long ts = now - 2000; // Set the TTL override to 3s Put p = new Put(R); p.setAttribute("ttl", new byte[] {}); p.add(F, tableName.getName(), Bytes.toBytes(3000L)); t.put(p); p = new Put(R); p.add(F, Q, ts, Q); t.put(p); p = new Put(R); p.add(F, Q, ts + 1, Q); t.put(p); // these two should be expired but for the override // (their ts was 2s in the past) Get g = new Get(R); g.setMaxVersions(10); Result r = t.get(g); // still there? assertEquals(2, r.size()); TEST_UTIL.flush(tableName); TEST_UTIL.compact(tableName, true); g = new Get(R); g.setMaxVersions(10); r = t.get(g); // still there? assertEquals(2, r.size()); // roll time forward 2s. me.setValue(now + 2000); // now verify that data eventually does expire g = new Get(R); g.setMaxVersions(10); r = t.get(g); // should be gone now assertEquals(0, r.size()); t.close(); }
private static String getVesselType(Table Vessel_Table, String IMO_str) throws IOException { Get get = new Get(Bytes.toBytes(IMO_str)); get.addColumn(ves, TYPE); Result result = Vessel_Table.get(get); byte[] type = result.getValue(ves, TYPE); return Bytes.toString(type); }
public static HBaseTuple getHBaseTuple(String row, String colFamily, String colName) throws IOException { Configuration conf = getConfiguration(); HTable table1 = new HTable(conf, "table1"); Get get = new Get(Bytes.toBytes(row)); get.addFamily(Bytes.toBytes(colFamily)); get.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(colName)); Result result = table1.get(get); return getHBaseTuple(result); }
// Get all events with exit at last location public static Map<Integer, VesselEvent> getAllEventsStartBeforeEndAfterBeforeLocation( Table VTEvent_Table, String IMO_str, VesselLocation location) throws IOException { Scan getAllEventsWithExistAtLastLocation = new Scan(); getAllEventsWithExistAtLastLocation .setStartRow( Bytes.toBytes( IMO_str + LpadNum(Long.MAX_VALUE - location.recordtime, 19) + "0000000000")) .setStopRow(Bytes.toBytes(IMO_str + LpadNum(Long.MAX_VALUE, 19) + "9999999999")) .addColumn(details, exittime); getAllEventsWithExistAtLastLocation.setCaching(100); Filter ExistTimeValuefilter = new ValueFilter( CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryComparator( Bytes.toBytes(new DateTime(location.recordtime).toString(rawformatter)))); getAllEventsWithExistAtLastLocation.setFilter(ExistTimeValuefilter); ResultScanner Result_event = VTEvent_Table.getScanner(getAllEventsWithExistAtLastLocation); Map<Integer, VesselEvent> events = new HashMap<Integer, VesselEvent>(); for (Result res : Result_event) { Get get = new Get(res.getRow()); get.addColumn(details, entrytime); get.addColumn(details, entrycoordinates); Result result = VTEvent_Table.get(get); String rowkey = Bytes.toString(result.getRow()); String polygonid = rowkey.substring(26); VesselEvent VE = new VesselEvent(); VE.exittime = location.recordtime; VE.exitcoordinates = location.coordinates; VE.destination = location.destination; VE.polygonid = Integer.parseInt(polygonid); for (Cell cell : result.rawCells()) { String Qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); String Value = Bytes.toString(CellUtil.cloneValue(cell)); if (Qualifier.equals("entertime")) { VE.entrytime = DateTime.parse(Value, rawformatter).getMillis(); } else if (Qualifier.equals("entercoordinates")) { VE.entrycoordinates = Value; } } events.put(VE.polygonid, VE); } Result_event.close(); return events; }
public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); HBaseHelper helper = HBaseHelper.getHelper(conf); helper.dropTable("testtable"); helper.createTable("testtable", "colfam1", "colfam2", "colfam3", "colfam4"); System.out.println("Adding rows to table..."); helper.fillTable("testtable", 1, 10, 2, "colfam1", "colfam2", "colfam3", "colfam4"); HTable table = new HTable(conf, "testtable"); // vv FamilyFilterExample Filter filter1 = new FamilyFilter( CompareFilter.CompareOp .LESS, // co FamilyFilterExample-1-Filter Create filter, while specifying the // comparison operator and comparator. new BinaryComparator(Bytes.toBytes("colfam3"))); Scan scan = new Scan(); scan.setFilter(filter1); ResultScanner scanner = table.getScanner( scan); // co FamilyFilterExample-2-Scan Scan over table while applying the filter. // ^^ FamilyFilterExample System.out.println("Scanning table... "); // vv FamilyFilterExample for (Result result : scanner) { System.out.println(result); } scanner.close(); Get get1 = new Get(Bytes.toBytes("row-5")); get1.setFilter(filter1); Result result1 = table.get(get1); // co FamilyFilterExample-3-Get Get a row while applying the same filter. System.out.println("Result of get(): " + result1); Filter filter2 = new FamilyFilter( CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("colfam3"))); Get get2 = new Get( Bytes.toBytes( "row-5")); // co FamilyFilterExample-4-Mismatch Create a filter on one column family // while trying to retrieve another. get2.addFamily(Bytes.toBytes("colfam1")); get2.setFilter(filter2); Result result2 = table.get( get2); // co FamilyFilterExample-5-Get2 Get the same row while applying the new filter, // this will return "NONE". System.out.println("Result of get(): " + result2); // ^^ FamilyFilterExample }
private void addFieldsToGet(Get get, List<FieldType> fields) { if (fields != null && (!fields.isEmpty())) { for (FieldType field : fields) { get.addColumn(RecordCf.DATA.bytes, ((FieldTypeImpl) field).getQualifier()); } RecordDecoder.addSystemColumnsToGet(get); } else { // Retrieve everything get.addFamily(RecordCf.DATA.bytes); } }
@Override public List<SpanBo> selectSpans(TransactionId transactionId) { if (transactionId == null) { throw new NullPointerException("transactionId must not be null"); } byte[] transactionIdRowKey = rowKeyDecoder.encodeRowKey(transactionId); Get get = new Get(transactionIdRowKey); get.addFamily(HBaseTables.TRACES_CF_SPAN); get.addFamily(HBaseTables.TRACES_CF_TERMINALSPAN); return template2.get(HBaseTables.TRACES, get, spanMapper); }
private static void assertGet(byte[] row, byte[] familiy, byte[] qualifier, byte[] value) throws IOException { // run a get and see if the value matches Get get = new Get(row); get.addColumn(familiy, qualifier); Result result = region.get(get, null); assertEquals(1, result.size()); KeyValue kv = result.raw()[0]; byte[] r = kv.getValue(); assertTrue(Bytes.compareTo(r, value) == 0); }
public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); HBaseHelper helper = HBaseHelper.getHelper(conf); helper.dropTable("testtable"); helper.createTable("testtable", "colfam1", "colfam2"); System.out.println("Adding rows to table..."); helper.fillTable("testtable", 1, 10, 10, "colfam1", "colfam2"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf("testtable")); // vv SingleColumnValueFilterExample SingleColumnValueFilter filter = new SingleColumnValueFilter( Bytes.toBytes("colfam1"), Bytes.toBytes("col-5"), CompareFilter.CompareOp.NOT_EQUAL, new SubstringComparator("val-5")); filter.setFilterIfMissing(true); Scan scan = new Scan(); scan.setFilter(filter); ResultScanner scanner = table.getScanner(scan); // ^^ SingleColumnValueFilterExample System.out.println("Results of scan:"); // vv SingleColumnValueFilterExample for (Result result : scanner) { for (Cell cell : result.rawCells()) { System.out.println( "Cell: " + cell + ", Value: " + Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } } scanner.close(); Get get = new Get(Bytes.toBytes("row-6")); get.setFilter(filter); Result result = table.get(get); System.out.println("Result of get: "); for (Cell cell : result.rawCells()) { System.out.println( "Cell: " + cell + ", Value: " + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } // ^^ SingleColumnValueFilterExample }
@Override public List<SqlMetaDataBo> getSqlMetaData(String agentId, long time, int hashCode) { if (agentId == null) { throw new NullPointerException("agentId must not be null"); } SqlMetaDataBo sqlMetaData = new SqlMetaDataBo(agentId, time, hashCode); byte[] sqlId = getDistributedKey(sqlMetaData.toRowKey()); Get get = new Get(sqlId); get.addFamily(HBaseTables.SQL_METADATA_CF_SQL); return hbaseOperations2.get(HBaseTables.SQL_METADATA, get, sqlMetaDataMapper); }
/** * HBaseのsc_item_dataから、商品データを取得。 * * @param shopID 店舗ID * @param itemID 商品ID。 * @param families column families * @return 該当商品データ */ public ItemData get(int shopID, int itemID, String[] families) throws IOException { byte[] rowkey = encodeRowkey(shopID, itemID); if (rowkey == null || rowkey.length == 0) { return null; } Get g = new Get(rowkey); for (String family : families) { byte[] bFamily = Bytes.toBytes(family); g.addFamily(bFamily); } g.setMaxVersions(); Result r = table.get(g); return toDataStore(r); }
@TimeDepend @Test public void testGetWith_Ts() throws Exception { recreateTable(); fillData(); Get get = new Get(rowKey_ForTest); get.addColumn(ColumnFamilyNameBytes, QName1); get.setMaxVersions(3); get.setTimeStamp(3L); Result result = table.get(get); Assert.assertEquals(1, result.raw().length); get.setTimeStamp(2L); result = table.get(get); Assert.assertEquals(1, result.raw().length); get.setTimeStamp(1L); result = table.get(get); Assert.assertEquals(1, result.raw().length); get.setTimeStamp(0L); result = table.get(get); Assert.assertEquals(0, result.raw().length); get.setTimeRange(1, 4); result = table.get(get); Assert.assertEquals(3, result.raw().length); recreateTable(); }
/* * 查询表中的某一列 * * @tableName 表名 * * @rowKey rowKey */ public static void getResultByColumn( String tableName, String rowKey, String familyName, String columnName) throws IOException { HTable table = new HTable(conf, Bytes.toBytes(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName)); // 获取指定列族和列修饰符对应的列 Result result = table.get(get); for (KeyValue kv : result.list()) { System.out.println("family:" + Bytes.toString(kv.getFamily())); System.out.println("qualifier:" + Bytes.toString(kv.getQualifier())); System.out.println("value:" + Bytes.toString(kv.getValue())); System.out.println("Timestamp:" + kv.getTimestamp()); System.out.println("-------------------------------------------"); } }