Java Code Examples for org.apache.hadoop.hbase.client.Put.addImmutable()

The following are Jave code examples for showing how to use addImmutable() of the org.apache.hadoop.hbase.client.Put class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: ditb   File: MultiThreadedClientExample.java   Source Code and License Vote up 6 votes
@Override
public Boolean call() throws Exception {

  // Table implements Closable so we use the try with resource structure here.
  // https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
  try (Table t = connection.getTable(tableName)) {
    byte[] value = Bytes.toBytes(Double.toString(ThreadLocalRandom.current().nextDouble()));
    int rows = 30;

    // Array to put the batch
    ArrayList<Put> puts = new ArrayList<>(rows);
    for (int i = 0; i < 30; i++) {
      byte[] rk = Bytes.toBytes(ThreadLocalRandom.current().nextLong());
      Put p = new Put(rk);
      p.addImmutable(FAMILY, QUAL, value);
      puts.add(p);
    }

    // now that we've assembled the batch it's time to push it to hbase.
    t.put(puts);
  }
  return true;
}
 
Example 2
Project: ditb   File: MetaMigrationConvertingToPB.java   Source Code and License Vote up 5 votes
static void migrateSplitIfNecessary(final Result r, final Put p, final byte [] which)
    throws IOException {
  byte [] hriSplitBytes = getBytes(r, which);
  if (!isMigrated(hriSplitBytes)) {
    //This will 'migrate' the HRI from 092.x and 0.94.x to 0.96+ by reading the
    //writable serialization
    HRegionInfo hri = parseFrom(hriSplitBytes);
    p.addImmutable(HConstants.CATALOG_FAMILY, which, hri.toByteArray());
  }
}
 
Example 3
Project: ditb   File: DefaultVisibilityLabelServiceImpl.java   Source Code and License Vote up 5 votes
protected void addSystemLabel(Region region, Map<String, Integer> labels,
    Map<String, List<Integer>> userAuths) throws IOException {
  if (!labels.containsKey(SYSTEM_LABEL)) {
    Put p = new Put(Bytes.toBytes(SYSTEM_LABEL_ORDINAL));
    p.addImmutable(LABELS_TABLE_FAMILY, LABEL_QUALIFIER, Bytes.toBytes(SYSTEM_LABEL));
    region.put(p);
    labels.put(SYSTEM_LABEL, SYSTEM_LABEL_ORDINAL);
  }
}
 
Example 4
Project: ditb   File: AccessControlLists.java   Source Code and License Vote up 5 votes
/**
 * Stores a new user permission grant in the access control lists table.
 * @param conf the configuration
 * @param userPerm the details of the permission to be granted
 * @throws IOException in the case of an error accessing the metadata table
 */
static void addUserPermission(Configuration conf, UserPermission userPerm)
    throws IOException {
  Permission.Action[] actions = userPerm.getActions();
  byte[] rowKey = userPermissionRowKey(userPerm);
  Put p = new Put(rowKey);
  byte[] key = userPermissionKey(userPerm);

  if ((actions == null) || (actions.length == 0)) {
    String msg = "No actions associated with user '" + Bytes.toString(userPerm.getUser()) + "'";
    LOG.warn(msg);
    throw new IOException(msg);
  }

  byte[] value = new byte[actions.length];
  for (int i = 0; i < actions.length; i++) {
    value[i] = actions[i].code();
  }
  p.addImmutable(ACL_LIST_FAMILY, key, value);
  if (LOG.isDebugEnabled()) {
    LOG.debug("Writing permission with rowKey "+
        Bytes.toString(rowKey)+" "+
        Bytes.toString(key)+": "+Bytes.toStringBinary(value)
    );
  }
  // TODO: Pass in a Connection rather than create one each time.
  try (Connection connection = ConnectionFactory.createConnection(conf)) {
    try (Table table = connection.getTable(ACL_TABLE_NAME)) {
      table.put(p);
    }
  }
}
 
Example 5
Project: ditb   File: FavoredNodeAssignmentHelper.java   Source Code and License Vote up 5 votes
/**
 * Generates and returns a Put containing the region info for the catalog table
 * and the servers
 * @param regionInfo
 * @param favoredNodeList
 * @return Put object
 */
static Put makePutFromRegionInfo(HRegionInfo regionInfo, List<ServerName>favoredNodeList)
throws IOException {
  Put put = null;
  if (favoredNodeList != null) {
    put = MetaTableAccessor.makePutFromRegionInfo(regionInfo);
    byte[] favoredNodes = getFavoredNodes(favoredNodeList);
    put.addImmutable(HConstants.CATALOG_FAMILY, FAVOREDNODES_QUALIFIER,
        EnvironmentEdgeManager.currentTime(), favoredNodes);
    LOG.info("Create the region " + regionInfo.getRegionNameAsString() +
        " with favored nodes " + Bytes.toString(favoredNodes));
  }
  return put;
}
 
Example 6
Project: ditb   File: TableNamespaceManager.java   Source Code and License Vote up 5 votes
private void upsert(Table table, NamespaceDescriptor ns) throws IOException {
  validateTableAndRegionCount(ns);
  Put p = new Put(Bytes.toBytes(ns.getName()));
  p.addImmutable(HTableDescriptor.NAMESPACE_FAMILY_INFO_BYTES,
      HTableDescriptor.NAMESPACE_COL_DESC_BYTES,
      ProtobufUtil.toProtoNamespaceDescriptor(ns).toByteArray());
  table.put(p);
  try {
    zkNamespaceManager.update(ns);
  } catch(IOException ex) {
    String msg = "Failed to update namespace information in ZK. Aborting.";
    LOG.fatal(msg, ex);
    masterServices.abort(msg, ex);
  }
}
 
Example 7
Project: ditb   File: SplitTransactionImpl.java   Source Code and License Vote up 5 votes
private static Put addEmptyLocation(final Put p, int replicaId){
  p.addImmutable(HConstants.CATALOG_FAMILY, MetaTableAccessor.getServerColumn(replicaId), null);
  p.addImmutable(HConstants.CATALOG_FAMILY, MetaTableAccessor.getStartCodeColumn(replicaId),
    null);
  p.addImmutable(HConstants.CATALOG_FAMILY, MetaTableAccessor.getSeqNumColumn(replicaId), null);
  return p;
}
 
Example 8
Project: ditb   File: SplitTransactionImpl.java   Source Code and License Vote up 5 votes
public Put addLocation(final Put p, final ServerName sn, long openSeqNum) {
  p.addImmutable(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER,
    Bytes.toBytes(sn.getHostAndPort()));
  p.addImmutable(HConstants.CATALOG_FAMILY, HConstants.STARTCODE_QUALIFIER,
    Bytes.toBytes(sn.getStartcode()));
  p.addImmutable(HConstants.CATALOG_FAMILY, HConstants.SEQNUM_QUALIFIER,
      Bytes.toBytes(openSeqNum));
  return p;
}
 
Example 9
Project: ditb   File: RegionMergeTransactionImpl.java   Source Code and License Vote up 5 votes
private static Put addEmptyLocation(final Put p, int replicaId) {
  p.addImmutable(HConstants.CATALOG_FAMILY, MetaTableAccessor.getServerColumn(replicaId), null);
  p.addImmutable(HConstants.CATALOG_FAMILY, MetaTableAccessor.getStartCodeColumn(replicaId),
    null);
  p.addImmutable(HConstants.CATALOG_FAMILY, MetaTableAccessor.getSeqNumColumn(replicaId), null);
  return p;
}
 
Example 10
Project: ditb   File: MetaTableAccessor.java   Source Code and License Vote up 5 votes
/**
 * Adds split daughters to the Put
 */
public static Put addDaughtersToPut(Put put, HRegionInfo splitA, HRegionInfo splitB) {
  if (splitA != null) {
    put.addImmutable(
      HConstants.CATALOG_FAMILY, HConstants.SPLITA_QUALIFIER, splitA.toByteArray());
  }
  if (splitB != null) {
    put.addImmutable(
      HConstants.CATALOG_FAMILY, HConstants.SPLITB_QUALIFIER, splitB.toByteArray());
  }
  return put;
}
 
Example 11
Project: ditb   File: MetaTableAccessor.java   Source Code and License Vote up 5 votes
/**
 * Merge the two regions into one in an atomic operation. Deletes the two
 * merging regions in hbase:meta and adds the merged region with the information of
 * two merging regions.
 * @param connection connection we're using
 * @param mergedRegion the merged region
 * @param regionA
 * @param regionB
 * @param sn the location of the region
 * @param masterSystemTime
 * @throws IOException
 */
public static void mergeRegions(final Connection connection, HRegionInfo mergedRegion,
    HRegionInfo regionA, HRegionInfo regionB, ServerName sn, int regionReplication,
    long masterSystemTime)
        throws IOException {
  Table meta = getMetaHTable(connection);
  try {
    HRegionInfo copyOfMerged = new HRegionInfo(mergedRegion);

    // use the maximum of what master passed us vs local time.
    long time = Math.max(EnvironmentEdgeManager.currentTime(), masterSystemTime);

    // Put for parent
    Put putOfMerged = makePutFromRegionInfo(copyOfMerged, time);
    putOfMerged.addImmutable(HConstants.CATALOG_FAMILY, HConstants.MERGEA_QUALIFIER,
      regionA.toByteArray());
    putOfMerged.addImmutable(HConstants.CATALOG_FAMILY, HConstants.MERGEB_QUALIFIER,
      regionB.toByteArray());

    // Deletes for merging regions
    Delete deleteA = makeDeleteFromRegionInfo(regionA, time);
    Delete deleteB = makeDeleteFromRegionInfo(regionB, time);

    // The merged is a new region, openSeqNum = 1 is fine.
    addLocation(putOfMerged, sn, 1, -1, mergedRegion.getReplicaId());

    // Add empty locations for region replicas of the merged region so that number of replicas can
    // be cached whenever the primary region is looked up from meta
    for (int i = 1; i < regionReplication; i++) {
      addEmptyLocation(putOfMerged, i);
    }

    byte[] tableRow = Bytes.toBytes(mergedRegion.getRegionNameAsString()
      + HConstants.DELIMITER);
    multiMutate(meta, tableRow, putOfMerged, deleteA, deleteB);
  } finally {
    meta.close();
  }
}
 
Example 12
Project: ditb   File: MetaTableAccessor.java   Source Code and License Vote up 5 votes
public static Put addLocation(final Put p, final ServerName sn, long openSeqNum,
    long time, int replicaId){
  if (time <= 0) {
    time = EnvironmentEdgeManager.currentTime();
  }
  p.addImmutable(HConstants.CATALOG_FAMILY, getServerColumn(replicaId), time,
    Bytes.toBytes(sn.getHostAndPort()));
  p.addImmutable(HConstants.CATALOG_FAMILY, getStartCodeColumn(replicaId), time,
    Bytes.toBytes(sn.getStartcode()));
  p.addImmutable(HConstants.CATALOG_FAMILY, getSeqNumColumn(replicaId), time,
    Bytes.toBytes(openSeqNum));
  return p;
}
 
Example 13
Project: ditb   File: MetaTableAccessor.java   Source Code and License Vote up 5 votes
public static Put addEmptyLocation(final Put p, int replicaId) {
  long now = EnvironmentEdgeManager.currentTime();
  p.addImmutable(HConstants.CATALOG_FAMILY, getServerColumn(replicaId), now, null);
  p.addImmutable(HConstants.CATALOG_FAMILY, getStartCodeColumn(replicaId), now, null);
  p.addImmutable(HConstants.CATALOG_FAMILY, getSeqNumColumn(replicaId), now, null);
  return p;
}
 
Example 14
Project: ditb   File: MultiThreadedClientExample.java   Source Code and License Vote up 5 votes
@Override
public Boolean call() throws Exception {
  try (Table t = connection.getTable(tableName)) {

    byte[] value = Bytes.toBytes(Double.toString(ThreadLocalRandom.current().nextDouble()));
    byte[] rk = Bytes.toBytes(ThreadLocalRandom.current().nextLong());
    Put p = new Put(rk);
    p.addImmutable(FAMILY, QUAL, value);
    t.put(p);
  }
  return true;
}
 
Example 15
Project: ditb   File: MetaTableAccessor.java   Source Code and License Vote up 4 votes
private static Put addRegionInfo(final Put p, final HRegionInfo hri)
  throws IOException {
  p.addImmutable(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER,
    hri.toByteArray());
  return p;
}
 
Example 16
Project: ditb   File: RowResource.java   Source Code and License Vote up 4 votes
Response updateBinary(final byte[] message, final HttpHeaders headers,
    final boolean replace) {
  servlet.getMetrics().incrementRequests(1);
  if (servlet.isReadOnly()) {
    servlet.getMetrics().incrementFailedPutRequests(1);
    return Response.status(Response.Status.FORBIDDEN)
      .type(MIMETYPE_TEXT).entity("Forbidden" + CRLF)
      .build();
  }
  Table table = null;
  try {
    byte[] row = rowspec.getRow();
    byte[][] columns = rowspec.getColumns();
    byte[] column = null;
    if (columns != null) {
      column = columns[0];
    }
    long timestamp = HConstants.LATEST_TIMESTAMP;
    List<String> vals = headers.getRequestHeader("X-Row");
    if (vals != null && !vals.isEmpty()) {
      row = Bytes.toBytes(vals.get(0));
    }
    vals = headers.getRequestHeader("X-Column");
    if (vals != null && !vals.isEmpty()) {
      column = Bytes.toBytes(vals.get(0));
    }
    vals = headers.getRequestHeader("X-Timestamp");
    if (vals != null && !vals.isEmpty()) {
      timestamp = Long.valueOf(vals.get(0));
    }
    if (column == null) {
      servlet.getMetrics().incrementFailedPutRequests(1);
      return Response.status(Response.Status.BAD_REQUEST)
          .type(MIMETYPE_TEXT).entity("Bad request: Column found to be null." + CRLF)
          .build();
    }
    Put put = new Put(row);
    byte parts[][] = KeyValue.parseColumn(column);
    if (parts.length != 2) {
      return Response.status(Response.Status.BAD_REQUEST)
        .type(MIMETYPE_TEXT).entity("Bad request" + CRLF)
        .build();
    }
    put.addImmutable(parts[0], parts[1], timestamp, message);
    table = servlet.getTable(tableResource.getName());
    table.put(put);
    if (LOG.isDebugEnabled()) {
      LOG.debug("PUT " + put.toString());
    }
    servlet.getMetrics().incrementSucessfulPutRequests(1);
    return Response.ok().build();
  } catch (Exception e) {
    servlet.getMetrics().incrementFailedPutRequests(1);
    return processException(e);
  } finally {
    if (table != null) try {
      table.close();
    } catch (IOException ioe) {
      LOG.debug(ioe);
    }
  }
}