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

The following examples show how to use org.apache.hadoop.hbase.util.Bytes#putInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PTimestamp.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public int toBytes(Object object, byte[] bytes, int offset) {
    if (object == null) {
        // Create the byte[] of size MAX_TIMESTAMP_BYTES
        if(bytes.length != getByteSize()) {
            bytes = Bytes.padTail(bytes, (getByteSize() - bytes.length));
        }
        PDate.INSTANCE.getCodec().encodeLong(0l, bytes, offset);
        Bytes.putInt(bytes, offset + Bytes.SIZEOF_LONG, 0);
        return getByteSize();
    }
    java.sql.Timestamp value = (java.sql.Timestamp) object;
    // For Timestamp, the getTime() method includes milliseconds that may
    // be stored in the nanos part as well.
    DateUtil.getCodecFor(this).encodeLong(value.getTime(), bytes, offset);

    /*
     * By not getting the stuff that got spilled over from the millis part,
     * it leaves the timestamp's byte representation saner - 8 bytes of millis | 4 bytes of nanos.
     * Also, it enables timestamp bytes to be directly compared with date/time bytes.
     */
    Bytes.putInt(bytes, offset + Bytes.SIZEOF_LONG, value.getNanos() % MAX_NANOS_VALUE_EXCLUSIVE);
    return getByteSize();
}
 
Example 2
Source File: RSGroupableBalancerTestBase.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Generate assigned regions to a given server using group information.
 *
 * @param numRegions the num regions to generate
 * @param sn the servername
 * @return the list of regions
 * @throws java.io.IOException Signals that an I/O exception has occurred.
 */
protected List<RegionInfo> assignedRegions(int numRegions, ServerName sn) throws IOException {
  List<RegionInfo> regions = new ArrayList<>(numRegions);
  byte[] start = new byte[16];
  byte[] end = new byte[16];
  Bytes.putInt(start, 0, numRegions << 1);
  Bytes.putInt(end, 0, (numRegions << 1) + 1);
  for (int i = 0; i < numRegions; i++) {
    TableName tableName = getTableName(sn);
    regions.add(RegionInfoBuilder.newBuilder(tableName)
        .setStartKey(start)
        .setEndKey(end)
        .setSplit(false)
        .setRegionId(regionId++)
        .build());
  }
  return regions;
}
 
Example 3
Source File: PUnsignedTimestamp.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public int toBytes(Object object, byte[] bytes, int offset) {
  if (object == null) {
    throw newIllegalDataException(this + " may not be null");
  }
  java.sql.Timestamp value = (java.sql.Timestamp) object;
  PUnsignedDate.INSTANCE.getCodec().encodeLong(value.getTime(), bytes, offset);

          /*
           * By not getting the stuff that got spilled over from the millis part,
           * it leaves the timestamp's byte representation saner - 8 bytes of millis | 4 bytes of nanos.
           * Also, it enables timestamp bytes to be directly compared with date/time bytes.
           */
  Bytes.putInt(bytes, offset + Bytes.SIZEOF_LONG, value.getNanos() % 1000000);
  return getByteSize();
}
 
Example 4
Source File: BalancerTestBase.java    From hbase with Apache License 2.0 6 votes vote down vote up
protected List<RegionInfo> uniformRegions(int numRegions) {
  List<RegionInfo> regions = new ArrayList<>(numRegions);
  byte[] start = new byte[16];
  byte[] end = new byte[16];
  Random rand = ThreadLocalRandom.current();
  rand.nextBytes(start);
  rand.nextBytes(end);
  for (int i = 0; i < numRegions; i++) {
    Bytes.putInt(start, 0, numRegions << 1);
    Bytes.putInt(end, 0, (numRegions << 1) + 1);
    TableName tableName =
            TableName.valueOf("table" + i);
    RegionInfo hri = RegionInfoBuilder.newBuilder(tableName)
        .setStartKey(start)
        .setEndKey(end)
        .setSplit(false)
        .build();
    regions.add(hri);
  }
  return regions;
}
 
Example 5
Source File: BalancerTestBase.java    From hbase with Apache License 2.0 6 votes vote down vote up
protected List<RegionInfo> createRegions(int numRegions, TableName tableName) {
  List<RegionInfo> regions = new ArrayList<>(numRegions);
  byte[] start = new byte[16];
  byte[] end = new byte[16];
  Random rand = ThreadLocalRandom.current();
  rand.nextBytes(start);
  rand.nextBytes(end);
  for (int i = 0; i < numRegions; i++) {
    Bytes.putInt(start, 0, numRegions << 1);
    Bytes.putInt(end, 0, (numRegions << 1) + 1);
    RegionInfo hri = RegionInfoBuilder.newBuilder(tableName)
      .setStartKey(start)
      .setEndKey(end)
      .setSplit(false)
      .build();
    regions.add(hri);
  }
  return regions;
}
 
Example 6
Source File: PhTypeUtil.java    From canal with Apache License 2.0 5 votes vote down vote up
private static int encodeFloat(float v, byte[] b, int o) {
    checkForSufficientLength(b, o, Bytes.SIZEOF_FLOAT);
    int i = Float.floatToIntBits(v);
    i = (i ^ ((i >> Integer.SIZE - 1) | Integer.MIN_VALUE)) + 1;
    Bytes.putInt(b, o, i);
    return Bytes.SIZEOF_FLOAT;
}
 
Example 7
Source File: PhTypeUtil.java    From canal with Apache License 2.0 5 votes vote down vote up
private static int encodeTimestamp(Object v, byte[] b, int o) {
    if (v instanceof Timestamp) {
        Timestamp ts = (Timestamp) v;
        encodeLong(ts.getTime(), b, o);
        Bytes.putInt(b, Bytes.SIZEOF_LONG, ts.getNanos() % 1000000);
    } else {
        encodeDate(v, b, o);
    }
    return Bytes.SIZEOF_LONG + Bytes.SIZEOF_INT;
}
 
Example 8
Source File: TestFiltersWithBinaryComponentComparator.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Since we are trying to emulate
 * SQL: SELECT * from table where a = 1 and b > 10 and b < 20 and
 * c > 90 and c < 100 and d = 1
 * We are generating rows with:
 * a = 1, b >=9 and b < 22, c >= 89 and c < 102, and d = 1
 * At the end the table will look something like this:
 * ------------
 *  a| b|  c|d|
 * ------------
 *  1| 9| 89|1|family:qf|xyz|
 *  -----------
 *  1| 9| 90|1|family:qf|abc|
 *  -----------
 *  1| 9| 91|1|family:qf|xyz|
 *  -------------------------
 *  .
 *  -------------------------
 *  .
 *  -------------------------
 *  1|21|101|1|family:qf|xyz|
 */
private void generateRows(Table ht, byte[] family, byte[] qf)
    throws IOException {
  for(int a = 1; a < 2; ++a) {
    for(int b = 9; b < 22; ++b) {
      for(int c = 89; c < 102; ++c) {
        for(int d = 1; d < 2 ; ++d) {
          byte[] key = new byte[16];
          Bytes.putInt(key,0,a);
          Bytes.putInt(key,4,b);
          Bytes.putInt(key,8,c);
          Bytes.putInt(key,12,d);
          Put row = new Put(key);
          if (c%2==0) {
            row.addColumn(family, qf, Bytes.toBytes("abc"));
            if (LOG.isInfoEnabled()) {
              LOG.info("added row: {} with value 'abc'", Arrays.toString(Hex.encodeHex(key)));
            }
          } else {
            row.addColumn(family, qf, Bytes.toBytes("xyz"));
            if (LOG.isInfoEnabled()) {
              LOG.info("added row: {} with value 'xyz'", Arrays.toString(Hex.encodeHex(key)));
            }
          }
        }
      }
    }
  }
  TEST_UTIL.flush();
}
 
Example 9
Source File: KeyValueUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * @param b
 * @param o
 * @param l
 * @return A KeyValue made of a byte array that holds the key-only part.
 *         Needed to convert hfile index members to KeyValues.
 */
public static KeyValue createKeyValueFromKey(final byte[] b, final int o, final int l) {
  byte[] newb = new byte[l + KeyValue.ROW_OFFSET];
  System.arraycopy(b, o, newb, KeyValue.ROW_OFFSET, l);
  Bytes.putInt(newb, 0, l);
  Bytes.putInt(newb, Bytes.SIZEOF_INT, 0);
  return new KeyValue(newb);
}
 
Example 10
Source File: KeyValueUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**************** copy key and value *********************/

  public static int appendToByteArray(Cell cell, byte[] output, int offset, boolean withTags) {
    int pos = offset;
    pos = Bytes.putInt(output, pos, keyLength(cell));
    pos = Bytes.putInt(output, pos, cell.getValueLength());
    pos = appendKeyTo(cell, output, pos);
    pos = CellUtil.copyValueTo(cell, output, pos);
    if (withTags && (cell.getTagsLength() > 0)) {
      pos = Bytes.putAsShort(output, pos, cell.getTagsLength());
      pos = PrivateCellUtil.copyTagsTo(cell, output, pos);
    }
    return pos;
  }
 
Example 11
Source File: PhTypeUtil.java    From canal with Apache License 2.0 5 votes vote down vote up
private static int encodeUnsignedInt(int v, byte[] b, int o) {
    checkForSufficientLength(b, o, Bytes.SIZEOF_INT);
    if (v < 0) {
        throw new RuntimeException();
    }
    Bytes.putInt(b, o, v);
    return Bytes.SIZEOF_INT;
}
 
Example 12
Source File: KeyValue.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Write KeyValue format into a byte array.
 * @param row row key
 * @param roffset row offset
 * @param rlength row length
 * @param family family name
 * @param foffset family offset
 * @param flength family length
 * @param qualifier column qualifier
 * @param qoffset qualifier offset
 * @param qlength qualifier length
 * @param timestamp version timestamp
 * @param type key type
 * @param value column value
 * @param voffset value offset
 * @param vlength value length
 * @return The newly created byte array.
 */
private static byte [] createByteArray(final byte [] row, final int roffset,
    final int rlength, final byte [] family, final int foffset, int flength,
    final byte [] qualifier, final int qoffset, int qlength,
    final long timestamp, final Type type,
    final byte [] value, final int voffset,
    int vlength, byte[] tags, int tagsOffset, int tagsLength) {

  checkParameters(row, rlength, family, flength, qlength, vlength);
  RawCell.checkForTagsLength(tagsLength);
  // Allocate right-sized byte array.
  int keyLength = (int) getKeyDataStructureSize(rlength, flength, qlength);
  byte[] bytes = new byte[(int) getKeyValueDataStructureSize(rlength, flength, qlength, vlength,
    tagsLength)];
  // Write key, value and key row length.
  int pos = 0;
  pos = Bytes.putInt(bytes, pos, keyLength);
  pos = Bytes.putInt(bytes, pos, vlength);
  pos = Bytes.putShort(bytes, pos, (short)(rlength & 0x0000ffff));
  pos = Bytes.putBytes(bytes, pos, row, roffset, rlength);
  pos = Bytes.putByte(bytes, pos, (byte)(flength & 0x0000ff));
  if(flength != 0) {
    pos = Bytes.putBytes(bytes, pos, family, foffset, flength);
  }
  if(qlength != 0) {
    pos = Bytes.putBytes(bytes, pos, qualifier, qoffset, qlength);
  }
  pos = Bytes.putLong(bytes, pos, timestamp);
  pos = Bytes.putByte(bytes, pos, type.getCode());
  if (value != null && value.length > 0) {
    pos = Bytes.putBytes(bytes, pos, value, voffset, vlength);
  }
  // Add the tags after the value part
  if (tagsLength > 0) {
    pos = Bytes.putAsShort(bytes, pos, tagsLength);
    pos = Bytes.putBytes(bytes, pos, tags, tagsOffset, tagsLength);
  }
  return bytes;
}
 
Example 13
Source File: BufferedDataBlockEncoder.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public void endBlockEncoding(HFileBlockEncodingContext encodingCtx, DataOutputStream out,
    byte[] uncompressedBytesWithHeader) throws IOException {
  EncodingState state = encodingCtx.getEncodingState();
  // Write the unencodedDataSizeWritten (with header size)
  Bytes.putInt(uncompressedBytesWithHeader,
    HConstants.HFILEBLOCK_HEADER_SIZE + DataBlockEncoding.ID_SIZE,
    state.getUnencodedDataSizeWritten());
  postEncoding(encodingCtx);
}
 
Example 14
Source File: PFloat.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public int encodeFloat(float v, byte[] b, int o) {
  checkForSufficientLength(b, o, Bytes.SIZEOF_FLOAT);
  int i = Float.floatToIntBits(v);
  i = (i ^ ((i >> Integer.SIZE - 1) | Integer.MIN_VALUE)) + 1;
  Bytes.putInt(b, o, i);
  return Bytes.SIZEOF_FLOAT;
}
 
Example 15
Source File: PhTypeUtil.java    From canal with Apache License 2.0 5 votes vote down vote up
private static int encodeUnsignedTimestamp(Object v, byte[] b, int o) {
    if (v instanceof Timestamp) {
        Timestamp ts = (Timestamp) v;
        encodeUnsignedLong(ts.getTime(), b, o);
        Bytes.putInt(b, Bytes.SIZEOF_LONG, ts.getNanos() % 1000000);
    } else {
        encodeUnsignedDate(v, b, o);
    }
    return Bytes.SIZEOF_LONG + Bytes.SIZEOF_INT;
}
 
Example 16
Source File: JobCountersSerDeser.java    From eagle with Apache License 2.0 5 votes vote down vote up
private int serializeGroup(byte[] buffer, int currentPos, String groupName, Map<String, Long> counterMap) throws JobCounterException {
    int pos = currentPos;
    final CounterGroupKey groupKey = getCounterGroup(groupName);
    if (groupKey == null) {
        throw new JobCounterException("Group name " + groupName + " is not defined");
    }
    Bytes.putInt(buffer, pos, groupKey.getIndex());
    pos += 4;
    int totalCounterNumberPos = pos;
    pos += 4;
    int totalCounters = 0;
    
    for (Map.Entry<String, Long> entry : counterMap.entrySet()) {
        final String counterName = entry.getKey();
        final CounterKey counterKey = groupKey.getCounterKeyByName(counterName);
        if (counterKey == null) {
            continue;
        }
        final Long counterValue = entry.getValue();
        Bytes.putInt(buffer, pos, counterKey.getIndex());
        pos += 4;
        Bytes.putLong(buffer, pos, counterValue);
        pos += 8;
        ++totalCounters;
    }
    Bytes.putInt(buffer, totalCounterNumberPos, totalCounters);
    return pos;
}
 
Example 17
Source File: RegionChecker.java    From splicer with Apache License 2.0 5 votes vote down vote up
/**
 * Find the best region server for a given row key range
 *
 * @param metric name of the metric
 * @param startTime in seconds
 * @param endTime in seconds
 * @return the best region server to query for this metric, start, stop row key
 */
public String getBestRegionHost(String metric, long startTime, long endTime) {
	final byte[] startRowKey = new byte[METRIC_WIDTH + TS_HOUR_WIDTH];
	final byte[] endRowKey = new byte[METRIC_WIDTH + TS_HOUR_WIDTH];

	byte[] metricKey = MetricsCache.get().getMetricKey(metric);

	System.arraycopy(metricKey, 0, startRowKey, 0, METRIC_WIDTH);
	System.arraycopy(metricKey, 0, endRowKey, 0, METRIC_WIDTH);

	Bytes.putInt(startRowKey, METRIC_WIDTH, (int) startTime);
	Bytes.putInt(endRowKey, METRIC_WIDTH, (int) endTime);

	return getBestRegionHost(startRowKey, endRowKey);
}
 
Example 18
Source File: PArrayDataType.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static void writeEndBytes(byte[] array, int newOffsetArrayPosition, int offsetArrayLength, int arrayLength,
        byte header, boolean useInt) {
    int byteSize = useInt ? Bytes.SIZEOF_INT : Bytes.SIZEOF_SHORT;

    Bytes.putInt(array, newOffsetArrayPosition + offsetArrayLength + byteSize, newOffsetArrayPosition);
    Bytes.putInt(array, newOffsetArrayPosition + offsetArrayLength + byteSize + Bytes.SIZEOF_INT, arrayLength);
    Bytes.putByte(array, newOffsetArrayPosition + offsetArrayLength + byteSize + 2 * Bytes.SIZEOF_INT, header);
}
 
Example 19
Source File: PUnsignedInt.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public int encodeInt(int v, byte[] b, int o) {
  checkForSufficientLength(b, o, Bytes.SIZEOF_INT);
  if (v < 0) {
    throw newIllegalDataException();
  }
  Bytes.putInt(b, o, v);
  return Bytes.SIZEOF_INT;
}
 
Example 20
Source File: WALCellCodec.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
protected Cell parseCell() throws IOException {
  int keylength = StreamUtils.readRawVarint32(in);
  int vlength = StreamUtils.readRawVarint32(in);

  int tagsLength = StreamUtils.readRawVarint32(in);
  int length = 0;
  if(tagsLength == 0) {
    length = KeyValue.KEYVALUE_INFRASTRUCTURE_SIZE + keylength + vlength;
  } else {
    length = KeyValue.KEYVALUE_WITH_TAGS_INFRASTRUCTURE_SIZE + keylength + vlength + tagsLength;
  }

  byte[] backingArray = new byte[length];
  int pos = 0;
  pos = Bytes.putInt(backingArray, pos, keylength);
  pos = Bytes.putInt(backingArray, pos, vlength);

  // the row
  int elemLen = readIntoArray(backingArray, pos + Bytes.SIZEOF_SHORT,
    compression.getDictionary(CompressionContext.DictionaryIndex.ROW));
  checkLength(elemLen, Short.MAX_VALUE);
  pos = Bytes.putShort(backingArray, pos, (short)elemLen);
  pos += elemLen;

  // family
  elemLen = readIntoArray(backingArray, pos + Bytes.SIZEOF_BYTE,
    compression.getDictionary(CompressionContext.DictionaryIndex.FAMILY));
  checkLength(elemLen, Byte.MAX_VALUE);
  pos = Bytes.putByte(backingArray, pos, (byte)elemLen);
  pos += elemLen;

  // qualifier
  elemLen = readIntoArray(backingArray, pos,
    compression.getDictionary(CompressionContext.DictionaryIndex.QUALIFIER));
  pos += elemLen;

  // timestamp, type and value
  int tsTypeValLen = length - pos;
  if (tagsLength > 0) {
    tsTypeValLen = tsTypeValLen - tagsLength - KeyValue.TAGS_LENGTH_SIZE;
  }
  IOUtils.readFully(in, backingArray, pos, tsTypeValLen);
  pos += tsTypeValLen;

  // tags
  if (tagsLength > 0) {
    pos = Bytes.putAsShort(backingArray, pos, tagsLength);
    if (compression.tagCompressionContext != null) {
      compression.tagCompressionContext.uncompressTags(in, backingArray, pos, tagsLength);
    } else {
      IOUtils.readFully(in, backingArray, pos, tagsLength);
    }
  }
  return new KeyValue(backingArray, 0, length);
}