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

The following are Jave code examples for showing how to use setCellVisibility() 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: TestVisibilityLabelsWithACL.java   Source Code and License Vote up 6 votes
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<Put>();
    for (String labelExp : labelExps) {
      Put put = new Put(Bytes.toBytes("row" + i));
      put.add(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
Project: ditb   File: TestVisibilityLabelsWithDeletes.java   Source Code and License Vote up 6 votes
public static Table createTableAndWriteDataWithLabels(TableName tableName, String... labelExps)
    throws Exception {
  Table table = null;
  table = TEST_UTIL.createTable(tableName, fam);
  int i = 1;
  List<Put> puts = new ArrayList<Put>();
  for (String labelExp : labelExps) {
    Put put = new Put(Bytes.toBytes("row" + i));
    put.add(fam, qual, HConstants.LATEST_TIMESTAMP, value);
    put.setCellVisibility(new CellVisibility(labelExp));
    puts.add(put);
    table.put(put);
    i++;
  }
  // table.put(puts);
  return table;
}
 
Example 3
Project: ditb   File: TestVisibilityLabelsWithDeletes.java   Source Code and License Vote up 6 votes
public static Table createTableAndWriteDataWithLabels(TableName tableName, long[] timestamp,
    String... labelExps) throws Exception {
  Table table = null;
  table = TEST_UTIL.createTable(tableName, fam);
  int i = 1;
  List<Put> puts = new ArrayList<Put>();
  for (String labelExp : labelExps) {
    Put put = new Put(Bytes.toBytes("row" + i));
    put.add(fam, qual, timestamp[i - 1], value);
    put.setCellVisibility(new CellVisibility(labelExp));
    puts.add(put);
    table.put(put);
    TEST_UTIL.getHBaseAdmin().flush(tableName);
    i++;
  }
  return table;
}
 
Example 4
Project: ditb   File: IntegrationTestWithCellVisibilityLoadAndVerify.java   Source Code and License Vote up 6 votes
@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.add(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
Project: ditb   File: TestScannersWithLabels.java   Source Code and License Vote up 6 votes
private static int insertData(TableName tableName, String column, double prob) throws IOException {
  byte[] k = new byte[3];
  byte[][] famAndQf = KeyValue.parseColumn(Bytes.toBytes(column));

  List<Put> puts = new ArrayList<>();
  for (int i = 0; i < 9; i++) {
    Put put = new Put(Bytes.toBytes("row" + i));
    put.setDurability(Durability.SKIP_WAL);
    put.add(famAndQf[0], famAndQf[1], k);
    put.setCellVisibility(new CellVisibility("(" + SECRET + "|" + CONFIDENTIAL + ")" + "&" + "!"
        + TOPSECRET));
    puts.add(put);
  }
  try (Table table = new HTable(TEST_UTIL.getConfiguration(), tableName)) {
    table.put(puts);
  }
  return puts.size();
}
 
Example 6
Project: ditb   File: TestWithDisabledAuthorization.java   Source Code and License Vote up 5 votes
static Table createTableAndWriteDataWithLabels(TableName tableName, String... labelExps)
    throws Exception {
  List<Put> puts = new ArrayList<Put>();
  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 7
Project: ditb   File: TestVisibilityLabelsReplication.java   Source Code and License Vote up 5 votes
static Table writeData(TableName tableName, String... labelExps) throws Exception {
  Table table = TEST_UTIL.getConnection().getTable(TableName.valueOf(TABLE_NAME));
  int i = 1;
  List<Put> puts = new ArrayList<Put>();
  for (String labelExp : labelExps) {
    Put put = new Put(Bytes.toBytes("row" + i));
    put.add(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 8
Project: ditb   File: TestVisibilityLabels.java   Source Code and License Vote up 5 votes
@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.add(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 9
Project: ditb   File: TestVisibilityLabels.java   Source Code and License Vote up 5 votes
@Test
public void testMutateRow() throws Exception {
  final byte[] qual2 = Bytes.toBytes("qual2");
  TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  HTableDescriptor desc = new HTableDescriptor(tableName);
  HColumnDescriptor col = new HColumnDescriptor(fam);
  desc.addFamily(col);
  TEST_UTIL.getHBaseAdmin().createTable(desc);
  try (Table table = TEST_UTIL.getConnection().getTable(tableName)){
    Put p1 = new Put(row1);
    p1.add(fam, qual, value);
    p1.setCellVisibility(new CellVisibility(CONFIDENTIAL));

    Put p2 = new Put(row1);
    p2.add(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
Project: ditb   File: TestVisibilityLabels.java   Source Code and License Vote up 5 votes
@Test
public void testFlushedFileWithVisibilityTags() throws Exception {
  final byte[] qual2 = Bytes.toBytes("qual2");
  TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  HTableDescriptor desc = new HTableDescriptor(tableName);
  HColumnDescriptor col = new HColumnDescriptor(fam);
  desc.addFamily(col);
  TEST_UTIL.getHBaseAdmin().createTable(desc);
  try (Table table = TEST_UTIL.getConnection().getTable(tableName)) {
    Put p1 = new Put(row1);
    p1.add(fam, qual, value);
    p1.setCellVisibility(new CellVisibility(CONFIDENTIAL));

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

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

    table.mutateRow(rm);
  }
  TEST_UTIL.getHBaseAdmin().flush(tableName);
  List<HRegion> regions = TEST_UTIL.getHBaseCluster().getRegions(tableName);
  Store store = regions.get(0).getStore(fam);
  Collection<StoreFile> storefiles = store.getStorefiles();
  assertTrue(storefiles.size() > 0);
  for (StoreFile storeFile : storefiles) {
    assertTrue(storeFile.getReader().getHFileReader().getFileContext().isIncludesTags());
  }
}
 
Example 11
Project: ditb   File: TestVisibilityLabels.java   Source Code and License Vote up 5 votes
static Table createTableAndWriteDataWithLabels(TableName tableName, String... labelExps)
    throws Exception {
  List<Put> puts = new ArrayList<Put>();
  for (int i = 0; i < labelExps.length; i++) {
    Put put = new Put(Bytes.toBytes("row" + (i+1)));
    put.add(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
Project: ditb   File: IntegrationTestBigLinkedListWithVisibility.java   Source Code and License Vote up 5 votes
@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]);
      put.add(FAMILY_NAME, COLUMN_PREV, prev == null ? NO_KEY : prev[i]);
      
      if (count >= 0) {
        put.add(FAMILY_NAME, COLUMN_COUNT, Bytes.toBytes(count + i));
      }
      if (id != null) {
        put.add(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 13
Project: ditb   File: TestVisibilityLabelsWithDeletes.java   Source Code and License Vote up 4 votes
@Test
public void testDeleteColumnWithLatestTimeStampUsingMultipleVersionsAfterCompaction()
    throws Exception {
  setAuths();
  final TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  try (Table table = doPuts(tableName)) {
    TEST_UTIL.getHBaseAdmin().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.getHBaseAdmin().flush(tableName);
    Put put = new Put(Bytes.toBytes("row3"));
    put.add(fam, qual, 127l, value);
    put.setCellVisibility(new CellVisibility(CONFIDENTIAL + "&" + PRIVATE));
    table.put(put);
    TEST_UTIL.getHBaseAdmin().flush(tableName);
    TEST_UTIL.getHBaseAdmin().majorCompact(tableName);
    // Sleep to ensure compaction happens. Need to do it in a better way
    Thread.sleep(5000);
    Scan s = new Scan();
    s.setMaxVersions(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(current.getTimestamp(), 127l);
    cellScanner.advance();
    current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
        current.getRowLength(), row1, 0, row1.length));
    assertEquals(current.getTimestamp(), 126l);
    cellScanner.advance();
    current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
        current.getRowLength(), row1, 0, row1.length));
    assertEquals(current.getTimestamp(), 124l);
    cellScanner.advance();
    current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
        current.getRowLength(), row1, 0, row1.length));
    assertEquals(current.getTimestamp(), 123l);
    cellScanner = next[1].cellScanner();
    cellScanner.advance();
    current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
        current.getRowLength(), row2, 0, row2.length));
  }
}
 
Example 14
Project: ditb   File: TestVisibilityLabelsWithDeletes.java   Source Code and License Vote up 4 votes
@Test
public void testScanAfterCompaction() throws Exception {
  setAuths();
  final TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  try (Table table = doPuts(tableName)) {
    TEST_UTIL.getHBaseAdmin().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.getHBaseAdmin().flush(tableName);
    Put put = new Put(Bytes.toBytes("row3"));
    put.add(fam, qual, 127l, value);
    put.setCellVisibility(new CellVisibility(CONFIDENTIAL + "&" + PRIVATE));
    table.put(put);
    TEST_UTIL.getHBaseAdmin().flush(tableName);
    TEST_UTIL.getHBaseAdmin().compact(tableName);
    Thread.sleep(5000);
    // Sleep to ensure compaction happens. Need to do it in a better way
    Scan s = new Scan();
    s.setMaxVersions(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(current.getTimestamp(), 127l);
    cellScanner = next[1].cellScanner();
    cellScanner.advance();
    current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
        current.getRowLength(), row2, 0, row2.length));
  }
}
 
Example 15
Project: ditb   File: TestVisibilityWithCheckAuths.java   Source Code and License Vote up 4 votes
@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.add(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.add(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.add(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 16
Project: ditb   File: IntegrationTestBigLinkedListWithVisibility.java   Source Code and License Vote up 4 votes
@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);
}