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

The following examples show how to use org.apache.hadoop.hbase.client.Put#setCellVisibility() . These examples are extracted from open source projects. 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
private static Table createTableAndWriteDataWithLabels(TableName tableName, String... labelExps)
    throws Exception {
  Table table = null;
  try {
    table = TEST_UTIL.createTable(tableName, fam);
    int i = 1;
    List<Put> puts = new ArrayList<>(labelExps.length);
    for (String labelExp : labelExps) {
      Put put = new Put(Bytes.toBytes("row" + i));
      put.addColumn(fam, qual, HConstants.LATEST_TIMESTAMP, value);
      put.setCellVisibility(new CellVisibility(labelExp));
      puts.add(put);
      i++;
    }
    table.put(puts);
  } finally {
    if (table != null) {
      table.close();
    }
  }
  return table;
}
 
Example 2
Source Project: hbase   File: TestVisibilityLabels.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testLabelsWithIncrement() throws Throwable {
  TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  try (Table table = TEST_UTIL.createTable(tableName, fam)) {
    byte[] row1 = Bytes.toBytes("row1");
    byte[] val = Bytes.toBytes(1L);
    Put put = new Put(row1);
    put.addColumn(fam, qual, HConstants.LATEST_TIMESTAMP, val);
    put.setCellVisibility(new CellVisibility(SECRET + " & " + CONFIDENTIAL));
    table.put(put);
    Get get = new Get(row1);
    get.setAuthorizations(new Authorizations(SECRET));
    Result result = table.get(get);
    assertTrue(result.isEmpty());
    table.incrementColumnValue(row1, fam, qual, 2L);
    result = table.get(get);
    assertTrue(result.isEmpty());
    Increment increment = new Increment(row1);
    increment.addColumn(fam, qual, 2L);
    increment.setCellVisibility(new CellVisibility(SECRET));
    table.increment(increment);
    result = table.get(get);
    assertTrue(!result.isEmpty());
  }
}
 
Example 3
private Table createTableAndWriteDataWithLabels(long[] timestamp, String... labelExps)
    throws Exception {
  Table table = createTable(fam);
  int i = 1;
  List<Put> puts = new ArrayList<>(labelExps.length);
  for (String labelExp : labelExps) {
    Put put = new Put(Bytes.toBytes("row" + i));
    put.addColumn(fam, qual, timestamp[i - 1], value);
    put.setCellVisibility(new CellVisibility(labelExp));
    puts.add(put);
    table.put(put);
    TEST_UTIL.getAdmin().flush(table.getName());
    i++;
  }
  return table;
}
 
Example 4
@Override
protected void map(NullWritable key, NullWritable value, Context context) throws IOException,
    InterruptedException {
  String suffix = "/" + shortTaskId;
  int BLOCK_SIZE = (int) (recordsToWrite / 100);
  for (long i = 0; i < recordsToWrite;) {
    for (long idx = 0; idx < BLOCK_SIZE && i < recordsToWrite; idx++, i++) {
      int expIdx = rand.nextInt(BLOCK_SIZE) % VISIBILITY_EXPS_COUNT;
      String exp = VISIBILITY_EXPS[expIdx];
      byte[] row = Bytes.add(Bytes.toBytes(i), Bytes.toBytes(suffix), Bytes.toBytes(exp));
      Put p = new Put(row);
      p.addColumn(TEST_FAMILY, TEST_QUALIFIER, HConstants.EMPTY_BYTE_ARRAY);
      p.setCellVisibility(new CellVisibility(exp));
      getCounter(expIdx).increment(1);
      mutator.mutate(p);

      if (i % 100 == 0) {
        context.setStatus("Written " + i + "/" + recordsToWrite + " records");
        context.progress();
      }
    }
    // End of block, flush all of them before we start writing anything
    // pointing to these!
    mutator.flush();
  }
}
 
Example 5
Source Project: hbase   File: TestScannersWithLabels.java    License: Apache License 2.0 6 votes vote down vote up
private static int insertData(TableName tableName, String column, double prob)
    throws IOException {
  byte[] k = new byte[3];
  byte[][] famAndQf = CellUtil.parseColumn(Bytes.toBytes(column));

  List<Put> puts = new ArrayList<>(9);
  for (int i = 0; i < 9; i++) {
    Put put = new Put(Bytes.toBytes("row" + i));
    put.setDurability(Durability.SKIP_WAL);
    put.addColumn(famAndQf[0], famAndQf[1], k);
    put.setCellVisibility(new CellVisibility("(" + SECRET + "|" + CONFIDENTIAL + ")" + "&" + "!"
        + TOPSECRET));
    puts.add(put);
  }
  try (Table table = TEST_UTIL.getConnection().getTable(tableName)) {
    table.put(puts);
  }
  return puts.size();
}
 
Example 6
Source Project: geowave   File: HBaseDataIndexWriter.java    License: Apache License 2.0 6 votes vote down vote up
private RowMutations rowToMutation(final GeoWaveRow row) {
  final RowMutations mutation = new RowMutations(row.getDataId());
  for (final GeoWaveValue value : row.getFieldValues()) {
    final Put put = new Put(row.getDataId());
    // visibility is in the visibility column so no need to serialize it with the value
    put.addColumn(
        StringUtils.stringToBinary(ByteArrayUtils.shortToString(row.getAdapterId())),
        new byte[0],
        DataIndexUtils.serializeDataIndexValue(value, false));
    if ((value.getVisibility() != null) && (value.getVisibility().length > 0)) {
      put.setCellVisibility(
          new CellVisibility(StringUtils.stringFromBinary(value.getVisibility())));
    }
    try {
      mutation.add(put);
    } catch (final IOException e) {
      LOGGER.error("Error creating HBase row mutation: " + e.getMessage());
    }
  }

  return mutation;
}
 
Example 7
static Table createTableAndWriteDataWithLabels(TableName tableName, String... labelExps)
    throws Exception {
  List<Put> puts = new ArrayList<>(labelExps.length + 1);
  for (int i = 0; i < labelExps.length; i++) {
    Put put = new Put(Bytes.toBytes("row" + (i+1)));
    put.addColumn(TEST_FAMILY, TEST_QUALIFIER, HConstants.LATEST_TIMESTAMP, ZERO);
    put.setCellVisibility(new CellVisibility(labelExps[i]));
    puts.add(put);
  }
  Table table = TEST_UTIL.createTable(tableName, TEST_FAMILY);
  table.put(puts);
  return table;
}
 
Example 8
static Table writeData(TableName tableName, String... labelExps) throws Exception {
  Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME);
  int i = 1;
  List<Put> puts = new ArrayList<>(labelExps.length);
  for (String labelExp : labelExps) {
    Put put = new Put(Bytes.toBytes("row" + i));
    put.addColumn(fam, qual, HConstants.LATEST_TIMESTAMP, value);
    put.setCellVisibility(new CellVisibility(labelExp));
    put.setAttribute(NON_VISIBILITY, Bytes.toBytes(TEMP));
    puts.add(put);
    i++;
  }
  table.put(puts);
  return table;
}
 
Example 9
Source Project: hbase   File: TestVisibilityLabels.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMutateRow() throws Exception {
  final byte[] qual2 = Bytes.toBytes("qual2");
  TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
    new TableDescriptorBuilder.ModifyableTableDescriptor(tableName);
  ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor familyDescriptor =
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(fam);
  tableDescriptor.setColumnFamily(familyDescriptor);
  TEST_UTIL.getAdmin().createTable(tableDescriptor);
  try (Table table = TEST_UTIL.getConnection().getTable(tableName)){
    Put p1 = new Put(row1);
    p1.addColumn(fam, qual, value);
    p1.setCellVisibility(new CellVisibility(CONFIDENTIAL));

    Put p2 = new Put(row1);
    p2.addColumn(fam, qual2, value);
    p2.setCellVisibility(new CellVisibility(SECRET));

    RowMutations rm = new RowMutations(row1);
    rm.add(p1);
    rm.add(p2);

    table.mutateRow(rm);

    Get get = new Get(row1);
    get.setAuthorizations(new Authorizations(CONFIDENTIAL));
    Result result = table.get(get);
    assertTrue(result.containsColumn(fam, qual));
    assertFalse(result.containsColumn(fam, qual2));

    get.setAuthorizations(new Authorizations(SECRET));
    result = table.get(get);
    assertFalse(result.containsColumn(fam, qual));
    assertTrue(result.containsColumn(fam, qual2));
  }
}
 
Example 10
Source Project: hbase   File: TestVisibilityLabels.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testFlushedFileWithVisibilityTags() throws Exception {
  final byte[] qual2 = Bytes.toBytes("qual2");
  TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
    new TableDescriptorBuilder.ModifyableTableDescriptor(tableName);
  ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor familyDescriptor =
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(fam);
  tableDescriptor.setColumnFamily(familyDescriptor);
  TEST_UTIL.getAdmin().createTable(tableDescriptor);
  try (Table table = TEST_UTIL.getConnection().getTable(tableName)) {
    Put p1 = new Put(row1);
    p1.addColumn(fam, qual, value);
    p1.setCellVisibility(new CellVisibility(CONFIDENTIAL));

    Put p2 = new Put(row1);
    p2.addColumn(fam, qual2, value);
    p2.setCellVisibility(new CellVisibility(SECRET));

    RowMutations rm = new RowMutations(row1);
    rm.add(p1);
    rm.add(p2);

    table.mutateRow(rm);
  }
  TEST_UTIL.getAdmin().flush(tableName);
  List<HRegion> regions = TEST_UTIL.getHBaseCluster().getRegions(tableName);
  HStore store = regions.get(0).getStore(fam);
  Collection<HStoreFile> storefiles = store.getStorefiles();
  assertTrue(storefiles.size() > 0);
  for (HStoreFile storeFile : storefiles) {
    assertTrue(storeFile.getReader().getHFileReader().getFileContext().isIncludesTags());
  }
}
 
Example 11
Source Project: hbase   File: TestVisibilityLabels.java    License: Apache License 2.0 5 votes vote down vote up
static Table createTableAndWriteDataWithLabels(TableName tableName, String... labelExps)
    throws Exception {
  List<Put> puts = new ArrayList<>(labelExps.length);
  for (int i = 0; i < labelExps.length; i++) {
    Put put = new Put(Bytes.toBytes("row" + (i+1)));
    put.addColumn(fam, qual, HConstants.LATEST_TIMESTAMP, value);
    put.setCellVisibility(new CellVisibility(labelExps[i]));
    puts.add(put);
  }
  Table table = TEST_UTIL.createTable(tableName, fam);
  table.put(puts);
  return table;
}
 
Example 12
private Table createTableAndWriteDataWithLabels(String... labelExps) throws Exception {
  Table table = createTable(fam);
  int i = 1;
  List<Put> puts = new ArrayList<>(labelExps.length);
  for (String labelExp : labelExps) {
    Put put = new Put(Bytes.toBytes("row" + i));
    put.addColumn(fam, qual, HConstants.LATEST_TIMESTAMP, value);
    put.setCellVisibility(new CellVisibility(labelExp));
    puts.add(put);
    table.put(put);
    i++;
  }
  // table.put(puts);
  return table;
}
 
Example 13
@Override
protected void persist(org.apache.hadoop.mapreduce.Mapper.Context output, long count,
    byte[][] prev, byte[][] current, byte[] id) throws IOException {
  String visibilityExps = "";
  String[] split = labels.split(COMMA);
  for (int i = 0; i < current.length; i++) {
    for (int j = 0; j < DEFAULT_TABLES_COUNT; j++) {
      Put put = new Put(current[i]);
      byte[] value = prev == null ? NO_KEY : prev[i];
      put.addColumn(FAMILY_NAME, COLUMN_PREV, value);

      if (count >= 0) {
        put.addColumn(FAMILY_NAME, COLUMN_COUNT, Bytes.toBytes(count + i));
      }
      if (id != null) {
        put.addColumn(FAMILY_NAME, COLUMN_CLIENT, id);
      }
      visibilityExps = split[j * 2] + OR + split[(j * 2) + 1];
      put.setCellVisibility(new CellVisibility(visibilityExps));
      tables[j].mutate(put);
      try {
        Thread.sleep(1);
      } catch (InterruptedException e) {
        throw new IOException();
      }
    }
    if (i % 1000 == 0) {
      // Tickle progress every so often else maprunner will think us hung
      output.progress();
    }
  }
}
 
Example 14
Source Project: geowave   File: HBaseMetadataWriter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void write(final GeoWaveMetadata metadata) {

  // we use a hashset of row IDs so that we can retain multiple versions
  // (otherwise timestamps will be applied on the server side in
  // batches and if the same row exists within a batch we will not
  // retain multiple versions)
  final Put put = new Put(metadata.getPrimaryId());

  final byte[] secondaryBytes =
      metadata.getSecondaryId() != null ? metadata.getSecondaryId() : new byte[0];

  put.addColumn(metadataTypeBytes, secondaryBytes, metadata.getValue());

  if ((metadata.getVisibility() != null) && (metadata.getVisibility().length > 0)) {
    put.setCellVisibility(
        new CellVisibility(StringUtils.stringFromBinary(metadata.getVisibility())));
  }

  try {
    synchronized (duplicateRowTracker) {
      final ByteArray primaryId = new ByteArray(metadata.getPrimaryId());
      if (!duplicateRowTracker.add(primaryId)) {
        writer.flush();
        duplicateRowTracker.clear();
        duplicateRowTracker.add(primaryId);
      }
    }
    writer.mutate(put);
  } catch (final IOException e) {
    LOGGER.error("Unable to write metadata", e);
  }
}
 
Example 15
@Test
public void testDeleteColumnWithLatestTimeStampUsingMultipleVersionsAfterCompaction()
    throws Exception {
  setAuths();
  final TableName tableName = TableName.valueOf(testName.getMethodName());
  try (Table table = doPuts(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.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
          d.addColumn(fam, qual);
          table.delete(d);
        } catch (Throwable t) {
          throw new IOException(t);
        }
        return null;
      }
    };
    SUPERUSER.runAs(actiona);
    TEST_UTIL.getAdmin().flush(tableName);
    Put put = new Put(Bytes.toBytes("row3"));
    put.addColumn(fam, qual, 127L, value);
    put.setCellVisibility(new CellVisibility(CONFIDENTIAL + "&" + PRIVATE));
    table.put(put);
    TEST_UTIL.getAdmin().flush(tableName);
    TEST_UTIL.getAdmin().majorCompact(tableName);
    // Sleep to ensure compaction happens. Need to do it in a better way
    Thread.sleep(5000);
    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 == 3);
    CellScanner cellScanner = next[0].cellScanner();
    cellScanner.advance();
    Cell current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
      row1, 0, row1.length));
    assertEquals(127L, current.getTimestamp());
    cellScanner.advance();
    current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
      row1, 0, row1.length));
    assertEquals(126L, current.getTimestamp());
    cellScanner.advance();
    current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
      row1, 0, row1.length));
    assertEquals(124L, current.getTimestamp());
    cellScanner.advance();
    current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
      row1, 0, row1.length));
    assertEquals(123L, current.getTimestamp());
    cellScanner = next[1].cellScanner();
    cellScanner.advance();
    current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
      row2, 0, row2.length));
  }
}
 
Example 16
@Test
public void testScanAfterCompaction() throws Exception {
  setAuths();
  final TableName tableName = TableName.valueOf(testName.getMethodName());
  try (Table table = doPuts(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.setCellVisibility(new CellVisibility(
              "(" + PRIVATE + "&" + CONFIDENTIAL + ")|(" + SECRET + "&" + TOPSECRET + ")"));
          d.addFamily(fam, 126L);
          table.delete(d);
        } catch (Throwable t) {
          throw new IOException(t);
        }
        return null;
      }
    };
    SUPERUSER.runAs(actiona);

    TEST_UTIL.getAdmin().flush(tableName);
    Put put = new Put(Bytes.toBytes("row3"));
    put.addColumn(fam, qual, 127L, value);
    put.setCellVisibility(new CellVisibility(CONFIDENTIAL + "&" + PRIVATE));
    table.put(put);
    TEST_UTIL.getAdmin().flush(tableName);
    TEST_UTIL.getAdmin().compact(tableName);
    Thread.sleep(5000);
    // Sleep to ensure compaction happens. Need to do it in a better way
    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 == 3);
    CellScanner cellScanner = next[0].cellScanner();
    cellScanner.advance();
    Cell current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
      row1, 0, row1.length));
    assertEquals(127L, current.getTimestamp());
    cellScanner = next[1].cellScanner();
    cellScanner.advance();
    current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
      row2, 0, row2.length));
  }
}
 
Example 17
@Test
public void testLabelsWithAppend() throws Throwable {
  PrivilegedExceptionAction<VisibilityLabelsResponse> action =
      new PrivilegedExceptionAction<VisibilityLabelsResponse>() {
    @Override
    public VisibilityLabelsResponse run() throws Exception {
      try (Connection conn = ConnectionFactory.createConnection(conf)) {
        return VisibilityClient.setAuths(conn, new String[] { TOPSECRET },
            USER.getShortName());
      } catch (Throwable e) {
      }
      return null;
    }
  };
  SUPERUSER.runAs(action);
  final TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  try (Table table = TEST_UTIL.createTable(tableName, fam)) {
    final byte[] row1 = Bytes.toBytes("row1");
    final byte[] val = Bytes.toBytes("a");
    PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
      @Override
      public Void run() throws Exception {
        try (Connection connection = ConnectionFactory.createConnection(conf);
             Table table = connection.getTable(tableName)) {
          Put put = new Put(row1);
          put.addColumn(fam, qual, HConstants.LATEST_TIMESTAMP, val);
          put.setCellVisibility(new CellVisibility(TOPSECRET));
          table.put(put);
        }
        return null;
      }
    };
    USER.runAs(actiona);
    actiona = new PrivilegedExceptionAction<Void>() {
      @Override
      public Void run() throws Exception {
        try (Connection connection = ConnectionFactory.createConnection(conf);
             Table table = connection.getTable(tableName)) {
          Append append = new Append(row1);
          append.addColumn(fam, qual, Bytes.toBytes("b"));
          table.append(append);
        }
        return null;
      }
    };
    USER.runAs(actiona);
    actiona = new PrivilegedExceptionAction<Void>() {
      @Override
      public Void run() throws Exception {
        try (Connection connection = ConnectionFactory.createConnection(conf);
             Table table = connection.getTable(tableName)) {
          Append append = new Append(row1);
          append.addColumn(fam, qual, Bytes.toBytes("c"));
          append.setCellVisibility(new CellVisibility(PUBLIC));
          table.append(append);
          Assert.fail("Testcase should fail with AccesDeniedException");
        } catch (Throwable t) {
          assertTrue(t.getMessage().contains("AccessDeniedException"));
        }
        return null;
      }
    };
    USER.runAs(actiona);
  }
}
 
Example 18
@Override
protected void addPutToKv(Put put, Cell kv) throws IOException {
  String visibilityExps = split[index * 2] + OR + split[(index * 2) + 1];
  put.setCellVisibility(new CellVisibility(visibilityExps));
  super.addPutToKv(put, kv);
}