Java Code Examples for org.apache.hadoop.hbase.util.Bytes.toLong()

The following are Jave code examples for showing how to use toLong() of the org.apache.hadoop.hbase.util.Bytes class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: ditb   File: CellCodec.java   View Source Code Vote up 7 votes
@Override
protected Cell parseCell() throws IOException {
  byte [] row = readByteArray(this.in);
  byte [] family = readByteArray(in);
  byte [] qualifier = readByteArray(in);
  byte [] longArray = new byte[Bytes.SIZEOF_LONG];
  IOUtils.readFully(this.in, longArray);
  long timestamp = Bytes.toLong(longArray);
  byte type = (byte) this.in.read();
  byte[] value = readByteArray(in);
  // Read memstore version
  byte[] memstoreTSArray = new byte[Bytes.SIZEOF_LONG];
  IOUtils.readFully(this.in, memstoreTSArray);
  long memstoreTS = Bytes.toLong(memstoreTSArray);
  return CellUtil.createCell(row, family, qualifier, timestamp, type, value, memstoreTS);
}
 
Example 2
Project: stroom-stats   File: StatEventKey.java   View Source Code Vote up 7 votes
public static StatEventKey fromBytes(final byte[] bytes) {
    UID statName = UID.from(bytes, STAT_NAME_PART_OFFSET);
    RollUpBitMask rollUpBitMask = RollUpBitMask.fromBytes(bytes, ROLLUP_MASK_PART_OFFSET);
    EventStoreTimeIntervalEnum interval = EventStoreTimeIntervalEnum.fromBytes(bytes, INTERVAL_PART_OFFSET);
    long timeMs = Bytes.toLong(bytes, TIME_PART_OFFSET);
    List<TagValue> tagValues = getTagValues(bytes);

    try {
        //de-serializing so leave time as in its byte form
        StatEventKey statEventKey = new StatEventKey(statName, rollUpBitMask, interval, timeMs, tagValues, TimeTruncation.DONT_TRUNCATE);
        LOGGER.trace(() -> String.format("De-serializing bytes %s to StatEventKey %s", ByteArrayUtils.byteArrayToHex(bytes), statEventKey));
        return statEventKey;
    } catch (Exception e) {
        LOGGER.error("Error de-serializing a StatEventKey from bytes {}", ByteArrayUtils.byteArrayToHex(bytes));
        throw e;
    }
}
 
Example 3
Project: ditb   File: TPCHWorkload.java   View Source Code Vote up 7 votes
@Override public String parseResult(Result result) {
  byte[] rowkey = result.getRow();
  // aId, uId, cId
  long orderKey = Bytes.toLong(rowkey, 4);
  long custKey = Bytes.toLong(result.getValue(FAMILY_NAME, Bytes.toBytes("ck")));
  String status = Bytes.toString(result.getValue(FAMILY_NAME, Bytes.toBytes("st")));
  int totalPrice = Bytes.toInt(result.getValue(FAMILY_NAME, Bytes.toBytes("t")));
  int date = Bytes.toInt(result.getValue(FAMILY_NAME, Bytes.toBytes("d")));
  int priority = Bytes.toInt(result.getValue(FAMILY_NAME, Bytes.toBytes("p")));
  String clerk = Bytes.toString(result.getValue(FAMILY_NAME, Bytes.toBytes("cl")));
  int shipPriority = Bytes.toInt(result.getValue(FAMILY_NAME, Bytes.toBytes("sh")));
  String comment = Bytes.toString(result.getValue(FAMILY_NAME, Bytes.toBytes("cm")));
  StringBuilder sb = new StringBuilder();
  sb.append("{");
  sb.append("orderKey=").append(orderKey).append(",");
  sb.append("custKey=").append(custKey).append(",");
  sb.append("status=").append(status).append(",");
  sb.append("totalPrice=").append(totalPrice).append(",");
  sb.append("date=").append(date).append(",");
  sb.append("priority=").append(priority).append(",");
  sb.append("clerk=").append(clerk).append(",");
  sb.append("shipPriority=").append(shipPriority).append(",");
  sb.append("comment=").append(comment).append("}");
  return sb.toString();
}
 
Example 4
Project: ditb   File: HFileReaderV3.java   View Source Code Vote up 7 votes
protected final void readKeyValueLen() {
  // TODO: METHOD (mostly) DUPLICATED IN V2!!!! FIXED in master branch by collapsing v3 and v2.
  // This is a hot method. We go out of our way to make this method short so it can be
  // inlined and is not too big to compile. We also manage position in ByteBuffer ourselves
  // because it is faster than going via range-checked ByteBuffer methods or going through a
  // byte buffer array a byte at a time.
  int p = blockBuffer.position() + blockBuffer.arrayOffset();
  // Get a long at a time rather than read two individual ints. In micro-benchmarking, even
  // with the extra bit-fiddling, this is order-of-magnitude faster than getting two ints.
  long ll = Bytes.toLong(blockBuffer.array(), p);
  // Read top half as an int of key length and bottom int as value length
  this.currKeyLen = (int)(ll >> Integer.SIZE);
  this.currValueLen = (int)(Bytes.MASK_FOR_LOWER_INT_IN_LONG ^ ll);
  checkKeyValueLen();
  // Move position past the key and value lengths and then beyond the key and value
  p += (Bytes.SIZEOF_LONG + currKeyLen + currValueLen);
  if (reader.hfileContext.isIncludesTags()) {
    // Tags length is a short.
    this.currTagsLen = Bytes.toShort(blockBuffer.array(), p);
    checkTagsLen();
    p += (Bytes.SIZEOF_SHORT + currTagsLen);
  }
  readMvccVersion(p);
}
 
Example 5
Project: ditb   File: KeyValue.java   View Source Code Vote up 7 votes
/**
 * Use for logging.
 * @param b Key portion of a KeyValue.
 * @param o Offset to start of key
 * @param l Length of key.
 * @return Key as a String.
 */
public static String keyToString(final byte [] b, final int o, final int l) {
  if (b == null) return "";
  int rowlength = Bytes.toShort(b, o);
  String row = Bytes.toStringBinary(b, o + Bytes.SIZEOF_SHORT, rowlength);
  int columnoffset = o + Bytes.SIZEOF_SHORT + 1 + rowlength;
  int familylength = b[columnoffset - 1];
  int columnlength = l - ((columnoffset - o) + TIMESTAMP_TYPE_SIZE);
  String family = familylength == 0? "":
    Bytes.toStringBinary(b, columnoffset, familylength);
  String qualifier = columnlength == 0? "":
    Bytes.toStringBinary(b, columnoffset + familylength,
    columnlength - familylength);
  long timestamp = Bytes.toLong(b, o + (l - TIMESTAMP_TYPE_SIZE));
  String timestampStr = humanReadableTimestamp(timestamp);
  byte type = b[o + l - 1];
  return row + "/" + family +
    (family != null && family.length() > 0? ":" :"") +
    qualifier + "/" + timestampStr + "/" + Type.codeToType(type);
}
 
Example 6
Project: ditb   File: ImportTsv.java   View Source Code Vote up 6 votes
public long getCellTTL() {
  if (!hasCellTTL()) {
    return 0;
  } else {
    return Bytes.toLong(lineBytes, getColumnOffset(cellTTLColumnIndex),
        getColumnLength(cellTTLColumnIndex));
  }
}
 
Example 7
Project: ditb   File: TestAtomicOperation.java   View Source Code Vote up 6 votes
private void assertICV(byte [] row,
                       byte [] familiy,
                       byte[] qualifier,
                       long amount) throws IOException {
  // run a get and see?
  Get get = new Get(row);
  get.addColumn(familiy, qualifier);
  Result result = region.get(get);
  assertEquals(1, result.size());

  Cell kv = result.rawCells()[0];
  long r = Bytes.toLong(CellUtil.cloneValue(kv));
  assertEquals(amount, r);
}
 
Example 8
Project: ditb   File: IntegrationTestBulkLoad.java   View Source Code Vote up 6 votes
@Override
protected void map(ImmutableBytesWritable key, Result value, Context context)
    throws IOException, InterruptedException {
  long longRk = Bytes.toLong(value.getRow());

  for (Map.Entry<byte[], byte[]> entry : value.getFamilyMap(CHAIN_FAM).entrySet()) {
    long chainId = Bytes.toLong(entry.getKey());
    long next = Bytes.toLong(entry.getValue());
    Cell c = value.getColumnCells(SORT_FAM, entry.getKey()).get(0);
    long order = Bytes.toLong(CellUtil.cloneValue(c));
    context.write(new LinkKey(chainId, order), new LinkChain(longRk, next));
  }
}
 
Example 9
Project: ditb   File: KeyValue.java   View Source Code Vote up 5 votes
@Override
public long getTimestamp() {
  int tsOffset = getTimestampOffset();
  return Bytes.toLong(this.bytes, tsOffset);
}
 
Example 10
Project: ditb   File: StoreFile.java   View Source Code Vote up 5 votes
/**
 * Return the timestamp at which this bulk load file was generated.
 */
public long getBulkLoadTimestamp() {
  byte[] bulkLoadTimestamp = metadataMap.get(BULKLOAD_TIME_KEY);
  return (bulkLoadTimestamp == null) ? 0 : Bytes.toLong(bulkLoadTimestamp);
}
 
Example 11
Project: ditb   File: LongColumnInterpreter.java   View Source Code Vote up 5 votes
public Long getValue(byte[] colFamily, byte[] colQualifier, Cell kv)
    throws IOException {
  if (kv == null || kv.getValueLength() != Bytes.SIZEOF_LONG)
    return null;
  return Bytes.toLong(kv.getValueArray(), kv.getValueOffset());
}
 
Example 12
Project: ditb   File: Compactor.java   View Source Code Vote up 5 votes
/**
 * Extracts some details about the files to compact that are commonly needed by compactors.
 *
 * @param filesToCompact Files.
 * @param allFiles       Whether all files are included for compaction
 * @return The result.
 */
protected FileDetails getFileDetails(Collection<StoreFile> filesToCompact, boolean allFiles)
    throws IOException {
  FileDetails fd = new FileDetails();
  long oldestHFileTimeStampToKeepMVCC =
      System.currentTimeMillis() - (1000L * 60 * 60 * 24 * this.keepSeqIdPeriod);

  for (StoreFile file : filesToCompact) {
    if (allFiles && (file.getModificationTimeStamp() < oldestHFileTimeStampToKeepMVCC)) {
      // when isAllFiles is true, all files are compacted so we can calculate the smallest
      // MVCC value to keep
      if (fd.minSeqIdToKeep < file.getMaxMemstoreTS()) {
        fd.minSeqIdToKeep = file.getMaxMemstoreTS();
      }
    }
    long seqNum = file.getMaxSequenceId();
    fd.maxSeqId = Math.max(fd.maxSeqId, seqNum);
    StoreFile.Reader r = file.getReader();
    if (r == null) {
      LOG.warn("Null reader for " + file.getPath());
      continue;
    }
    // NOTE: use getEntries when compacting instead of getFilterEntries, otherwise under-sized
    // blooms can cause progress to be miscalculated or if the user switches bloom
    // type (e.g. from ROW to ROWCOL)
    long keyCount = r.getEntries();
    fd.maxKeyCount += keyCount;
    // calculate the latest MVCC readpoint in any of the involved store files
    Map<byte[], byte[]> fileInfo = r.loadFileInfo();
    byte tmp[] = null;
    // Get and set the real MVCCReadpoint for bulk loaded files, which is the
    // SeqId number.
    if (r.isBulkLoaded()) {
      fd.maxMVCCReadpoint = Math.max(fd.maxMVCCReadpoint, r.getSequenceID());
    } else {
      tmp = fileInfo.get(HFileWriterV2.MAX_MEMSTORE_TS_KEY);
      if (tmp != null) {
        fd.maxMVCCReadpoint = Math.max(fd.maxMVCCReadpoint, Bytes.toLong(tmp));
      }
    }
    tmp = fileInfo.get(FileInfo.MAX_TAGS_LEN);
    if (tmp != null) {
      fd.maxTagsLength = Math.max(fd.maxTagsLength, Bytes.toInt(tmp));
    }
    // If required, calculate the earliest put timestamp of all involved storefiles.
    // This is used to remove family delete marker during compaction.
    long earliestPutTs = 0;
    if (allFiles) {
      tmp = fileInfo.get(StoreFile.EARLIEST_PUT_TS);
      if (tmp == null) {
        // There's a file with no information, must be an old one
        // assume we have very old puts
        fd.earliestPutTs = earliestPutTs = HConstants.OLDEST_TIMESTAMP;
      } else {
        earliestPutTs = Bytes.toLong(tmp);
        fd.earliestPutTs = Math.min(fd.earliestPutTs, earliestPutTs);
      }
    }
    if (LOG.isDebugEnabled()) {
      LOG.debug("Compacting " + file + ", keycount=" + keyCount + ", bloomtype=" + r
          .getBloomFilterType().toString() + ", size=" + TraditionalBinaryPrefix
          .long2String(r.length(), "", 1) + ", encoding=" + r.getHFileReader()
          .getDataBlockEncoding() + ", seqNum=" + seqNum + (allFiles ?
          ", earliestPutTs=" + earliestPutTs :
          ""));
    }
  }
  return fd;
}
 
Example 13
Project: ditb   File: LongComparator.java   View Source Code Vote up 5 votes
@Override
public int compareTo(byte[] value, int offset, int length) {
  Long that = Bytes.toLong(value, offset, length);
  return this.longValue.compareTo(that);
}
 
Example 14
Project: ditb   File: RawLong.java   View Source Code Vote up 5 votes
@Override
public Long decode(PositionedByteRange src) {
  long val = Bytes.toLong(src.getBytes(), src.getOffset() + src.getPosition());
  skip(src);
  return val;
}
 
Example 15
Project: ditb   File: IntegrationTestBigLinkedList.java   View Source Code Vote up 5 votes
/**
 * Dump out extra info around references if there are any. Helps debugging.
 * @return StringBuilder filled with references if any.
 * @throws IOException
 */
private StringBuilder dumpExtraInfoOnRefs(final BytesWritable key, final Context context,
    final List<byte []> refs)
throws IOException {
  StringBuilder refsSb = null;
  if (refs.isEmpty()) return refsSb;
  refsSb = new StringBuilder();
  String comma = "";
  // If a row is a reference but has no define, print the content of the row that has
  // this row as a 'prev'; it will help debug.  The missing row was written just before
  // the row we are dumping out here.
  TableName tn = getTableName(context.getConfiguration());
  try (Table t = this.connection.getTable(tn)) {
    for (byte [] ref : refs) {
      Result r = t.get(new Get(ref));
      List<Cell> cells = r.listCells();
      String ts = (cells != null && !cells.isEmpty())?
          new java.util.Date(cells.get(0).getTimestamp()).toString(): "";
      byte [] b = r.getValue(FAMILY_NAME, COLUMN_CLIENT);
      String jobStr = (b != null && b.length > 0)? Bytes.toString(b): "";
      b = r.getValue(FAMILY_NAME, COLUMN_COUNT);
      long count = (b != null && b.length > 0)? Bytes.toLong(b): -1;
      b = r.getValue(FAMILY_NAME, COLUMN_PREV);
      String refRegionLocation = "";
      String keyRegionLocation = "";
      if (b != null && b.length > 0) {
        try (RegionLocator rl = this.connection.getRegionLocator(tn)) {
          HRegionLocation hrl = rl.getRegionLocation(b);
          if (hrl != null) refRegionLocation = hrl.toString();
          // Key here probably has trailing zeros on it.
          hrl = rl.getRegionLocation(key.getBytes());
          if (hrl != null) keyRegionLocation = hrl.toString();
        }
      }
      LOG.error("Extras on ref without a def, ref=" + Bytes.toStringBinary(ref) +
        ", refPrevEqualsKey=" +
          (Bytes.compareTo(key.getBytes(), 0, key.getLength(), b, 0, b.length) == 0) +
          ", key=" + Bytes.toStringBinary(key.getBytes(), 0, key.getLength()) +
          ", ref row date=" + ts + ", jobStr=" + jobStr +
          ", ref row count=" + count +
          ", ref row regionLocation=" + refRegionLocation +
          ", key row regionLocation=" + keyRegionLocation);
      refsSb.append(comma);
      comma = ",";
      refsSb.append(Bytes.toStringBinary(ref));
    }
  }
  return refsSb;
}
 
Example 16
Project: ditb   File: KeyValue.java   View Source Code Vote up 5 votes
/**
 * @param keylength Pass if you have it to save on a int creation.
 * @return Timestamp
 */
long getTimestamp(final int keylength) {
  int tsOffset = getTimestampOffset(keylength);
  return Bytes.toLong(this.bytes, tsOffset);
}
 
Example 17
Project: ditb   File: HRegionInfo.java   View Source Code Vote up 4 votes
/**
 * The latest seqnum that the server writing to meta observed when opening the region.
 * E.g. the seqNum when the result of {@link #getServerName(Result)} was written.
 * @param r Result to pull the seqNum from
 * @return SeqNum, or HConstants.NO_SEQNUM if there's no value written.
 * @deprecated use MetaTableAccessor methods for interacting with meta layouts
 */
@Deprecated
public static long getSeqNumDuringOpen(final Result r) {
  Cell cell = r.getColumnLatestCell(HConstants.CATALOG_FAMILY, HConstants.SEQNUM_QUALIFIER);
  if (cell == null || cell.getValueLength() == 0) return HConstants.NO_SEQNUM;
  return Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
}
 
Example 18
Project: hbase-client   File: HResultUtils.java   View Source Code Vote up 3 votes
/**
 * Read row ID as long(little-endian).
 *
 * @param result
 * @return
 */
public static long rowIdAsLong( Result result ) {
    return Bytes.toLong(result.getRow());
}
 
Example 19
Project: ditb   File: TimeRange.java   View Source Code Vote up 3 votes
/**
 * Represents interval [minStamp, maxStamp)
 * @param minStamp the minimum timestamp, inclusive
 * @param maxStamp the maximum timestamp, exclusive
 * @throws IOException
 * @deprecated This is removed in the 2.0 line and above
 */
@Deprecated
public TimeRange(byte [] minStamp, byte [] maxStamp)
throws IOException {
  this(Bytes.toLong(minStamp), Bytes.toLong(maxStamp));
}
 
Example 20
Project: ditb   File: MetaTableAccessor.java   View Source Code Vote up 3 votes
/**
 * The latest seqnum that the server writing to meta observed when opening the region.
 * E.g. the seqNum when the result of {@link #getServerName(Result, int)} was written.
 * @param r Result to pull the seqNum from
 * @return SeqNum, or HConstants.NO_SEQNUM if there's no value written.
 */
private static long getSeqNumDuringOpen(final Result r, final int replicaId) {
  Cell cell = r.getColumnLatestCell(getFamily(), getSeqNumColumn(replicaId));
  if (cell == null || cell.getValueLength() == 0) return HConstants.NO_SEQNUM;
  return Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
}