Package org.apache.hadoop.hbase.client

Examples of org.apache.hadoop.hbase.client.Table


   * @param sn the location of the region
   */
  public static void splitRegion(final HConnection hConnection,
                                 HRegionInfo parent, HRegionInfo splitA, HRegionInfo splitB,
                                 ServerName sn) throws IOException {
    Table meta = getMetaHTable(hConnection);
    try {
      HRegionInfo copyOfParent = new HRegionInfo(parent);
      copyOfParent.setOffline(true);
      copyOfParent.setSplit(true);

      //Put for parent
      Put putParent = makePutFromRegionInfo(copyOfParent);
      addDaughtersToPut(putParent, splitA, splitB);

      //Puts for daughters
      Put putA = makePutFromRegionInfo(splitA);
      Put putB = makePutFromRegionInfo(splitB);

      addLocation(putA, sn, 1, splitA.getReplicaId()); //new regions, openSeqNum = 1 is fine.
      addLocation(putB, sn, 1, splitB.getReplicaId());

      byte[] tableRow = Bytes.toBytes(parent.getRegionNameAsString() + HConstants.DELIMITER);
      multiMutate(meta, tableRow, putParent, putA, putB);
    } finally {
      meta.close();
    }
  }
View Full Code Here


   * @throws Throwable
   */
  public static GrantResponse grant(Configuration conf, final TableName tableName,
      final String userName, final byte[] family, final byte[] qual,
      final AccessControlProtos.Permission.Action... actions) throws Throwable {
    Table ht = null;
    try {
      TableName aclTableName =
          TableName.valueOf(NamespaceDescriptor.SYSTEM_NAMESPACE_NAME_STR, "acl");
      ht = new HTable(conf, aclTableName.getName());
      Batch.Call<AccessControlService, GrantResponse> callable =
          new Batch.Call<AccessControlService, GrantResponse>() {
        ServerRpcController controller = new ServerRpcController();
        BlockingRpcCallback<GrantResponse> rpcCallback =
            new BlockingRpcCallback<GrantResponse>();

        @Override
        public GrantResponse call(AccessControlService service) throws IOException {
          GrantRequest.Builder builder = GrantRequest.newBuilder();
          AccessControlProtos.Permission.Builder ret =
              AccessControlProtos.Permission.newBuilder();
          AccessControlProtos.TablePermission.Builder permissionBuilder =
              AccessControlProtos.TablePermission
              .newBuilder();
          for (AccessControlProtos.Permission.Action a : actions) {
            permissionBuilder.addAction(a);
          }
          permissionBuilder.setTableName(ProtobufUtil.toProtoTableName(tableName));

          if (family != null) {
            permissionBuilder.setFamily(ByteStringer.wrap(family));
          }
          if (qual != null) {
            permissionBuilder.setQualifier(ByteStringer.wrap(qual));
          }
          ret.setType(AccessControlProtos.Permission.Type.Table).setTablePermission(
              permissionBuilder);
          builder.setUserPermission(AccessControlProtos.UserPermission.newBuilder()
              .setUser(ByteString.copyFromUtf8(userName)).setPermission(ret));
          service.grant(controller, builder.build(), rpcCallback);
          return rpcCallback.get();
        }
      };
      Map<byte[], GrantResponse> result = ht.coprocessorService(AccessControlService.class,
          HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, callable);
      return result.values().iterator().next(); // There will be exactly one
                                                // region for labels
                                                // table and so one entry in
                                                // result Map.
    } finally {
      if (ht != null) {
        ht.close();
      }
    }
  }
View Full Code Here

   * @throws Throwable
   */
  public static RevokeResponse revoke(Configuration conf, final String username,
      final TableName tableName, final byte[] family, final byte[] qualifier,
      final AccessControlProtos.Permission.Action... actions) throws Throwable {
    Table ht = null;
    try {
      TableName aclTableName = TableName.valueOf(NamespaceDescriptor.SYSTEM_NAMESPACE_NAME_STR,
          "acl");
      ht = new HTable(conf, aclTableName.getName());
      Batch.Call<AccessControlService, AccessControlProtos.RevokeResponse> callable =
          new Batch.Call<AccessControlService, AccessControlProtos.RevokeResponse>() {
        ServerRpcController controller = new ServerRpcController();
        BlockingRpcCallback<AccessControlProtos.RevokeResponse> rpcCallback =
            new BlockingRpcCallback<AccessControlProtos.RevokeResponse>();

        @Override
        public RevokeResponse call(AccessControlService service) throws IOException {
          AccessControlProtos.Permission.Builder ret =
              AccessControlProtos.Permission.newBuilder();
          AccessControlProtos.TablePermission.Builder permissionBuilder =
              AccessControlProtos.TablePermission.newBuilder();
          for (AccessControlProtos.Permission.Action a : actions) {
            permissionBuilder.addAction(a);
          }
          if (tableName != null) {
            permissionBuilder.setTableName(ProtobufUtil.toProtoTableName(tableName));
          }
          if (family != null) {
            permissionBuilder.setFamily(ByteStringer.wrap(family));
          }
          if (qualifier != null) {
            permissionBuilder.setQualifier(ByteStringer.wrap(qualifier));
          }
          ret.setType(AccessControlProtos.Permission.Type.Table).setTablePermission(
              permissionBuilder);
          RevokeRequest builder = AccessControlProtos.RevokeRequest
              .newBuilder()
              .setUserPermission(
                  AccessControlProtos.UserPermission.newBuilder()
                      .setUser(ByteString.copyFromUtf8(username)).setPermission(ret)).build();
          service.revoke(controller, builder, rpcCallback);
          return rpcCallback.get();
        }
      };
      Map<byte[], RevokeResponse> result = ht.coprocessorService(AccessControlService.class,
          HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY, callable);
      return result.values().iterator().next();

    } finally {
      if (ht != null) {
        ht.close();
      }
    }
  }
View Full Code Here

   * @throws Throwable
   */
  public static List<UserPermission> getUserPermissions(Configuration conf, String tableRegex)
      throws Throwable {
    List<UserPermission> permList = new ArrayList<UserPermission>();
    Table ht = null;
    Admin ha = null;
    try {
      TableName aclTableName =
          TableName.valueOf(NamespaceDescriptor.SYSTEM_NAMESPACE_NAME_STR, "acl");
      ha = new HBaseAdmin(conf);
      ht = new HTable(conf, aclTableName.getName());
      CoprocessorRpcChannel service = ht.coprocessorService(HConstants.EMPTY_START_ROW);
      BlockingInterface protocol =
          AccessControlProtos.AccessControlService.newBlockingStub(service);
      HTableDescriptor[] htds = null;
     
      if (tableRegex != null) {
        htds = ha.listTables(Pattern.compile(tableRegex));
        for (HTableDescriptor hd: htds) {
          permList.addAll(ProtobufUtil.getUserPermissions(protocol, hd.getTableName()));
        }
      } else {
        permList = ProtobufUtil.getUserPermissions(protocol);
      }
    } finally {
      if (ht != null) {
        ht.close();
      }
      if (ha != null) {
        ha.close();
      }
    }
View Full Code Here

    if (LOG.isDebugEnabled()) {
      LOG.debug("Removing permissions of removed namespace "+ namespace);
    }

    Table acls = null;
    try {
      acls = new HTable(conf, ACL_TABLE_NAME);
      acls.delete(d);
    } finally {
      if (acls != null) acls.close();
    }
  }
View Full Code Here

    if (LOG.isDebugEnabled()) {
      LOG.debug("Removing permissions of removed column " + Bytes.toString(column) +
                " from table "+ tableName);
    }

    Table acls = null;
    try {
      acls = new HTable(conf, ACL_TABLE_NAME);

      Scan scan = new Scan();
      scan.addFamily(ACL_LIST_FAMILY);

      String columnName = Bytes.toString(column);
      scan.setFilter(new QualifierFilter(CompareOp.EQUAL, new RegexStringComparator(
                     String.format("(%s%s%s)|(%s%s)$",
                     ACL_KEY_DELIMITER, columnName, ACL_KEY_DELIMITER,
                     ACL_KEY_DELIMITER, columnName))));

      Set<byte[]> qualifierSet = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
      ResultScanner scanner = acls.getScanner(scan);
      try {
        for (Result res : scanner) {
          for (byte[] q : res.getFamilyMap(ACL_LIST_FAMILY).navigableKeySet()) {
            qualifierSet.add(q);
          }
        }
      } finally {
        scanner.close();
      }

      if (qualifierSet.size() > 0) {
        Delete d = new Delete(tableName.getName());
        for (byte[] qualifier : qualifierSet) {
          d.deleteColumns(ACL_LIST_FAMILY, qualifier);
        }
        acls.delete(d);
      }
    } finally {
      if (acls != null) acls.close();
    }
  }
View Full Code Here

    // do a full scan of _acl_, filtering on only first table region rows

    Scan scan = new Scan();
    scan.addFamily(ACL_LIST_FAMILY);

    Table acls = null;
    ResultScanner scanner = null;
    try {
      acls = new HTable(conf, ACL_TABLE_NAME);
      scanner = acls.getScanner(scan);
      for (Result row : scanner) {
        ListMultimap<String,TablePermission> resultPerms =
            parsePermissions(row.getRow(), row);
        allPerms.put(row.getRow(), resultPerms);
      }
    } finally {
      if (scanner != null) scanner.close();
      if (acls != null) acls.close();
    }

    return allPerms;
  }
View Full Code Here

      byte[] entryName) throws IOException {
    if (entryName == null) entryName = ACL_TABLE_NAME.getName();

    // for normal user tables, we just read the table row from _acl_
    ListMultimap<String, TablePermission> perms = ArrayListMultimap.create();
    Table acls = null;
    try {
      acls = new HTable(conf, ACL_TABLE_NAME);
      Get get = new Get(entryName);
      get.addFamily(ACL_LIST_FAMILY);
      Result row = acls.get(get);
      if (!row.isEmpty()) {
        perms = parsePermissions(entryName, row);
      } else {
        LOG.info("No permissions found in " + ACL_TABLE_NAME + " for acl entry "
            + Bytes.toString(entryName));
      }
    } finally {
      if (acls != null) acls.close();
    }

    return perms;
  }
View Full Code Here

    private void monitorRegionServers(Map<String, List<HRegionInfo>> rsAndRMap) {
      String serverName = null;
      String tableName = null;
      HRegionInfo region = null;
      Table table = null;
      Get get = null;
      byte[] startKey = null;
      Scan scan = null;
      StopWatch stopWatch = new StopWatch();
      // monitor one region on every region server
      for (Map.Entry<String, List<HRegionInfo>> entry : rsAndRMap.entrySet()) {
        stopWatch.reset();
        serverName = entry.getKey();
        // always get the first region
        region = entry.getValue().get(0);
        try {
          tableName = region.getTable().getNameAsString();
          table = new HTable(this.admin.getConfiguration(), tableName);
          startKey = region.getStartKey();
          // Can't do a get on empty start row so do a Scan of first element if any instead.
          if(startKey.length > 0) {
            get = new Get(startKey);
            stopWatch.start();
            table.get(get);
            stopWatch.stop();
          } else {
            scan = new Scan();
            scan.setCaching(1);
            scan.setMaxResultSize(1L);
            stopWatch.start();
            ResultScanner s = table.getScanner(scan);
            s.close();
            stopWatch.stop();
          }
          this.getSink().publishReadTiming(tableName, serverName, stopWatch.getTime());
        } catch (TableNotFoundException tnfe) {
          // This is ignored because it doesn't imply that the regionserver is dead
        } catch (TableNotEnabledException tnee) {
          // This is considered a success since we got a response.
          LOG.debug("The targeted table was disabled.  Assuming success.");
        } catch (DoNotRetryIOException dnrioe) {
            this.getSink().publishReadFailure(tableName, serverName);
            LOG.error(dnrioe);
        } catch (IOException e) {
          this.getSink().publishReadFailure(tableName, serverName);
          LOG.error(e);
          this.errorCode = ERROR_EXIT_CODE;
        } finally {
          if (table != null) {
            try {
              table.close();
            } catch (IOException e) {/* DO NOTHING */
            }
          }
          scan = null;
          get = null;
View Full Code Here

  }

  // @Ignore @Test
  public void testBulkDeleteEndpoint() throws Throwable {
    byte[] tableName = Bytes.toBytes("testBulkDeleteEndpoint");
    Table ht = createTable(tableName);
    List<Put> puts = new ArrayList<Put>(100);
    for (int j = 0; j < 100; j++) {
      byte[] rowkey = Bytes.toBytes(j);
      puts.add(createPut(rowkey, "v1"));
    }
    ht.put(puts);
    // Deleting all the rows.
    long noOfRowsDeleted = invokeBulkDeleteProtocol(tableName, new Scan(), 5, DeleteType.ROW, null);
    assertEquals(100, noOfRowsDeleted);

    int rows = 0;
    for (Result result : ht.getScanner(new Scan())) {
      rows++;
    }
    assertEquals(0, rows);
    ht.close();
  }
View Full Code Here

TOP

Related Classes of org.apache.hadoop.hbase.client.Table

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.