Java Code Examples for org.apache.accumulo.core.client.Scanner#iterator()

The following examples show how to use org.apache.accumulo.core.client.Scanner#iterator() . 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: AccumuloRyaUtils.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Prints the table with the specified config and additional settings.
 * @param tableName the name of the table to print.
 * @param config the {@link AccumuloRdfConfiguration}.
 * @param shouldAddCommonIterators {@code true} to add the common iterators to the table scanner.
 * {@code false} otherwise.
 * @param settings the additional {@link IteratorSetting}s to add besides the common ones.
 * @throws IOException
 */
public static void printTable(final String tableName, final AccumuloRdfConfiguration config, final boolean shouldAddCommonIterators, final IteratorSetting... settings) throws IOException {
    final Scanner scanner = AccumuloRyaUtils.getScanner(tableName, config, shouldAddCommonIterators);
    for (final IteratorSetting setting : settings) {
        scanner.addScanIterator(setting);
    }

    final Iterator<Entry<Key, Value>> iterator = scanner.iterator();

    final String instance = config.get(MRUtils.AC_INSTANCE_PROP);
    log.info("==================");
    log.info("TABLE: " + tableName + " INSTANCE: " + instance);
    log.info("------------------");
    while (iterator.hasNext()) {
        final Entry<Key, Value> entry = iterator.next();
        final Key key = entry.getKey();
        final Value value = entry.getValue();
        final String keyString = getFormattedKeyString(key);
        log.info(keyString + " - " + value);
    }
    log.info("==================");
}
 
Example 2
Source File: ElementTableWrapper.java    From AccumuloGraph with Apache License 2.0 6 votes vote down vote up
/**
 * Read the given property from the backing table
 * for the given element id.
 * @param id
 * @param key
 * @return
 */
public <V> V readProperty(Element element, String key) {
  Scanner s = getScanner();

  s.setRange(new Range(element.getId().toString()));

  Text colf = StringFactory.LABEL.equals(key)
      ? new Text(Constants.LABEL) : new Text(key);
  s.fetchColumnFamily(colf);

  V value = null;

  Iterator<Entry<Key, Value>> iter = s.iterator();
  if (iter.hasNext()) {
    value = AccumuloByteSerializer.deserialize(iter.next().getValue().get());
  }
  s.close();

  return value;
}
 
Example 3
Source File: AccumuloRyaUtils.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Prints the table with the specified config and additional settings.
 * @param tableName the name of the table to print.
 * @param config the {@link AccumuloRdfConfiguration}.
 * @param shouldAddCommonIterators {@code true} to add the common iterators to the table scanner.
 * {@code false} otherwise.
 * @param settings the additional {@link IteratorSetting}s to add besides the common ones.
 * @throws IOException
 */
public static void printTable(final String tableName, final AccumuloRdfConfiguration config, final boolean shouldAddCommonIterators, final IteratorSetting... settings) throws IOException {
    final Scanner scanner = AccumuloRyaUtils.getScanner(tableName, config, shouldAddCommonIterators);
    for (final IteratorSetting setting : settings) {
        scanner.addScanIterator(setting);
    }

    final Iterator<Entry<Key, Value>> iterator = scanner.iterator();

    final String instance = config.get(MRUtils.AC_INSTANCE_PROP);
    log.info("==================");
    log.info("TABLE: " + tableName + " INSTANCE: " + instance);
    log.info("------------------");
    while (iterator.hasNext()) {
        final Entry<Key, Value> entry = iterator.next();
        final Key key = entry.getKey();
        final Value value = entry.getValue();
        final String keyString = getFormattedKeyString(key);
        log.info(keyString + " - " + value);
    }
    log.info("==================");
}
 
Example 4
Source File: SnapshotScanner.java    From fluo with Apache License 2.0 5 votes vote down vote up
private void setUpIterator() {
  Scanner scanner;
  try {
    scanner = env.getAccumuloClient().createScanner(env.getTable(), env.getAuthorizations());
  } catch (TableNotFoundException e) {
    throw new RuntimeException(e);
  }
  scanner.clearColumns();
  scanner.clearScanIterators();
  scanner.setRange(SpanUtil.toRange(snapIterConfig.getSpan()));

  setupScanner(scanner, snapIterConfig.getColumns(), startTs, snapIterConfig.showReadLocks);

  this.iterator = scanner.iterator();
}
 
Example 5
Source File: CardinalityScanner.java    From datawave with Apache License 2.0 5 votes vote down vote up
public Set<CardinalityIntersectionRecord> scanCardinalities(List<String> fields, DateAggregationType dateAggregationType,
                DatatypeAggregationType datatypeAggregationType) throws Exception {
    
    Map<CardinalityIntersectionRecord,HyperLogLogPlus> cardinalityMap = new TreeMap<>();
    Scanner scanner = null;
    try {
        ZooKeeperInstance instance = new ZooKeeperInstance(config.getInstanceName(), config.getZookeepers());
        Connector connector = instance.getConnector(config.getUsername(), new PasswordToken(config.getPassword()));
        Collection<Authorizations> authCollection = Collections.singleton(new Authorizations(config.getAuths().split(",")));
        if (!connector.tableOperations().exists(config.getTableName())) {
            throw new IllegalArgumentException("Table " + config.getTableName() + " does not exist");
        }
        scanner = ScannerHelper.createScanner(connector, config.getTableName(), authCollection);
        Range r = new Range(config.getBeginDate(), config.getEndDate() + "\0");
        scanner.setRange(r);
        
        Iterator<Map.Entry<Key,Value>> itr = scanner.iterator();
        while (itr.hasNext()) {
            Map.Entry<Key,Value> nextEntry = itr.next();
            Key key = nextEntry.getKey();
            String field = key.getColumnFamily().toString();
            if (fields != null && !fields.isEmpty() && !fields.contains(field)) {
                continue;
            } else {
                addEntry(cardinalityMap, nextEntry, dateAggregationType, datatypeAggregationType);
            }
        }
    } catch (Exception e) {
        log.error(e);
    } finally {
        if (scanner != null) {
            scanner.close();
            
        }
    }
    return cardinalityMap.keySet();
}
 
Example 6
Source File: QueryTestTableHelper.java    From datawave with Apache License 2.0 5 votes vote down vote up
public void dumpTable(String table, Authorizations auths) throws TableNotFoundException {
    TableOperations tops = connector.tableOperations();
    Scanner scanner = connector.createScanner(table, auths);
    Iterator<Map.Entry<Key,Value>> iterator = scanner.iterator();
    System.out.println("*************** " + table + " ********************");
    while (iterator.hasNext()) {
        Map.Entry<Key,Value> entry = iterator.next();
        System.out.println(entry);
    }
    scanner.close();
}
 
Example 7
Source File: GarbageCollectionIteratorIT.java    From fluo with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that older versions of data are newer than given timestamp
 *
 */
private void verify(long oldestTs) throws TableNotFoundException {
  Scanner scanner = aClient.createScanner(table, Authorizations.EMPTY);

  Iterator<Entry<Key, Value>> iter = scanner.iterator();

  Entry<Key, Value> prev = null;
  int numWrites = 0;
  while (iter.hasNext()) {
    Entry<Key, Value> entry = iter.next();

    if ((prev == null)
        || !prev.getKey().equals(entry.getKey(), PartialKey.ROW_COLFAM_COLQUAL_COLVIS)) {
      numWrites = 0;
    }

    ColumnType colType = ColumnType.from(entry.getKey());
    long ts = entry.getKey().getTimestamp() & ColumnConstants.TIMESTAMP_MASK;

    if (colType == ColumnType.WRITE) {
      numWrites++;
      if (numWrites > 1) {
        Assert.assertTrue("Extra write had ts " + ts + " < " + oldestTs, ts >= oldestTs);
      }
    }
    prev = entry;
  }
}
 
Example 8
Source File: AccumuloSelectivityEvalDAO.java    From rya with Apache License 2.0 5 votes vote down vote up
public int getTableSize(RdfCloudTripleStoreConfiguration conf) throws TableNotFoundException {

      Authorizations authorizations = getAuths(conf);
    

    if (joinMap.containsKey("subjectpredicateobject" + DELIM + "FullTableCardinality")) {
      FullTableCardinality = joinMap.get("subjectpredicateobject" + DELIM + "FullTableCardinality").intValue();
      return FullTableCardinality;
    }

    if (FullTableCardinality == 0) {
      Scanner joinScanner = connector.createScanner(tableLayoutStrategy.getSelectivity(), authorizations);
      joinScanner.setRange(Range.prefix(new Text("subjectpredicateobject" + DELIM + "FullTableCardinality")));
      Iterator<Map.Entry<Key,Value>> iterator = joinScanner.iterator();
      if (iterator.hasNext()) {
        Map.Entry<Key,Value> entry = iterator.next();
        if (entry.getKey().getColumnFamily().toString().equals("FullTableCardinality")) {
          String Count = entry.getKey().getColumnQualifier().toString();
          FullTableCardinality = Integer.parseInt(Count);
        }
      }
      if (FullTableCardinality == 0) {
        throw new RuntimeException("Table does not contain full cardinality");
      }

    }

    return FullTableCardinality;

  }
 
Example 9
Source File: WeakNotificationOverlapIT.java    From fluo with Apache License 2.0 5 votes vote down vote up
private int countNotifications() throws Exception {
  // deletes of notifications are queued async at end of transaction
  env.getSharedResources().getBatchWriter().waitForAsyncFlush();

  Scanner scanner = aClient.createScanner(getCurTableName(), Authorizations.EMPTY);
  Notification.configureScanner(scanner);

  int count = 0;
  for (Iterator<Entry<Key, Value>> iterator = scanner.iterator(); iterator.hasNext();) {
    iterator.next();
    count++;
  }
  return count;
}
 
Example 10
Source File: AccumuloRyaDAO.java    From rya with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public CloseableIteration<Namespace, RyaDAOException> iterateNamespace() throws RyaDAOException {
    try {
        final Scanner scanner = connector.createScanner(tableLayoutStrategy.getNs(),
                ALL_AUTHORIZATIONS);
        scanner.fetchColumnFamily(INFO_NAMESPACE_TXT);
        final Iterator<Map.Entry<Key, Value>> result = scanner.iterator();
        return new AccumuloNamespaceTableIterator(result);
    } catch (final Exception e) {
        throw new RyaDAOException(e);
    }
}
 
Example 11
Source File: PCJKeyToCrossProductBindingSetIterator.java    From rya with Apache License 2.0 5 votes vote down vote up
public PCJKeyToCrossProductBindingSetIterator(Scanner scanner,
		List<BindingSet> crossProductBs,
		Map<String, Value> constantConstraints, Set<String> unAssuredVariables,
		Map<String, String> pcjVarMap) {
	this.crossProductBs = crossProductBs;
	this.scanner = scanner;
	this.iterator = scanner.iterator();
	this.pcjVarMap = HashBiMap.create(pcjVarMap).inverse();
	this.constantConstraints = constantConstraints;
	this.crossProductBsExist = crossProductBs.size() > 0;
	this.constantConstraintsExist = constantConstraints.size() > 0;
	this.unAssuredVariables = unAssuredVariables;
}
 
Example 12
Source File: AccumuloValueBindingSetIterator.java    From rya with Apache License 2.0 4 votes vote down vote up
public AccumuloValueBindingSetIterator(Scanner scanner) {
    this.scanner = scanner;
    iter = scanner.iterator();
}
 
Example 13
Source File: AccumuloLoadStatementsFileIT.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void loadTurtleFile() throws Exception {
    // Install an instance of Rya.
    final InstallConfiguration installConfig = InstallConfiguration.builder()
            .setEnableTableHashPrefix(false)
            .setEnableEntityCentricIndex(false)
            .setEnableFreeTextIndex(false)
            .setEnableTemporalIndex(false)
            .setEnablePcjIndex(false)
            .setEnableGeoIndex(false)
            .setFluoPcjAppName("fluo_app_name")
            .build();

    final AccumuloConnectionDetails connectionDetails = new AccumuloConnectionDetails(
            getUsername(),
            getPassword().toCharArray(),
            getInstanceName(),
            getZookeepers());

    final RyaClient ryaClient = AccumuloRyaClientFactory.build(connectionDetails, getConnector());
    final Install install = ryaClient.getInstall();
    install.install(getRyaInstanceName(), installConfig);

    // Load the test statement file.
    ryaClient.getLoadStatementsFile().loadStatements(getRyaInstanceName(), Paths.get("src/test/resources/example.ttl"), RDFFormat.TURTLE);

    // Verify that the statements were loaded.
    final ValueFactory vf = SimpleValueFactory.getInstance();

    final List<Statement> expected = new ArrayList<>();
    expected.add( vf.createStatement(vf.createIRI("http://example#alice"), vf.createIRI("http://example#talksTo"), vf.createIRI("http://example#bob")) );
    expected.add( vf.createStatement(vf.createIRI("http://example#bob"), vf.createIRI("http://example#talksTo"), vf.createIRI("http://example#charlie")) );
    expected.add( vf.createStatement(vf.createIRI("http://example#charlie"), vf.createIRI("http://example#likes"), vf.createIRI("http://example#icecream")) );

    final List<Statement> statements = new ArrayList<>();

    final WholeRowTripleResolver tripleResolver = new WholeRowTripleResolver();
    final Scanner scanner = getConnector().createScanner(getRyaInstanceName() + "spo", new Authorizations());
    final Iterator<Entry<Key, Value>> it = scanner.iterator();
    while(it.hasNext()) {
        final Entry<Key, Value> next = it.next();

        final Key key = next.getKey();
        final byte[] row = key.getRow().getBytes();
        final byte[] columnFamily = key.getColumnFamily().getBytes();
        final byte[] columnQualifier = key.getColumnQualifier().getBytes();
        final TripleRow tripleRow = new TripleRow(row, columnFamily, columnQualifier);

        final RyaStatement ryaStatement = tripleResolver.deserialize(TABLE_LAYOUT.SPO, tripleRow);
        final Statement statement = RyaToRdfConversions.convertStatement(ryaStatement);

        // Filter out the rya version statement if it is present.
        if(!isRyaMetadataStatement(vf, statement)) {
            statements.add( statement );
        }
    }

    assertEquals(expected, statements);
}
 
Example 14
Source File: CopyToolTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testImportDirectoryTool() throws Exception {
    log.info("");
    log.info("Setting up initial state of parent before importing directory to child...");
    log.info("Adding data to parent...");

    log.info("Starting import directory tool. Importing all data after the specified start time: " + YESTERDAY);
    log.info("");

    isImporting = true;
    copyToolRun(YESTERDAY);


    // Import Directory Tool made child instance so hook the tables and dao into the driver.
    final String childUser = accumuloDualInstanceDriver.getChildUser();
    final Connector childConnector = ConfigUtils.getConnector(childConfig);
    accumuloDualInstanceDriver.getChildAccumuloInstanceDriver().setConnector(childConnector);

    accumuloDualInstanceDriver.getChildAccumuloInstanceDriver().setUpTables();

    accumuloDualInstanceDriver.getChildAccumuloInstanceDriver().setUpDao();


    // Update child config to include changes made from import directory process
    final SecurityOperations childSecOps = accumuloDualInstanceDriver.getChildSecOps();
    final Authorizations newChildAuths = AccumuloRyaUtils.addUserAuths(childUser, childSecOps, PARENT_AUTH);
    childSecOps.changeUserAuthorizations(childUser, newChildAuths);
    final String childAuthString = newChildAuths.toString();
    final List<String> duplicateKeys = MergeTool.DUPLICATE_KEY_MAP.get(MRUtils.AC_AUTH_PROP);
    childConfig.set(MRUtils.AC_AUTH_PROP, childAuthString);
    for (final String key : duplicateKeys) {
        childConfig.set(key, childAuthString);
    }


    //AccumuloRyaUtils.printTablePretty(CHILD_TABLE_PREFIX + RdfCloudTripleStoreConstants.TBL_PO_SUFFIX, childConfig);
    //AccumuloRyaUtils.printTablePretty(CHILD_TABLE_PREFIX + RdfCloudTripleStoreConstants.TBL_OSP_SUFFIX, childConfig);
    AccumuloRyaUtils.printTablePretty(CHILD_TABLE_PREFIX + RdfCloudTripleStoreConstants.TBL_SPO_SUFFIX, childConfig);

    final Scanner scanner = AccumuloRyaUtils.getScanner(CHILD_TABLE_PREFIX + RdfCloudTripleStoreConstants.TBL_SPO_SUFFIX, childConfig);
    final Iterator<Entry<Key, Value>> iterator = scanner.iterator();
    int count = 0;
    while (iterator.hasNext()) {
        iterator.next();
        count++;
    }
    log.info("");
    log.info("Total rows imported: " + count);
    log.info("");

    assertEquals(20, count);

    log.info("DONE");
}
 
Example 15
Source File: FileCount.java    From accumulo-examples with Apache License 2.0 4 votes vote down vote up
private void calculateCounts(Scanner scanner, int depth, BatchWriter batchWriter)
    throws Exception {

  scanner.setRange(
      new Range(String.format("%03d", depth), true, String.format("%03d", depth + 1), false));

  CountValue countVal = new CountValue();

  Iterator<Entry<Key,Value>> iterator = scanner.iterator();

  String currentDir = null;

  Entry<Key,Value> entry = null;
  if (iterator.hasNext()) {
    entry = iterator.next();
    entriesScanned++;
  }

  while (entry != null) {
    Key key = entry.getKey();

    String dir = extractDir(key);

    if (currentDir == null) {
      currentDir = dir;
    } else if (!currentDir.equals(dir)) {
      batchWriter.addMutation(createMutation(depth - 1, currentDir, countVal));
      inserts++;
      currentDir = dir;
      countVal.clear();
    }

    // process a whole row
    if (key.compareColumnFamily(QueryUtil.DIR_COLF) == 0) {
      CountValue tmpCount = new CountValue();
      entry = findCount(entry, iterator, tmpCount);

      if (tmpCount.dirCount == 0 && tmpCount.fileCount == 0) {
        // in this case the higher depth will not insert anything if the
        // dir has no children, so insert something here
        Mutation m = new Mutation(key.getRow());
        m.put(QueryUtil.DIR_COLF, QueryUtil.COUNTS_COLQ, visibility, tmpCount.toValue());
        batchWriter.addMutation(m);
        inserts++;
      }

      countVal.incrementRecursive(tmpCount);
      countVal.incrementDirs();
    } else {
      entry = consumeRow(entry, iterator);
      countVal.incrementFiles();
    }
  }

  if (currentDir != null) {
    batchWriter.addMutation(createMutation(depth - 1, currentDir, countVal));
    inserts++;
  }
}
 
Example 16
Source File: PcjTables.java    From rya with Apache License 2.0 4 votes vote down vote up
/**
 * Fetch the {@link PCJMetadata} from an Accumulo table.
 * <p>
 * This method assumes the PCJ table has already been created.
 *
 * @param accumuloConn - A connection to the Accumulo that hosts the PCJ table. (not null)
 * @param pcjTableName - The name of the table that will be search. (not null)
 * @return The PCJ Metadata that has been stolred in the in the PCJ Table.
 * @throws PCJStorageException The PCJ Table does not exist.
 */
public PcjMetadata getPcjMetadata(
        final Connector accumuloConn,
        final String pcjTableName) throws PCJStorageException {
    checkNotNull(accumuloConn);
    checkNotNull(pcjTableName);

    Scanner scanner = null;
    try {
        // Create an Accumulo scanner that iterates through the metadata entries.
        scanner = accumuloConn.createScanner(pcjTableName, new Authorizations());
        final Iterator<Entry<Key, Value>> entries = scanner.iterator();

        // No metadata has been stored in the table yet.
        if(!entries.hasNext()) {
            throw new PCJStorageException("Could not find any PCJ metadata in the table named: " + pcjTableName);
        }

        // Fetch the metadata from the entries. Assuming they all have the same cardinality and sparql query.
        String sparql = null;
        Long cardinality = null;
        final Set<VariableOrder> varOrders = new HashSet<>();

        while(entries.hasNext()) {
            final Entry<Key, Value> entry = entries.next();
            final Text columnQualifier = entry.getKey().getColumnQualifier();
            final byte[] value = entry.getValue().get();

            if(columnQualifier.equals(PCJ_METADATA_SPARQL_QUERY)) {
                sparql = stringLexicoder.decode(value);
            } else if(columnQualifier.equals(PCJ_METADATA_CARDINALITY)) {
                cardinality = longLexicoder.decode(value);
            } else if(columnQualifier.equals(PCJ_METADATA_VARIABLE_ORDERS)) {
                for(final String varOrderStr : listLexicoder.decode(value)) {
                    varOrders.add( new VariableOrder(varOrderStr) );
                }
            }
        }

        return new PcjMetadata(sparql, cardinality, varOrders);

    } catch (final TableNotFoundException e) {
        throw new PCJStorageException("Could not add results to a PCJ because the PCJ table does not exist.", e);
    } finally {
        if(scanner != null) {
            scanner.close();
        }
    }
}
 
Example 17
Source File: DiscoveryIteratorTest.java    From datawave with Apache License 2.0 4 votes vote down vote up
@Test
public void testReverseIndex() throws Throwable {
    Connector con = new InMemoryInstance("DiscoveryIteratorTest").getConnector("root", new PasswordToken(""));
    con.tableOperations().create("reverseIndex");
    writeSample(con.createBatchWriter("reverseIndex", new BatchWriterConfig().setMaxLatency(0, TimeUnit.SECONDS).setMaxMemory(0).setMaxWriteThreads(1)),
                    true);
    Scanner s = con.createScanner("reverseIndex", new Authorizations("FOO"));
    IteratorSetting setting = new IteratorSetting(50, DiscoveryIterator.class);
    setting.addOption(DiscoveryLogic.REVERSE_INDEX, "true");
    s.addScanIterator(setting);
    s.setRange(new Range());
    
    Iterator<Map.Entry<Key,Value>> itr = s.iterator();
    assertTrue(itr.hasNext());
    Map.Entry<Key,Value> e = itr.next();
    assertFalse(itr.hasNext());
    
    Key key = e.getKey();
    assertEquals("mret", key.getRow().toString());
    assertEquals("field", key.getColumnFamily().toString());
    // see DiscoveryIterator for why this has a max unsigned char tacked on the end
    assertEquals("20130101\uffff", key.getColumnQualifier().toString());
    
    Value value = e.getValue();
    assertTrue(value.getSize() > 0);
    
    DataInputBuffer in = new DataInputBuffer();
    in.reset(value.get(), value.getSize());
    ArrayWritable valWrapper = new ArrayWritable(DiscoveredThing.class);
    valWrapper.readFields(in);
    Writable[] values = valWrapper.get();
    assertEquals(3, values.length);
    Set<String> types = Sets.newHashSet("t1", "t2", "t3");
    for (int i = 0; i < 3; ++i) {
        DiscoveredThing thing = (DiscoveredThing) values[i];
        assertEquals("term", thing.getTerm());
        assertEquals("field", thing.getField());
        assertTrue(types.remove(thing.getType()));
        assertEquals("20130101", thing.getDate());
        assertEquals("FOO", thing.getColumnVisibility());
        assertEquals(240L, thing.getCount());
    }
    
}
 
Example 18
Source File: FailureIT.java    From fluo with Apache License 2.0 4 votes vote down vote up
@Test
public void testAcks() throws Exception {
  // TODO test that acks are properly handled in rollback and rollforward

  TestTransaction tx = new TestTransaction(env);

  final Column lastUpdate = new Column("attr", "lastupdate");
  final Column docContent = new Column("doc", "content");
  final Column docUrl = new Column("doc", "url");

  tx.set("url0000", lastUpdate, "3");
  tx.set("url0000", docContent, "abc def");

  tx.done();

  TestTransaction tx2 = new TestTransaction(env, "url0000", lastUpdate);
  tx2.set("idx:abc", docUrl, "url0000");
  tx2.set("idx:def", docUrl, "url0000");
  CommitData cd = tx2.createCommitData();
  tx2.preCommit(cd);

  TestTransaction tx3 = new TestTransaction(env);
  Assert.assertNull(tx3.gets("idx:abc", docUrl));
  Assert.assertNull(tx3.gets("idx:def", docUrl));
  Assert.assertEquals("3", tx3.gets("url0000", lastUpdate));

  Scanner scanner = env.getAccumuloClient().createScanner(env.getTable(), Authorizations.EMPTY);
  Notification.configureScanner(scanner);
  Iterator<Entry<Key, Value>> iter = scanner.iterator();
  Assert.assertTrue(iter.hasNext());
  Assert.assertEquals("url0000", iter.next().getKey().getRow().toString());

  TestTransaction tx5 = new TestTransaction(env, "url0000", lastUpdate);
  tx5.set("idx:abc", docUrl, "url0000");
  tx5.set("idx:def", docUrl, "url0000");
  cd = tx5.createCommitData();
  Assert.assertTrue(tx5.preCommit(cd));
  Stamp commitTs = env.getSharedResources().getOracleClient().getStamp();
  Assert.assertTrue(tx5.commitPrimaryColumn(cd, commitTs));

  // should roll tx5 forward
  TestTransaction tx6 = new TestTransaction(env);
  Assert.assertEquals("3", tx6.gets("url0000", lastUpdate));
  Assert.assertEquals("url0000", tx6.gets("idx:abc", docUrl));
  Assert.assertEquals("url0000", tx6.gets("idx:def", docUrl));

  iter = scanner.iterator();
  Assert.assertTrue(iter.hasNext());

  // TODO is tx4 start before tx5, then this test will not work because AlreadyAck is not thrown
  // for overlapping.. CommitException is thrown
  TestTransaction tx4 = new TestTransaction(env, "url0000", lastUpdate);
  tx4.set("idx:abc", docUrl, "url0000");
  tx4.set("idx:def", docUrl, "url0000");

  try {
    // should not go through if tx5 is properly rolled forward
    tx4.commit();
    Assert.fail();
  } catch (AlreadyAcknowledgedException aae) {
    // do nothing
  }

  // commit above should schedule async delete of notification
  env.getSharedResources().getBatchWriter().waitForAsyncFlush();
  iter = scanner.iterator();
  Assert.assertFalse(iter.hasNext());
}
 
Example 19
Source File: TestIndexer.java    From presto with Apache License 2.0 4 votes vote down vote up
@Test
public void testMutationIndex()
        throws Exception
{
    Instance inst = new MockInstance();
    Connector conn = inst.getConnector("root", new PasswordToken(""));
    conn.tableOperations().create(table.getFullTableName());
    conn.tableOperations().create(table.getIndexTableName());
    conn.tableOperations().create(table.getMetricsTableName());

    for (IteratorSetting s : Indexer.getMetricIterators(table)) {
        conn.tableOperations().attachIterator(table.getMetricsTableName(), s);
    }

    Indexer indexer = new Indexer(conn, new Authorizations(), table, new BatchWriterConfig());
    indexer.index(m1);
    indexer.flush();

    Scanner scan = conn.createScanner(table.getIndexTableName(), new Authorizations());
    scan.setRange(new Range());

    Iterator<Entry<Key, Value>> iter = scan.iterator();
    assertKeyValuePair(iter.next(), AGE_VALUE, "cf_age", "row1", "");
    assertKeyValuePair(iter.next(), bytes("abc"), "cf_arr", "row1", "");
    assertKeyValuePair(iter.next(), M1_FNAME_VALUE, "cf_firstname", "row1", "");
    assertKeyValuePair(iter.next(), bytes("def"), "cf_arr", "row1", "");
    assertKeyValuePair(iter.next(), bytes("ghi"), "cf_arr", "row1", "");
    assertFalse(iter.hasNext());

    scan.close();

    scan = conn.createScanner(table.getMetricsTableName(), new Authorizations());
    scan.setRange(new Range());

    iter = scan.iterator();
    assertKeyValuePair(iter.next(), AGE_VALUE, "cf_age", "___card___", "1");
    assertKeyValuePair(iter.next(), Indexer.METRICS_TABLE_ROW_ID.array(), "___rows___", "___card___", "1");
    assertKeyValuePair(iter.next(), Indexer.METRICS_TABLE_ROW_ID.array(), "___rows___", "___first_row___", "row1");
    assertKeyValuePair(iter.next(), Indexer.METRICS_TABLE_ROW_ID.array(), "___rows___", "___last_row___", "row1");
    assertKeyValuePair(iter.next(), bytes("abc"), "cf_arr", "___card___", "1");
    assertKeyValuePair(iter.next(), M1_FNAME_VALUE, "cf_firstname", "___card___", "1");
    assertKeyValuePair(iter.next(), bytes("def"), "cf_arr", "___card___", "1");
    assertKeyValuePair(iter.next(), bytes("ghi"), "cf_arr", "___card___", "1");
    assertFalse(iter.hasNext());

    scan.close();

    indexer.index(m2);
    indexer.close();

    scan = conn.createScanner(table.getIndexTableName(), new Authorizations());
    scan.setRange(new Range());
    iter = scan.iterator();
    assertKeyValuePair(iter.next(), AGE_VALUE, "cf_age", "row1", "");
    assertKeyValuePair(iter.next(), AGE_VALUE, "cf_age", "row2", "");
    assertKeyValuePair(iter.next(), bytes("abc"), "cf_arr", "row1", "");
    assertKeyValuePair(iter.next(), bytes("abc"), "cf_arr", "row2", "");
    assertKeyValuePair(iter.next(), M1_FNAME_VALUE, "cf_firstname", "row1", "");
    assertKeyValuePair(iter.next(), M2_FNAME_VALUE, "cf_firstname", "row2", "");
    assertKeyValuePair(iter.next(), bytes("def"), "cf_arr", "row1", "");
    assertKeyValuePair(iter.next(), bytes("ghi"), "cf_arr", "row1", "");
    assertKeyValuePair(iter.next(), bytes("ghi"), "cf_arr", "row2", "");
    assertKeyValuePair(iter.next(), bytes("mno"), "cf_arr", "row2", "");
    assertFalse(iter.hasNext());

    scan.close();

    scan = conn.createScanner(table.getMetricsTableName(), new Authorizations());
    scan.setRange(new Range());

    iter = scan.iterator();
    assertKeyValuePair(iter.next(), AGE_VALUE, "cf_age", "___card___", "2");
    assertKeyValuePair(iter.next(), Indexer.METRICS_TABLE_ROW_ID.array(), "___rows___", "___card___", "2");
    assertKeyValuePair(iter.next(), Indexer.METRICS_TABLE_ROW_ID.array(), "___rows___", "___first_row___", "row1");
    assertKeyValuePair(iter.next(), Indexer.METRICS_TABLE_ROW_ID.array(), "___rows___", "___last_row___", "row2");
    assertKeyValuePair(iter.next(), bytes("abc"), "cf_arr", "___card___", "2");
    assertKeyValuePair(iter.next(), M1_FNAME_VALUE, "cf_firstname", "___card___", "1");
    assertKeyValuePair(iter.next(), M2_FNAME_VALUE, "cf_firstname", "___card___", "1");
    assertKeyValuePair(iter.next(), bytes("def"), "cf_arr", "___card___", "1");
    assertKeyValuePair(iter.next(), bytes("ghi"), "cf_arr", "___card___", "2");
    assertKeyValuePair(iter.next(), bytes("mno"), "cf_arr", "___card___", "1");
    assertFalse(iter.hasNext());

    scan.close();
}
 
Example 20
Source File: ProspectorService.java    From rya with Apache License 2.0 3 votes vote down vote up
/**
 * Get a list of timestamps that represents all of the Prospect runs that
 * have been performed inclusively between two timestamps.
 *
 * @param beginTime - The start of the time range.
 * @param endTime - The end of the time range.
 * @param auths - The authorizations used to scan the table for prospects.
 * @return A list of timestamps representing each Prospect run that was found.
 * @throws TableNotFoundException The table name that was provided when this
 *   class was constructed does not match a table that the connector has access to.
 */
public Iterator<Long> getProspectsInRange(long beginTime, long endTime, String[] auths) throws TableNotFoundException {
    final Scanner scanner = connector.createScanner(tableName, new Authorizations(auths));
    scanner.setRange(new Range(
            new Key(METADATA, PROSPECT_TIME, ProspectorUtils.getReverseIndexDateTime(new Date(endTime)), "", Long.MAX_VALUE),
            new Key(METADATA, PROSPECT_TIME, ProspectorUtils.getReverseIndexDateTime(new Date(beginTime)), "", 0l)
    ));

    return new ProspectTimestampIterator( scanner.iterator() );
}