Package org.apache.hadoop.hbase.client

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


   */
  protected void batch(TableName tableName, Collection<List<Row>> allRows) throws IOException {
    if (allRows.isEmpty()) {
      return;
    }
    Table table = null;
    try {
      table = this.sharedHtableCon.getTable(tableName);
      for (List<Row> rows : allRows) {
        table.batch(rows);
      }
    } catch (InterruptedException ix) {
      throw (InterruptedIOException)new InterruptedIOException().initCause(ix);
    } finally {
      if (table != null) {
        table.close();
      }
    }
  }
View Full Code Here


    if (tSplit.getTableName() == null) {
      throw new IOException("Cannot create a record reader because of a"
          + " previous error. Please look at the previous logs lines from"
          + " the task's full log for more details.");
    }
    Table table =
        new HTable(context.getConfiguration(), tSplit.getTableName());

    TableRecordReader trr = this.tableRecordReader;

    try {
      // if no table record reader was provided use default
      if (trr == null) {
        trr = new TableRecordReader();
      }
      Scan sc = tSplit.getScan();
      sc.setStartRow(tSplit.getStartRow());
      sc.setStopRow(tSplit.getEndRow());
      trr.setScan(sc);
      trr.setHTable(table);
    } catch (IOException ioe) {
      // If there is an exception make sure that all
      // resources are closed and released.
      table.close();
      trr.close();
      throw ioe;
    }
    return trr;
  }
View Full Code Here

  @Override
  public void init() {
    // Reading all the labels and ordinal.
    // This scan should be done by user with global_admin previliges.. Ensure that it works
    Table labelsTable = null;
    try {
      labelsTable = new HTable(conf, LABELS_TABLE_NAME);
    } catch (TableNotFoundException e) {
      // Just return with out doing any thing. When the VC is not used we wont be having 'labels'
      // table in the cluster.
      return;
    } catch (IOException e) {
      LOG.error("Error opening 'labels' table", e);
      return;
    }
    Scan scan = new Scan();
    scan.setAuthorizations(new Authorizations(VisibilityUtils.SYSTEM_LABEL));
    scan.addColumn(LABELS_TABLE_FAMILY, LABEL_QUALIFIER);
    ResultScanner scanner = null;
    try {
      scanner = labelsTable.getScanner(scan);
      Result next = null;
      while ((next = scanner.next()) != null) {
        byte[] row = next.getRow();
        byte[] value = next.getValue(LABELS_TABLE_FAMILY, LABEL_QUALIFIER);
        labels.put(Bytes.toString(value), Bytes.toInt(row));
      }
    } catch (IOException e) {
      LOG.error("Error reading 'labels' table", e);
    } finally {
      try {
        if (scanner != null) {
          scanner.close();
        }
      } finally {
        try {
          labelsTable.close();
        } catch (IOException e) {
          LOG.warn("Error on closing 'labels' table", e);
        }
      }
    }
View Full Code Here

   * @param conf The configuration for connecting to the cluster
   * @return the authentication token instance
   */
  public static Token<AuthenticationTokenIdentifier> obtainToken(
      Configuration conf) throws IOException {
    Table meta = null;
    try {
      meta = new HTable(conf, TableName.META_TABLE_NAME);
      CoprocessorRpcChannel rpcChannel = meta.coprocessorService(HConstants.EMPTY_START_ROW);
      AuthenticationProtos.AuthenticationService.BlockingInterface service =
          AuthenticationProtos.AuthenticationService.newBlockingStub(rpcChannel);
      AuthenticationProtos.GetAuthenticationTokenResponse response = service.getAuthenticationToken(null,
          AuthenticationProtos.GetAuthenticationTokenRequest.getDefaultInstance());

      return ProtobufUtil.toToken(response.getToken());
    } catch (ServiceException se) {
      ProtobufUtil.toIOException(se);
    } finally {
      if (meta != null) {
        meta.close();
      }
    }
    // dummy return for ServiceException catch block
    return null;
  }
View Full Code Here

          public Void connect(HConnection conn) throws IOException {
            String zkClusterKey = conf.get(NAME + ".peerQuorumAddress");
            Configuration peerConf = HBaseConfiguration.create(conf);
            ZKUtil.applyClusterKeyToConf(peerConf, zkClusterKey);

            Table replicatedTable = new HTable(peerConf, conf.get(NAME + ".tableName"));
            scan.setStartRow(value.getRow());
            replicatedScanner = replicatedTable.getScanner(scan);
            return null;
          }
        });
      }
      Result res = replicatedScanner.next();
View Full Code Here

   * @return VisibilityLabelsResponse
   * @throws Throwable
   */
  public static VisibilityLabelsResponse addLabels(Configuration conf, final String[] labels)
      throws Throwable {
    Table ht = null;
    try {
      ht = new HTable(conf, LABELS_TABLE_NAME.getName());
      Batch.Call<VisibilityLabelsService, VisibilityLabelsResponse> callable =
          new Batch.Call<VisibilityLabelsService, VisibilityLabelsResponse>() {
        ServerRpcController controller = new ServerRpcController();
        BlockingRpcCallback<VisibilityLabelsResponse> rpcCallback =
            new BlockingRpcCallback<VisibilityLabelsResponse>();

        public VisibilityLabelsResponse call(VisibilityLabelsService service) throws IOException {
          VisibilityLabelsRequest.Builder builder = VisibilityLabelsRequest.newBuilder();
          for (String label : labels) {
            if (label.length() > 0) {
              VisibilityLabel.Builder newBuilder = VisibilityLabel.newBuilder();
              newBuilder.setLabel(ByteStringer.wrap(Bytes.toBytes(label)));
              builder.addVisLabel(newBuilder.build());
            }
          }
          service.addLabels(controller, builder.build(), rpcCallback);
          return rpcCallback.get();
        }
      };
      Map<byte[], VisibilityLabelsResponse> result = ht.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.
    } finally {
      if (ht != null) {
        ht.close();
      }
    }
  }
View Full Code Here

   * @param user
   * @return labels, the given user is globally authorized for.
   * @throws Throwable
   */
  public static GetAuthsResponse getAuths(Configuration conf, final String user) throws Throwable {
    Table ht = null;
    try {
      ht = new HTable(conf, LABELS_TABLE_NAME.getName());
      Batch.Call<VisibilityLabelsService, GetAuthsResponse> callable =
          new Batch.Call<VisibilityLabelsService, GetAuthsResponse>() {
        ServerRpcController controller = new ServerRpcController();
        BlockingRpcCallback<GetAuthsResponse> rpcCallback =
            new BlockingRpcCallback<GetAuthsResponse>();

        public GetAuthsResponse call(VisibilityLabelsService service) throws IOException {
          GetAuthsRequest.Builder getAuthReqBuilder = GetAuthsRequest.newBuilder();
          getAuthReqBuilder.setUser(ByteStringer.wrap(Bytes.toBytes(user)));
          service.getAuths(controller, getAuthReqBuilder.build(), rpcCallback);
          return rpcCallback.get();
        }
      };
      Map<byte[], GetAuthsResponse> result = ht.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.
    } finally {
      if (ht != null) {
        ht.close();
      }
    }
  }
View Full Code Here

    return setOrClearAuths(conf, auths, user, false);
  }

  private static VisibilityLabelsResponse setOrClearAuths(Configuration conf, final String[] auths,
      final String user, final boolean setOrClear) throws IOException, ServiceException, Throwable {
    Table ht = null;
    try {
      ht = new HTable(conf, LABELS_TABLE_NAME.getName());
      Batch.Call<VisibilityLabelsService, VisibilityLabelsResponse> callable =
          new Batch.Call<VisibilityLabelsService, VisibilityLabelsResponse>() {
        ServerRpcController controller = new ServerRpcController();
        BlockingRpcCallback<VisibilityLabelsResponse> rpcCallback =
            new BlockingRpcCallback<VisibilityLabelsResponse>();

        public VisibilityLabelsResponse call(VisibilityLabelsService service) throws IOException {
          SetAuthsRequest.Builder setAuthReqBuilder = SetAuthsRequest.newBuilder();
          setAuthReqBuilder.setUser(ByteStringer.wrap(Bytes.toBytes(user)));
          for (String auth : auths) {
            if (auth.length() > 0) {
              setAuthReqBuilder.addAuth(ByteStringer.wrap(Bytes.toBytes(auth)));
            }
          }
          if (setOrClear) {
            service.setAuths(controller, setAuthReqBuilder.build(), rpcCallback);
          } else {
            service.clearAuths(controller, setAuthReqBuilder.build(), rpcCallback);
          }
          return rpcCallback.get();
        }
      };
      Map<byte[], VisibilityLabelsResponse> result = ht.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.
    } finally {
      if (ht != null) {
        ht.close();
      }
    }
  }
View Full Code Here

    desc.addFamily(new HColumnDescriptor(TEST_FAMILY));
    admin.createTable(desc, new byte[][]{ROWS[rowSeperator1], ROWS[rowSeperator2]});
    util.waitUntilAllRegionsAssigned(TEST_TABLE);
    admin.close();

    Table table = new HTable(conf, TEST_TABLE);
    for (int i = 0; i < ROWSIZE; i++) {
      Put put = new Put(ROWS[i]);
      put.add(TEST_FAMILY, TEST_QUALIFIER, Bytes.toBytes(i));
      table.put(put);
    }
    table.close();
  }
View Full Code Here

      });
  }

  @Test
  public void testAggregation() throws Throwable {
    Table table = new HTable(util.getConfiguration(), TEST_TABLE);
    Map<byte[], Long> results = sum(table, TEST_FAMILY, TEST_QUALIFIER,
      ROWS[0], ROWS[ROWS.length-1]);
    int sumResult = 0;
    int expectedResult = 0;
    for (Map.Entry<byte[], Long> e : results.entrySet()) {
      LOG.info("Got value "+e.getValue()+" for region "+Bytes.toStringBinary(e.getKey()));
      sumResult += e.getValue();
    }
    for (int i = 0; i < ROWSIZE; i++) {
      expectedResult += i;
    }
    assertEquals("Invalid result", expectedResult, sumResult);

    results.clear();

    // scan: for region 2 and region 3
    results = sum(table, TEST_FAMILY, TEST_QUALIFIER,
      ROWS[rowSeperator1], ROWS[ROWS.length-1]);
    sumResult = 0;
    expectedResult = 0;
    for (Map.Entry<byte[], Long> e : results.entrySet()) {
      LOG.info("Got value "+e.getValue()+" for region "+Bytes.toStringBinary(e.getKey()));
      sumResult += e.getValue();
    }
    for (int i = rowSeperator1; i < ROWSIZE; i++) {
      expectedResult += i;
    }
    assertEquals("Invalid result", expectedResult, sumResult);
    table.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.