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

The following are Jave code examples for showing how to use toStringBinary() 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: CellUtil.java   View Source Code Vote up 6 votes
/**
 * @param cell
 * @return The Key portion of the passed <code>cell</code> as a String.
 */
public static String getCellKeyAsString(Cell cell) {
  StringBuilder sb = new StringBuilder(Bytes.toStringBinary(
    cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()));
  sb.append('/');
  sb.append(cell.getFamilyLength() == 0? "":
    Bytes.toStringBinary(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength()));
  // KeyValue only added ':' if family is non-null.  Do same.
  if (cell.getFamilyLength() > 0) sb.append(':');
  sb.append(cell.getQualifierLength() == 0? "":
    Bytes.toStringBinary(cell.getQualifierArray(), cell.getQualifierOffset(),
      cell.getQualifierLength()));
  sb.append('/');
  sb.append(KeyValue.humanReadableTimestamp(cell.getTimestamp()));
  sb.append('/');
  sb.append(Type.codeToType(cell.getTypeByte()));
  sb.append("/vlen=");
  sb.append(cell.getValueLength());
  sb.append("/seqid=");
  sb.append(cell.getSequenceId());
  return sb.toString();
}
 
Example 2
Project: ditb   File: HFileBlock.java   View Source Code Vote up 6 votes
/**
 * Called after reading a block with provided onDiskSizeWithHeader.
 */
private static void validateOnDiskSizeWithoutHeader(int expectedOnDiskSizeWithoutHeader,
    int actualOnDiskSizeWithoutHeader, ByteBuffer buf, long offset) throws IOException {
  if (actualOnDiskSizeWithoutHeader != expectedOnDiskSizeWithoutHeader) {
    // We make the read-only copy here instead of when passing the parameter to this function
    // to make duplicates only in failure cases, instead of every single time.
    ByteBuffer bufReadOnly = buf.asReadOnlyBuffer();
    String dataBegin = null;
    byte[] dataBeginBytes = new byte[Math.min(32, bufReadOnly.limit() - bufReadOnly.position())];
    bufReadOnly.get(dataBeginBytes);
    dataBegin = Bytes.toStringBinary(dataBeginBytes);
    String blockInfoMsg =
      "Block offset: " + offset + ", data starts with: " + dataBegin;
    throw new IOException("On-disk size without header provided is "
        + expectedOnDiskSizeWithoutHeader + ", but block "
        + "header contains " + actualOnDiskSizeWithoutHeader + ". " +
        blockInfoMsg);
  }
}
 
Example 3
Project: ditb   File: ReversedScannerCallable.java   View Source Code Vote up 5 votes
/**
 * Get the corresponding regions for an arbitrary range of keys.
 * @param startKey Starting row in range, inclusive
 * @param endKey Ending row in range, exclusive
 * @param reload force reload of server location
 * @return A list of HRegionLocation corresponding to the regions that contain
 *         the specified range
 * @throws IOException
 */
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value="NP_NULL_ON_SOME_PATH",
    justification="I thought I'd fixed it but FB still complains; see below")
private List<HRegionLocation> locateRegionsInRange(byte[] startKey,
    byte[] endKey, boolean reload) throws IOException {
  final boolean endKeyIsEndOfTable = Bytes.equals(endKey,
      HConstants.EMPTY_END_ROW);
  if ((Bytes.compareTo(startKey, endKey) > 0) && !endKeyIsEndOfTable) {
    throw new IllegalArgumentException("Invalid range: "
        + Bytes.toStringBinary(startKey) + " > "
        + Bytes.toStringBinary(endKey));
  }
  List<HRegionLocation> regionList = new ArrayList<HRegionLocation>();
  byte[] currentKey = startKey;
  do {
    RegionLocations rl = RpcRetryingCallerWithReadReplicas.getRegionLocations(reload, id,
        getConnection(), tableName, currentKey);
    HRegionLocation regionLocation = id < rl.size() ? rl.getRegionLocation(id) : null;
    if (regionLocation != null && regionLocation.getRegionInfo().containsRow(currentKey)) {
      regionList.add(regionLocation);
    } else {
      // FindBugs: NP_NULL_ON_SOME_PATH Complaining about regionLocation
      throw new DoNotRetryIOException("Does hbase:meta exist hole? Locating row "
          + Bytes.toStringBinary(currentKey) + " returns incorrect region "
          + (regionLocation != null? regionLocation.getRegionInfo(): null));
    }
    currentKey = regionLocation.getRegionInfo().getEndKey();
  } while (!Bytes.equals(currentKey, HConstants.EMPTY_END_ROW)
      && (endKeyIsEndOfTable || Bytes.compareTo(currentKey, endKey) < 0));
  return regionList;
}
 
Example 4
Project: ditb   File: Increment.java   View Source Code Vote up 5 votes
/**
 * Add the specified KeyValue to this operation.
 * @param cell individual Cell
 * @return this
 * @throws java.io.IOException e
 */
public Increment add(Cell cell) throws IOException{
  byte [] family = CellUtil.cloneFamily(cell);
  List<Cell> list = getCellList(family);
  //Checking that the row of the kv is the same as the put
  int res = Bytes.compareTo(this.row, 0, row.length,
      cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
  if (res != 0) {
    throw new WrongRowIOException("The row in " + cell +
      " doesn't match the original one " +  Bytes.toStringBinary(this.row));
  }
  list.add(cell);
  familyMap.put(family, list);
  return this;
}
 
Example 5
Project: QDrill   File: HBaseSubScan.java   View Source Code Vote up 5 votes
@Override
public String toString() {
  return "HBaseScanSpec [tableName=" + tableName
      + ", startRow=" + (startRow == null ? null : Bytes.toStringBinary(startRow))
      + ", stopRow=" + (stopRow == null ? null : Bytes.toStringBinary(stopRow))
      + ", filter=" + (getScanFilter() == null ? null : getScanFilter().toString())
      + ", regionServer=" + regionServer + "]";
}
 
Example 6
Project: ditb   File: TestHFileBlockCompatibility.java   View Source Code Vote up 5 votes
private String createTestBlockStr(Compression.Algorithm algo,
    int correctLength) throws IOException {
  Writer hbw = createTestV2Block(algo);
  byte[] testV2Block = hbw.getHeaderAndData();
  int osOffset = HConstants.HFILEBLOCK_HEADER_SIZE_NO_CHECKSUM + 9;
  if (testV2Block.length == correctLength) {
    // Force-set the "OS" field of the gzip header to 3 (Unix) to avoid
    // variations across operating systems.
    // See http://www.gzip.org/zlib/rfc-gzip.html for gzip format.
    testV2Block[osOffset] = 3;
  }
  return Bytes.toStringBinary(testV2Block);
}
 
Example 7
Project: dremio-oss   File: HBaseSubScan.java   View Source Code Vote up 5 votes
@Override
public String toString() {
  return "HBaseScanSpec [tableName=" + tableName
      + ", startRow=" + (startRow == null ? null : Bytes.toStringBinary(startRow))
      + ", stopRow=" + (stopRow == null ? null : Bytes.toStringBinary(stopRow))
      + ", filter=" + (getScanFilter() == null ? null : getScanFilter().toString())
      + ", regionServer=" + regionServer + "]";
}
 
Example 8
Project: aliyun-tablestore-hbase-client   File: OUpdate.java   View Source Code Vote up 5 votes
private void internalAdd(OMutation m) throws IOException {
    int res = Bytes.compareTo(this.row, m.getRow());
    if (res != 0) {
        throw new WrongRowIOException("The row in the recently added Put/Delete <" +
                Bytes.toStringBinary(m.getRow()) + "> doesn't match the original one <" +
                Bytes.toStringBinary(this.row) + ">");
    }
    this.keyValues.addAll(m.getKeyValues());
}
 
Example 9
Project: ditb   File: TestSeekOptimizations.java   View Source Code Vote up 5 votes
private List<Cell> filterExpectedResults(Set<String> qualSet,
    byte[] startRow, byte[] endRow, int maxVersions) {
  final List<Cell> filteredKVs = new ArrayList<Cell>();
  final Map<String, Integer> verCount = new HashMap<String, Integer>();
  for (Cell kv : expectedKVs) {
    if (startRow.length > 0 &&
        Bytes.compareTo(kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(),
            startRow, 0, startRow.length) < 0) {
      continue;
    }

    // In this unit test the end row is always inclusive.
    if (endRow.length > 0 &&
        Bytes.compareTo(kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(),
            endRow, 0, endRow.length) > 0) {
      continue;
    }

    if (!qualSet.isEmpty() && (!CellUtil.matchingFamily(kv, FAMILY_BYTES)
        || !qualSet.contains(Bytes.toString(CellUtil.cloneQualifier(kv))))) {
      continue;
    }

    final String rowColStr =
      Bytes.toStringBinary(CellUtil.cloneRow(kv)) + "/"
          + Bytes.toStringBinary(CellUtil.cloneFamily(kv)) + ":"
          + Bytes.toStringBinary(CellUtil.cloneQualifier(kv));
    final Integer curNumVer = verCount.get(rowColStr);
    final int newNumVer = curNumVer != null ? (curNumVer + 1) : 1;
    if (newNumVer <= maxVersions) {
      filteredKVs.add(kv);
      verCount.put(rowColStr, newNumVer);
    }
  }

  return filteredKVs;
}
 
Example 10
Project: ditb   File: ThriftServerRunner.java   View Source Code Vote up 5 votes
/**
 * Adds all the attributes into the Operation object
 */
private static void addAttributes(OperationWithAttributes op,
  Map<ByteBuffer, ByteBuffer> attributes) {
  if (attributes == null || attributes.size() == 0) {
    return;
  }
  for (Map.Entry<ByteBuffer, ByteBuffer> entry : attributes.entrySet()) {
    String name = Bytes.toStringBinary(getBytes(entry.getKey()));
    byte[] value =  getBytes(entry.getValue());
    op.setAttribute(name, value);
  }
}
 
Example 11
Project: ditb   File: HBaseAdmin.java   View Source Code Vote up 5 votes
/**
 * {@inheritDoc}
 */
@Override
public void flushRegion(final byte[] regionName) throws IOException {
  Pair<HRegionInfo, ServerName> regionServerPair = getRegion(regionName);
  if (regionServerPair == null) {
    throw new IllegalArgumentException("Unknown regionname: " + Bytes.toStringBinary(regionName));
  }
  if (regionServerPair.getSecond() == null) {
    throw new NoServerForRegionException(Bytes.toStringBinary(regionName));
  }
  flush(regionServerPair.getSecond(), regionServerPair.getFirst());
}
 
Example 12
Project: ditb   File: MultiTableInputFormatTestBase.java   View Source Code Vote up 5 votes
protected void makeAssertions(ImmutableBytesWritable key,
    Iterable<ImmutableBytesWritable> values) {
  int count = 0;
  for (ImmutableBytesWritable value : values) {
    String val = Bytes.toStringBinary(value.get());
    LOG.debug("reduce: key[" + count + "] -> " +
        Bytes.toStringBinary(key.get()) + ", value -> " + val);
    if (first == null) first = val;
    last = val;
    count++;
  }
  assertEquals(3, count);
}
 
Example 13
Project: ditb   File: MetricsConnection.java   View Source Code Vote up 5 votes
public void updateServerStats(ServerName serverName, byte[] regionName,
                              Object r) {
  if (!(r instanceof Result)) {
    return;
  }
  Result result = (Result) r;
  ClientProtos.RegionLoadStats stats = result.getStats();
  if(stats == null){
    return;
  }
  String name = serverName.getServerName() + "," + Bytes.toStringBinary(regionName);
  ConcurrentMap<byte[], RegionStats> rsStats = null;
  if (serverStats.containsKey(serverName)) {
    rsStats = serverStats.get(serverName);
  } else {
    rsStats = serverStats.putIfAbsent(serverName,
        new ConcurrentSkipListMap<byte[], RegionStats>(Bytes.BYTES_COMPARATOR));
    if (rsStats == null) {
      rsStats = serverStats.get(serverName);
    }
  }
  RegionStats regionStats = null;
  if (rsStats.containsKey(regionName)) {
    regionStats = rsStats.get(regionName);
  } else {
    regionStats = rsStats.putIfAbsent(regionName, new RegionStats(this.registry, name));
    if (regionStats == null) {
      regionStats = rsStats.get(regionName);
    }
  }
  regionStats.update(stats);
}
 
Example 14
Project: ditb   File: TestTableInputFormatScanBase.java   View Source Code Vote up 5 votes
protected void reduce(ImmutableBytesWritable key,
    Iterable<ImmutableBytesWritable> values, Context context)
throws IOException ,InterruptedException {
  int count = 0;
  for (ImmutableBytesWritable value : values) {
    String val = Bytes.toStringBinary(value.get());
    LOG.info("reduce: key[" + count + "] -> " +
      Bytes.toStringBinary(key.get()) + ", value -> " + val);
    if (first == null) first = val;
    last = val;
    count++;
  }
}
 
Example 15
Project: ditb   File: HFileReaderV2.java   View Source Code Vote up 4 votes
@Override
public String getValueString() {
  ByteBuffer valueBuffer = getValue();
  return Bytes.toStringBinary(valueBuffer.array(),
      valueBuffer.arrayOffset(), valueBuffer.limit());
}
 
Example 16
Project: ditb   File: CellCounter.java   View Source Code Vote up 4 votes
/**
 * Maps the data.
 *
 * @param row     The current table row key.
 * @param values  The columns.
 * @param context The current context.
 * @throws IOException When something is broken with the data.
 * @see org.apache.hadoop.mapreduce.Mapper#map(KEYIN, VALUEIN,
 *      org.apache.hadoop.mapreduce.Mapper.Context)
 */

@Override
public void map(ImmutableBytesWritable row, Result values,
                Context context)
    throws IOException {
  Preconditions.checkState(values != null,
      "values passed to the map is null");
  String currentFamilyName = null;
  String currentQualifierName = null;
  String currentRowKey = null;
  Configuration config = context.getConfiguration();
  String separator = config.get("ReportSeparator",":");
  try {
    context.getCounter(Counters.ROWS).increment(1);
    context.write(new Text("Total ROWS"), new IntWritable(1));

    for (Cell value : values.listCells()) {
      currentRowKey = Bytes.toStringBinary(CellUtil.cloneRow(value));
      String thisRowFamilyName = Bytes.toStringBinary(CellUtil.cloneFamily(value));
      if (!thisRowFamilyName.equals(currentFamilyName)) {
        currentFamilyName = thisRowFamilyName;
        context.getCounter("CF", thisRowFamilyName).increment(1);
        if (1 == context.getCounter("CF", thisRowFamilyName).getValue()) {
          context.write(new Text("Total Families Across all Rows"), new IntWritable(1));
          context.write(new Text(thisRowFamilyName), new IntWritable(1));
        }
      }
      String thisRowQualifierName = thisRowFamilyName + separator
          + Bytes.toStringBinary(CellUtil.cloneQualifier(value));
      if (!thisRowQualifierName.equals(currentQualifierName)) {
        currentQualifierName = thisRowQualifierName;
        context.getCounter("CFQL", thisRowQualifierName).increment(1);
        context.write(new Text("Total Qualifiers across all Rows"),
          new IntWritable(1));
        context.write(new Text(thisRowQualifierName), new IntWritable(1));
        // Intialize versions
        context.getCounter("QL_VERSIONS", currentRowKey + separator +
          thisRowQualifierName).increment(1);
        context.write(new Text(currentRowKey + separator
            + thisRowQualifierName + "_Versions"), new IntWritable(1));

      } else {
        // Increment versions
        currentQualifierName = thisRowQualifierName;
        context.getCounter("QL_VERSIONS", currentRowKey + separator +
          thisRowQualifierName).increment(1);
        context.write(new Text(currentRowKey + separator
            + thisRowQualifierName + "_Versions"), new IntWritable(1));
      }
    }
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
}
 
Example 17
Project: ditb   File: SplitRequest.java   View Source Code Vote up 4 votes
@Override
public String toString() {
  return "regionName=" + parent + ", midKey=" + Bytes.toStringBinary(midKey);
}
 
Example 18
Project: ditb   File: StoreFile.java   View Source Code Vote up 4 votes
private void appendGeneralBloomfilter(final Cell cell) throws IOException {
  if (this.generalBloomFilterWriter != null) {
    // only add to the bloom filter on a new, unique key
    boolean newKey = true;
    if (this.lastCell != null) {
      switch (bloomType) {
      case ROW:
        newKey = !kvComparator.matchingRows(cell, lastCell);
        break;
      case ROWCOL:
        newKey = !kvComparator.matchingRowColumn(cell, lastCell);
        break;
      case NONE:
        newKey = false;
        break;
      default:
        throw new IOException(
            "Invalid Bloom filter type: " + bloomType + " (ROW or ROWCOL expected)");
      }
    }
    if (newKey) {
      /*
       * http://2.bp.blogspot.com/_Cib_A77V54U/StZMrzaKufI/AAAAAAAAADo/ZhK7bGoJdMQ/s400/KeyValue.
       * png Key = RowLen + Row + FamilyLen + Column [Family + Qualifier] + TimeStamp 2 Types of
       * Filtering: 1. Row = Row 2. RowCol = Row + Qualifier
       */
      byte[] bloomKey;
      int bloomKeyOffset, bloomKeyLen;

      switch (bloomType) {
      case ROW:
        bloomKey = cell.getRowArray();
        bloomKeyOffset = cell.getRowOffset();
        bloomKeyLen = cell.getRowLength();
        break;
      case ROWCOL:
        // merge(row, qualifier)
        // TODO: could save one buffer copy in case of compound Bloom
        // filters when this involves creating a KeyValue
        bloomKey = generalBloomFilterWriter
            .createBloomKey(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength(),
                cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
        bloomKeyOffset = 0;
        bloomKeyLen = bloomKey.length;
        break;
      default:
        throw new IOException(
            "Invalid Bloom filter type: " + bloomType + " (ROW or ROWCOL expected)");
      }
      generalBloomFilterWriter.add(bloomKey, bloomKeyOffset, bloomKeyLen);
      if (lastBloomKey != null && generalBloomFilterWriter.getComparator()
          .compareFlatKey(bloomKey, bloomKeyOffset, bloomKeyLen, lastBloomKey,
              lastBloomKeyOffset, lastBloomKeyLen) <= 0) {
        throw new IOException("Non-increasing Bloom keys: " + Bytes
            .toStringBinary(bloomKey, bloomKeyOffset, bloomKeyLen) + " after " + Bytes
            .toStringBinary(lastBloomKey, lastBloomKeyOffset, lastBloomKeyLen));
      }
      lastBloomKey = bloomKey;
      lastBloomKeyOffset = bloomKeyOffset;
      lastBloomKeyLen = bloomKeyLen;
      this.lastCell = cell;
    }
  }
}
 
Example 19
Project: ditb   File: ScanWildcardColumnTracker.java   View Source Code Vote up 4 votes
/**
 * {@inheritDoc}
 * This receives puts *and* deletes. Deletes do not count as a version, but rather
 * take the version of the previous put (so eventually all but the last can be reclaimed).
 */
@Override
public ScanQueryMatcher.MatchCode checkVersions(byte[] bytes, int offset, int length,
    long timestamp, byte type, boolean ignoreCount) throws IOException {

  if (columnBuffer == null) {
    // first iteration.
    resetBuffer(bytes, offset, length);
    if (ignoreCount) return ScanQueryMatcher.MatchCode.INCLUDE;
    // do not count a delete marker as another version
    return checkVersion(type, timestamp);
  }
  int cmp = Bytes.compareTo(bytes, offset, length,
      columnBuffer, columnOffset, columnLength);
  if (cmp == 0) {
    if (ignoreCount) return ScanQueryMatcher.MatchCode.INCLUDE;

    //If column matches, check if it is a duplicate timestamp
    if (sameAsPreviousTSAndType(timestamp, type)) {
      return ScanQueryMatcher.MatchCode.SKIP;
    }
    return checkVersion(type, timestamp);
  }

  resetTSAndType();

  // new col > old col
  if (cmp > 0) {
    // switched columns, lets do something.x
    resetBuffer(bytes, offset, length);
    if (ignoreCount) return ScanQueryMatcher.MatchCode.INCLUDE;
    return checkVersion(type, timestamp);
  }

  // new col < oldcol
  // WARNING: This means that very likely an edit for some other family
  // was incorrectly stored into the store for this one. Throw an exception,
  // because this might lead to data corruption.
  throw new IOException(
      "ScanWildcardColumnTracker.checkColumn ran into a column actually " +
      "smaller than the previous column: " +
      Bytes.toStringBinary(bytes, offset, length));
}
 
Example 20
Project: ditb   File: TestMultiTableSnapshotInputFormatImpl.java   View Source Code Vote up 2 votes
/**
 * Creates a new instance of this class while copying all values.
 *
 * @param scan The scan instance to copy from.
 * @throws java.io.IOException When copying the values fails.
 */
public ScanWithEquals(Scan scan) throws IOException {
  this.startRow = Bytes.toStringBinary(scan.getStartRow());
  this.stopRow = Bytes.toStringBinary(scan.getStopRow());
}