org.apache.hadoop.hbase.client.Connection Java Examples

The following examples show how to use org.apache.hadoop.hbase.client.Connection. 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: ProcessRecordUpdater.java    From hraven with Apache License 2.0 6 votes vote down vote up
@Override
public Boolean call() throws Exception {

  ProcessRecord updatedRecord = null;
  Connection hbaseConnection = null;
  try {
    hbaseConnection = ConnectionFactory.createConnection(hbaseConf);
    // Connect only when needed.
    ProcessRecordService processRecordService =
        new ProcessRecordService(hbaseConf, hbaseConnection);

    updatedRecord =
        processRecordService.setProcessState(processRecord, newState);
  } finally {
    if (hbaseConnection != null) {
      hbaseConnection.close();
    }
  }

  if ((updatedRecord != null)
      && (updatedRecord.getProcessState() == newState)) {
    return true;
  }
  return false;
}
 
Example #2
Source File: BigtableStorage.java    From styx with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
BigtableStorage(Connection connection,
                ExecutorService executor,
                WaitStrategy waitStrategy,
                StopStrategy retryStopStrategy) {
  this.connection = Objects.requireNonNull(connection, "connection");

  this.executor = MDCUtil.withMDC(Context.currentContextExecutor(
      register(closer, Objects.requireNonNull(executor, "executor"), "bigtable-storage")));

  retryer = RetryerBuilder.<Void>newBuilder()
      .retryIfExceptionOfType(IOException.class)
      .withWaitStrategy(Objects.requireNonNull(waitStrategy, "waitStrategy"))
      .withStopStrategy(Objects.requireNonNull(retryStopStrategy, "retryStopStrategy"))
      .withRetryListener(BigtableStorage::onRequestAttempt)
      .build();
}
 
Example #3
Source File: CheckPoint.java    From antsdb with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void readFromHBase(Connection conn) throws IOException {
    // Get table object
    Table table = conn.getTable(this.tn);
    
    // Query row
    Get get = new Get(KEY);
    Result result = table.get(get);
    if (!result.isEmpty()) {
        this.currentSp = Bytes.toLong(get(result, "currentSp"));
        this.serverId = Bytes.toLong(get(result, "serverId"));
        this.createTimestamp = Optional.ofNullable(get(result, "createTimestamp")).map(Bytes::toLong).orElse(0l);
        this.updateTimestamp = Optional.ofNullable(get(result, "updateTimestamp")).map(Bytes::toLong).orElse(0l);
        this.createOrcaVersion = Bytes.toString(get(result, "createOrcaVersion"));
        this.updateorcaVersion = Bytes.toString(get(result, "updateOrcaVersion"));
        this.isActive = Optional.ofNullable(get(result, "isActive")).map(Bytes::toBoolean).orElse(Boolean.FALSE);
    }
}
 
Example #4
Source File: TestCellACLWithMultipleVersions.java    From hbase with Apache License 2.0 6 votes vote down vote up
private void verifyUserDeniedForDeleteExactVersion(final User user, final byte[] row,
    final byte[] q1, final byte[] q2) throws IOException, InterruptedException {
  user.runAs(new PrivilegedExceptionAction<Void>() {
    @Override
    public Void run() throws Exception {
      try (Connection connection = ConnectionFactory.createConnection(conf)) {
        try (Table t = connection.getTable(testTable.getTableName())) {
          Delete d = new Delete(row, 127);
          d.addColumns(TEST_FAMILY1, q1);
          d.addColumns(TEST_FAMILY1, q2);
          d.addFamily(TEST_FAMILY2, 129);
          t.delete(d);
          fail(user.getShortName() + " can not do the delete");
        } catch (Exception e) {

        }
      }
      return null;
    }
  });
}
 
Example #5
Source File: CleanHtableCLI.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private void clean() throws IOException {
    Connection conn = HBaseConnection.get(KylinConfig.getInstanceFromEnv().getStorageUrl());
    Admin hbaseAdmin = conn.getAdmin();

    for (HTableDescriptor descriptor : hbaseAdmin.listTables()) {
        String name = descriptor.getNameAsString().toLowerCase(Locale.ROOT);
        if (name.startsWith("kylin") || name.startsWith("_kylin")) {
            String x = descriptor.getValue(IRealizationConstants.HTableTag);
            System.out.println("table name " + descriptor.getNameAsString() + " host: " + x);
            System.out.println(descriptor);
            System.out.println();

            descriptor.setValue(IRealizationConstants.HTableOwner, "[email protected]");
            hbaseAdmin.modifyTable(TableName.valueOf(descriptor.getNameAsString()), descriptor);
        }
    }
    hbaseAdmin.close();
}
 
Example #6
Source File: JobFileProcessor.java    From hraven with Apache License 2.0 6 votes vote down vote up
/**
 * @param conf used to communicate arguments to the running jobs.
 * @param hbaseConnection used to create Table references connecting to HBase.
 * @param cluster for which we are processing
 * @param reprocess Reprocess those records that may have been processed
 *          already. Otherwise successfully processed job files are skipped.
 * @param batchSize the total number of jobs to process in a batch (a MR job
 *          scanning these many records in the raw table).
 * @param minJobId used to start the scan. If null then there is no min limit
 *          on JobId.
 * @param maxJobId used to end the scan (inclusive). If null then there is no
 *          max limit on jobId.
 * @throws IOException
 * @throws InterruptedException
 * @throws ClassNotFoundException
 * @throws ExecutionException
 * @throws RowKeyParseException
 */
private List<JobRunner> getJobRunners(Configuration conf, Connection hbaseConnection, String cluster,
    boolean reprocess, int batchSize, String minJobId, String maxJobId)
    throws IOException, InterruptedException, ClassNotFoundException,
    RowKeyParseException {
  List<JobRunner> jobRunners = new LinkedList<JobRunner>();

  JobHistoryRawService jobHistoryRawService = new JobHistoryRawService(hbaseConnection);

  // Bind all MR jobs together with one runID.
  long now = System.currentTimeMillis();
  conf.setLong(Constants.MR_RUN_CONF_KEY, now);

  List<Scan> scanList = jobHistoryRawService.getHistoryRawTableScans(cluster,
      minJobId, maxJobId, reprocess, batchSize);

  for (Scan scan : scanList) {
    Job job = getProcessingJob(conf, scan, scanList.size());

    JobRunner jobRunner = new JobRunner(job, null);
    jobRunners.add(jobRunner);
  }

  return jobRunners;

}
 
Example #7
Source File: IndexSplitTransaction.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private void offlineParentInMetaAndputMetaEntries(Connection conn,
    HRegionInfo parent, HRegionInfo splitA, HRegionInfo splitB,
    ServerName serverName, List<Mutation> metaEntries) throws IOException {
  List<Mutation> mutations = metaEntries;
  HRegionInfo copyOfParent = new HRegionInfo(parent);
  copyOfParent.setOffline(true);
  copyOfParent.setSplit(true);

  //Put for parent
  Put putParent = MetaTableAccessor.makePutFromRegionInfo(copyOfParent);
  MetaTableAccessor.addDaughtersToPut(putParent, splitA, splitB);
  mutations.add(putParent);
  
  //Puts for daughters
  Put putA = MetaTableAccessor.makePutFromRegionInfo(splitA);
  Put putB = MetaTableAccessor.makePutFromRegionInfo(splitB);

  addLocation(putA, serverName, 1); //these are new regions, openSeqNum = 1 is fine.
  addLocation(putB, serverName, 1);
  mutations.add(putA);
  mutations.add(putB);
  MetaTableAccessor.mutateMetaTable(conn, mutations);
}
 
Example #8
Source File: SystemCatalogWALEntryFilterIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public WAL.Entry getEntry(TableName tableName, Get get) throws IOException {
  WAL.Entry entry = null;
  try(Connection conn = ConnectionFactory.createConnection(getUtility().getConfiguration())){
    Table htable = conn.getTable(tableName);
    Result result = htable.get(get);
    WALEdit edit = new WALEdit();
    if (result != null) {
      List<Cell> cellList = result.listCells();
      Assert.assertNotNull("Didn't retrieve any cells from SYSTEM.CATALOG", cellList);
      for (Cell c : cellList) {
        edit.add(c);
      }
    }
    Assert.assertTrue("Didn't retrieve any cells from SYSTEM.CATALOG",
        edit.getCells().size() > 0);
    WALKeyImpl key = new WALKeyImpl(REGION, tableName, 0, 0, uuid);
    entry = new WAL.Entry(key, edit);
  }
  return entry;
}
 
Example #9
Source File: PerformanceEvaluation.java    From hbase with Apache License 2.0 6 votes vote down vote up
private void runTest(final Class<? extends TestBase> cmd, TestOptions opts) throws IOException,
    InterruptedException, ClassNotFoundException, ExecutionException {
  // Log the configuration we're going to run with. Uses JSON mapper because lazy. It'll do
  // the TestOptions introspection for us and dump the output in a readable format.
  LOG.info(cmd.getSimpleName() + " test run options=" + GSON.toJson(opts));
  Admin admin = null;
  Connection connection = null;
  try {
    connection = ConnectionFactory.createConnection(getConf());
    admin = connection.getAdmin();
    checkTable(admin, opts);
  } finally {
    if (admin != null) admin.close();
    if (connection != null) connection.close();
  }
  if (opts.nomapred) {
    doLocalClients(opts, getConf());
  } else {
    doMapReduce(opts, getConf());
  }
}
 
Example #10
Source File: HFileGenerationFunction.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
private static InetSocketAddress getFavoredNode(BulkImportPartition partition) throws IOException {
    InetSocketAddress favoredNode = null;
    SConfiguration configuration = HConfiguration.getConfiguration();
    Connection connection = HBaseConnectionFactory.getInstance(configuration).getConnection();

    String regionName = partition.getRegionName();
    HRegionLocation regionLocation = MetaTableAccessor.getRegionLocation(connection,
            com.splicemachine.primitives.Bytes.toBytesBinary(regionName));

    if (regionLocation != null) {
        String hostname = regionLocation.getHostname();
        int port = regionLocation.getPort();

        InetSocketAddress address = new InetSocketAddress(hostname, port);
        if (!address.isUnresolved()) {
            favoredNode = address;
        } else {
            SpliceLogUtils.info(LOG, "Cannot resolve host %s to achieve better data locality.", hostname);
        }

    }
    else {
        SpliceLogUtils.info(LOG, "Cannot to get region location %s to achieve better data locality.", regionName);
    }
    return favoredNode;
}
 
Example #11
Source File: SecureTestUtil.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Grant permissions on a namespace to the given user using AccessControl Client.
 * Will wait until all active AccessController instances have updated their permissions caches
 * or will throw an exception upon timeout (10 seconds).
 */
public static void grantOnNamespaceUsingAccessControlClient(final HBaseTestingUtility util,
    final Connection connection, final String user, final String namespace,
    final Permission.Action... actions) throws Exception {
  SecureTestUtil.updateACLs(util, new Callable<Void>() {
    @Override
    public Void call() throws Exception {
      try {
        AccessControlClient.grant(connection, namespace, user, actions);
      } catch (Throwable t) {
        LOG.error("grant failed: ", t);
      }
      return null;
    }
  });
}
 
Example #12
Source File: Reads.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
public static void readPrefix(String projectId, String instanceId, String tableId) {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
    Table table = connection.getTable(TableName.valueOf(tableId));
    Scan prefixScan = new Scan().setRowPrefixFilter(Bytes.toBytes("phone"));
    ResultScanner rows = table.getScanner(prefixScan);

    for (Result row : rows) {
      printRow(row);
    }
  } catch (IOException e) {
    System.out.println(
        "Unable to initialize service client, as a network error occurred: \n" + e.toString());
  }
}
 
Example #13
Source File: HfileBulkExporter.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
/**
 * Do hfile bulk exporting
 * 
 * @param builder
 * @throws Exception
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void doExporting(CommandLine line) throws Exception {
	// Configuration.
	String tabname = line.getOptionValue("tabname");
	String user = line.getOptionValue("user");
	Configuration conf = new Configuration();
	conf.set("hbase.zookeeper.quorum", line.getOptionValue("zkaddr"));
	conf.set("hbase.fs.tmp.dir", line.getOptionValue("T", DEFAULT_HBASE_MR_TMPDIR));
	conf.set(TableInputFormat.INPUT_TABLE, tabname);
	conf.set(TableInputFormat.SCAN_BATCHSIZE, line.getOptionValue("batchSize", DEFAULT_SCAN_BATCH_SIZE));

	// Check directory.
	String outputDir = line.getOptionValue("output", DEFAULT_HFILE_OUTPUT_DIR) + "/" + tabname;
	FileSystem fs = FileSystem.get(new URI(outputDir), new Configuration(), user);
	state(!fs.exists(new Path(outputDir)), format("HDFS temporary directory already has data, path: '%s'", outputDir));

	// Set scan condition.(if necessary)
	setScanIfNecessary(conf, line);

	// Job.
	Connection conn = ConnectionFactory.createConnection(conf);
	TableName tab = TableName.valueOf(tabname);
	Job job = Job.getInstance(conf);
	job.setJobName(HfileBulkExporter.class.getSimpleName() + "@" + tab.getNameAsString());
	job.setJarByClass(HfileBulkExporter.class);
	job.setMapperClass((Class<Mapper>) ClassUtils.getClass(line.getOptionValue("mapperClass", DEFAULT_MAPPER_CLASS)));
	job.setInputFormatClass(TableInputFormat.class);
	job.setMapOutputKeyClass(ImmutableBytesWritable.class);
	job.setMapOutputValueClass(Put.class);

	HFileOutputFormat2.configureIncrementalLoad(job, conn.getTable(tab), conn.getRegionLocator(tab));
	FileOutputFormat.setOutputPath(job, new Path(outputDir));
	if (job.waitForCompletion(true)) {
		long total = job.getCounters().findCounter(DEFUALT_COUNTER_GROUP, DEFUALT_COUNTER_TOTAL).getValue();
		long processed = job.getCounters().findCounter(DEFUALT_COUNTER_GROUP, DEFUALT_COUNTER_PROCESSED).getValue();
		log.info(String.format("Exported to successfully! with processed:(%d)/total:(%d)", processed, total));
	}

}
 
Example #14
Source File: VisibilityClient.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve the list of visibility labels defined in the system.
 * @param connection The Connection instance to use.
 * @param regex  The regular expression to filter which labels are returned.
 * @return labels The list of visibility labels defined in the system.
 * @throws Throwable
 */
public static ListLabelsResponse listLabels(Connection connection, final String regex)
    throws Throwable {
  try (Table table = connection.getTable(LABELS_TABLE_NAME)) {
    Batch.Call<VisibilityLabelsService, ListLabelsResponse> callable =
        new Batch.Call<VisibilityLabelsService, ListLabelsResponse>() {
      ServerRpcController controller = new ServerRpcController();
      CoprocessorRpcUtils.BlockingRpcCallback<ListLabelsResponse> rpcCallback =
          new CoprocessorRpcUtils.BlockingRpcCallback<>();

      @Override
      public ListLabelsResponse call(VisibilityLabelsService service) throws IOException {
        ListLabelsRequest.Builder listAuthLabelsReqBuilder = ListLabelsRequest.newBuilder();
        if (regex != null) {
          // Compile the regex here to catch any regex exception earlier.
          Pattern pattern = Pattern.compile(regex);
          listAuthLabelsReqBuilder.setRegex(pattern.toString());
        }
        service.listLabels(controller, listAuthLabelsReqBuilder.build(), rpcCallback);
        ListLabelsResponse response = rpcCallback.get();
        if (controller.failedOnException()) {
          throw controller.getFailedOn();
        }
        return response;
      }
    };
    Map<byte[], ListLabelsResponse> result =
        table.coprocessorService(VisibilityLabelsService.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.
  }
}
 
Example #15
Source File: TestSlowLogAccessor.java    From hbase with Apache License 2.0 5 votes vote down vote up
private int getTableCount(Connection connection) {
  try (Table table = connection.getTable(SlowLogTableAccessor.SLOW_LOG_TABLE_NAME)) {
    ResultScanner resultScanner = table.getScanner(new Scan().setReadType(Scan.ReadType.STREAM));
    int count = 0;
    for (Result result : resultScanner) {
      ++count;
    }
    return count;
  } catch (Exception e) {
    return 0;
  }
}
 
Example #16
Source File: MajorCompactionRequest.java    From hbase with Apache License 2.0 5 votes vote down vote up
Set<String> getStoresRequiringCompaction(Set<String> requestedStores, long timestamp)
    throws IOException {
  try(Connection connection = getConnection(configuration)) {
    HRegionFileSystem fileSystem = getFileSystem(connection);
    Set<String> familiesToCompact = Sets.newHashSet();
    for (String family : requestedStores) {
      if (shouldCFBeCompacted(fileSystem, family, timestamp)) {
        familiesToCompact.add(family);
      }
    }
    return familiesToCompact;
  }
}
 
Example #17
Source File: GridTableHBaseBenchmark.java    From kylin with Apache License 2.0 5 votes vote down vote up
private static void prepareData(Connection conn) throws IOException {
    Table table = conn.getTable(TableName.valueOf(TEST_TABLE));

    try {
        // check how many rows existing
        int nRows = 0;
        Scan scan = new Scan();
        scan.setFilter(new KeyOnlyFilter());
        ResultScanner scanner = table.getScanner(scan);
        for (Result r : scanner) {
            r.getRow(); // nothing to do
            nRows++;
        }

        if (nRows > 0) {
            logger.info("{} existing rows", nRows);
            if (nRows != N_ROWS)
                throw new IOException("Expect " + N_ROWS + " rows but it is not");
            return;
        }

        // insert rows into empty table
        logger.info("Writing {} rows to {}", N_ROWS, TEST_TABLE);
        long nBytes = 0;
        for (int i = 0; i < N_ROWS; i++) {
            byte[] rowkey = Bytes.toBytes(i);
            Put put = new Put(rowkey);
            byte[] cell = randomBytes();
            put.addColumn(CF, QN, cell);
            table.put(put);
            nBytes += cell.length;
            dot(i, N_ROWS);
        }
        logger.info("Written {} rows, {} bytes", N_ROWS, nBytes);

    } finally {
        IOUtils.closeQuietly(table);
    }

}
 
Example #18
Source File: SecureTestUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Revoke permissions globally from the given user. Will wait until all active
 * AccessController instances have updated their permissions caches or will
 * throw an exception upon timeout (10 seconds).
 */
public static void revokeGlobal(final HBaseTestingUtility util, final String user,
    final Permission.Action... actions) throws Exception {
  SecureTestUtil.updateACLs(util, new Callable<Void>() {
    @Override
    public Void call() throws Exception {
      try (Connection connection = ConnectionFactory.createConnection(util.getConfiguration())) {
        connection.getAdmin().revoke(
          new UserPermission(user, Permission.newBuilder().withActions(actions).build()));
      }
      return null;
    }
  });
}
 
Example #19
Source File: SecureTestUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Revoke permissions globally from the given user. Will wait until all active
 * AccessController instances have updated their permissions caches or will
 * throw an exception upon timeout (10 seconds).
 */
public static void revokeGlobal(final User caller, final HBaseTestingUtility util,
    final String user, final Permission.Action... actions) throws Exception {
  SecureTestUtil.updateACLs(util, new Callable<Void>() {
    @Override
    public Void call() throws Exception {
      Configuration conf = util.getConfiguration();
      try (Connection connection = ConnectionFactory.createConnection(conf, caller)) {
        connection.getAdmin().revoke(
          new UserPermission(user, Permission.newBuilder().withActions(actions).build()));
      }
      return null;
    }
  });
}
 
Example #20
Source File: HBaseUtil.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void closeQuietly(Connection conn) {
    try {
        if (conn != null) {
            conn.close();
        }
    }
    catch (Exception ignored) {}
}
 
Example #21
Source File: QuotaTableUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the current space quota snapshot of the given {@code tableName} from
 * {@code QuotaTableUtil.QUOTA_TABLE_NAME} or null if the no quota information is available for
 * that tableName.
 * @param conn connection to re-use
 * @param tableName name of the table whose current snapshot is to be retreived
 */
public static SpaceQuotaSnapshot getCurrentSnapshotFromQuotaTable(Connection conn,
    TableName tableName) throws IOException {
  try (Table quotaTable = conn.getTable(QuotaTableUtil.QUOTA_TABLE_NAME)) {
    Map<TableName, SpaceQuotaSnapshot> snapshots = new HashMap<>(1);
    Result result = quotaTable.get(makeQuotaSnapshotGetForTable(tableName));
    // if we don't have any row corresponding to this get, return null
    if (result.isEmpty()) {
      return null;
    }
    // otherwise, extract quota snapshot in snapshots object
    extractQuotaSnapshot(result, snapshots);
    return snapshots.get(tableName);
  }
}
 
Example #22
Source File: TableInputFormat.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
protected Pair<byte[][], byte[][]> getStartEndKeys() throws IOException {
  if (conf.get(SPLIT_TABLE) != null) {
    TableName splitTableName = TableName.valueOf(conf.get(SPLIT_TABLE));
    try (Connection conn = ConnectionFactory.createConnection(getConf())) {
      try (RegionLocator rl = conn.getRegionLocator(splitTableName)) {
        return rl.getStartEndKeys();
      }
    }
  }

  return super.getStartEndKeys();
}
 
Example #23
Source File: TestVisibilityLabelsWithDeletes.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteFamilyWithoutCellVisibilityWithMulipleVersions() throws Exception {
  setAuths();
  final TableName tableName = TableName.valueOf(testName.getMethodName());
  try (Table table = doPutsWithoutVisibility(tableName)) {
    TEST_UTIL.getAdmin().flush(tableName);
    PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
      @Override
      public Void run() throws Exception {
        try (Connection connection = ConnectionFactory.createConnection(conf);
          Table table = connection.getTable(tableName)) {
          Delete d = new Delete(row1);
          d.addFamily(fam);
          table.delete(d);
        } catch (Throwable t) {
          throw new IOException(t);
        }
        return null;
      }
    };
    SUPERUSER.runAs(actiona);

    TEST_UTIL.getAdmin().flush(tableName);
    Scan s = new Scan();
    s.readVersions(5);
    s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
    ResultScanner scanner = table.getScanner(s);
    Result[] next = scanner.next(3);
    assertTrue(next.length == 1);
    // All cells wrt row1 should be deleted as we are not passing the Cell Visibility
    CellScanner cellScanner = next[0].cellScanner();
    cellScanner.advance();
    Cell current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
      row2, 0, row2.length));
  }
}
 
Example #24
Source File: GridTableHBaseBenchmark.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
private static void createHTableIfNeeded(Connection conn, String tableName) throws IOException {
    Admin hbase = conn.getAdmin();

    try {
        boolean tableExist = false;
        try {
            hbase.getTableDescriptor(TableName.valueOf(tableName));
            tableExist = true;
        } catch (TableNotFoundException e) {
            //do nothing?
        }

        if (tableExist) {
            logger.info("HTable '{}' already exists", tableName);
            return;
        }

        logger.info("Creating HTable '{}'", tableName);

        HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));

        HColumnDescriptor fd = new HColumnDescriptor(CF);
        fd.setBlocksize(CELL_SIZE);
        desc.addFamily(fd);
        hbase.createTable(desc);

        logger.info("HTable '{}' created", tableName);
    } finally {
        hbase.close();
    }
}
 
Example #25
Source File: HBaseUtilDataComparer.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
public HBaseUtilDataComparer(Orca orca, Connection hbaseConn, int testRows, 
		String[] skipTables, String[] syncTables, boolean ignoreError) {
	this.orca = orca;
	this.hbaseConn = hbaseConn;
	this.testRows = testRows;
	this.humpback = orca.getHumpback();
	this.metaService = orca.getMetaService();
	// get skip tables list
	if (skipTables != null && !skipTables[0].isEmpty()) this.skipTables = skipTables;	
	if (syncTables != null && !syncTables[0].isEmpty()) this.syncTables = syncTables;

	this.ignoreError = ignoreError;
}
 
Example #26
Source File: MetaTableAccessor.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch table state for given table from META table
 * @param conn connection to use
 * @param tableName table to fetch state for
 */
@Nullable
public static TableState getTableState(Connection conn, TableName tableName) throws IOException {
  if (tableName.equals(TableName.META_TABLE_NAME)) {
    return new TableState(tableName, TableState.State.ENABLED);
  }
  Table metaHTable = getMetaHTable(conn);
  Get get = new Get(tableName.getName()).addColumn(HConstants.TABLE_FAMILY,
    HConstants.TABLE_STATE_QUALIFIER);
  Result result = metaHTable.get(get);
  return CatalogFamilyFormat.getTableState(result);
}
 
Example #27
Source File: HBaseRowInputFormat.java    From flink with Apache License 2.0 5 votes vote down vote up
private void connectToTable() {
	try {
		Connection conn = ConnectionFactory.createConnection(getHadoopConfiguration());
		super.table = (HTable) conn.getTable(TableName.valueOf(tableName));
	} catch (TableNotFoundException tnfe) {
		LOG.error("The table " + tableName + " not found ", tnfe);
		throw new RuntimeException("HBase table '" + tableName + "' not found.", tnfe);
	} catch (IOException ioe) {
		LOG.error("Exception while creating connection to HBase.", ioe);
		throw new RuntimeException("Cannot create connection to HBase.", ioe);
	}
}
 
Example #28
Source File: TestVisibilityLablesWithGroups.java    From hbase with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setupBeforeClass() throws Exception {
  // setup configuration
  conf = TEST_UTIL.getConfiguration();
  VisibilityTestUtil.enableVisiblityLabels(conf);
  // Not setting any SLG class. This means to use the default behavior.
  // Use a group as the super user.
  conf.set("hbase.superuser", "@supergroup");
  TEST_UTIL.startMiniCluster(1);
  // 'admin' has super user permission because it is part of the 'supergroup'
  SUPERUSER = User.createUserForTesting(conf, "admin", new String[] { "supergroup" });
  // 'test' user will inherit 'testgroup' visibility labels
  TESTUSER = User.createUserForTesting(conf, "test", new String[] {"testgroup" });

  // Wait for the labels table to become available
  TEST_UTIL.waitTableEnabled(LABELS_TABLE_NAME.getName(), 50000);

  // Set up for the test
  SUPERUSER.runAs(new PrivilegedExceptionAction<Void>() {
    @Override
    public Void run() throws Exception {
      try (Connection conn = ConnectionFactory.createConnection(conf)) {
        VisibilityClient.addLabels(conn, new String[] { SECRET, CONFIDENTIAL });
        // set auth for @testgroup
        VisibilityClient.setAuths(conn, new String[] { CONFIDENTIAL }, "@testgroup");
      } catch (Throwable t) {
        throw new IOException(t);
      }
      return null;
    }
  });
}
 
Example #29
Source File: HFileArchiveManager.java    From hbase with Apache License 2.0 5 votes vote down vote up
public HFileArchiveManager(Connection connection, Configuration conf)
    throws ZooKeeperConnectionException, IOException {
  this.zooKeeper = new ZKWatcher(conf, "hfileArchiveManager-on-" + connection.toString(),
      connection);
  this.archiveZnode = ZKTableArchiveClient.getArchiveZNode(this.zooKeeper.getConfiguration(),
    this.zooKeeper);
}
 
Example #30
Source File: TestAccessController.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testTableDeletion() throws Exception {
  User TABLE_ADMIN = User.createUserForTesting(conf, "TestUser", new String[0]);
  final TableName tableName = TableName.valueOf(name.getMethodName());
  createTestTable(tableName);

  // Grant TABLE ADMIN privs
  grantOnTable(TEST_UTIL, TABLE_ADMIN.getShortName(), tableName, null, null, Permission.Action.ADMIN);

  AccessTestAction deleteTableAction = new AccessTestAction() {
    @Override
    public Object run() throws Exception {
      Connection unmanagedConnection =
          ConnectionFactory.createConnection(TEST_UTIL.getConfiguration());
      Admin admin = unmanagedConnection.getAdmin();
      try {
        deleteTable(TEST_UTIL, admin, tableName);
      } finally {
        admin.close();
        unmanagedConnection.close();
      }
      return null;
    }
  };

  verifyDenied(deleteTableAction, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
    USER_GROUP_WRITE);
  verifyAllowed(deleteTableAction, TABLE_ADMIN);
}