Java Code Examples for org.apache.accumulo.core.client.Connector#createScanner()

The following examples show how to use org.apache.accumulo.core.client.Connector#createScanner() . 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: Indexer.java    From presto with Apache License 2.0 6 votes vote down vote up
public static Pair<byte[], byte[]> getMinMaxRowIds(Connector connector, AccumuloTable table, Authorizations auths)
        throws TableNotFoundException
{
    Scanner scanner = connector.createScanner(table.getMetricsTableName(), auths);
    scanner.setRange(new Range(new Text(Indexer.METRICS_TABLE_ROW_ID.array())));
    Text family = new Text(Indexer.METRICS_TABLE_ROWS_CF.array());
    Text firstRowQualifier = new Text(Indexer.METRICS_TABLE_FIRST_ROW_CQ.array());
    Text lastRowQualifier = new Text(Indexer.METRICS_TABLE_LAST_ROW_CQ.array());
    scanner.fetchColumn(family, firstRowQualifier);
    scanner.fetchColumn(family, lastRowQualifier);

    byte[] firstRow = null;
    byte[] lastRow = null;
    for (Entry<Key, Value> entry : scanner) {
        if (entry.getKey().compareColumnQualifier(firstRowQualifier) == 0) {
            firstRow = entry.getValue().get();
        }

        if (entry.getKey().compareColumnQualifier(lastRowQualifier) == 0) {
            lastRow = entry.getValue().get();
        }
    }
    scanner.close();
    return Pair.of(firstRow, lastRow);
}
 
Example 2
Source File: IngestMetricsSummaryLoader.java    From datawave with Apache License 2.0 6 votes vote down vote up
@Override
protected void setup(Context context) throws IOException, InterruptedException {
    super.setup(context);
    
    Configuration conf = context.getConfiguration();
    String user = conf.get(MetricsConfig.USER);
    String password = conf.get(MetricsConfig.PASS);
    String instance = conf.get(MetricsConfig.INSTANCE);
    String zookeepers = conf.get(MetricsConfig.ZOOKEEPERS);
    
    useHourlyPrecision = HourlyPrecisionHelper.checkForHourlyPrecisionOption(context.getConfiguration(), log);
    
    try {
        ZooKeeperInstance inst = new ZooKeeperInstance(ClientConfiguration.loadDefault().withInstance(instance).withZkHosts(zookeepers));
        Connector con = inst.getConnector(user, new PasswordToken(password));
        ingestScanner = con.createScanner(conf.get(MetricsConfig.INGEST_TABLE, MetricsConfig.DEFAULT_INGEST_TABLE), Authorizations.EMPTY);
    } catch (TableNotFoundException | AccumuloException | AccumuloSecurityException e) {
        throw new IOException(e);
    }
}
 
Example 3
Source File: PrintUtility.java    From datawave with Apache License 2.0 6 votes vote down vote up
/**
 * Utility class to print all the entries in a table
 *
 * @param conn
 *            Connector to mock accumulo
 * @param authorizations
 *            Authorizations to run scanner with
 * @param tableName
 *            Table to scan
 * @throws TableNotFoundException
 *             Invalid table name
 */
public static void printTable(final Connector conn, final Authorizations authorizations, final String tableName) throws TableNotFoundException {
    if (logger.isDebugEnabled()) {
        final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HHmmss");
        
        final StringBuilder sb = new StringBuilder("--Begin entire " + tableName + " table--");
        
        sb.append("\n");
        
        final Scanner scanner = conn.createScanner(tableName, authorizations);
        for (final Entry<Key,Value> e : scanner) {
            sb.append(e.getKey().toStringNoTime());
            sb.append(' ');
            sb.append(dateFormat.format(new Date(e.getKey().getTimestamp())));
            sb.append('\t');
            sb.append(getPrintableValue(e.getValue()));
            sb.append("\n");
        }
        
        sb.append("--End entire ").append(tableName).append(" table--").append("\n");
        
        logger.debug(sb.toString());
    }
}
 
Example 4
Source File: PrintUtility.java    From datawave with Apache License 2.0 6 votes vote down vote up
public static void printTable(final Connector conn, final Authorizations authorizations, final String tableName, final PrintStream out)
                throws TableNotFoundException {
    final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HHmmss");
    
    final StringBuilder sb = new StringBuilder("--Begin entire " + tableName + " table--");
    
    sb.append("\n");
    
    final Scanner scanner = conn.createScanner(tableName, authorizations);
    for (final Entry<Key,Value> e : scanner) {
        sb.append(e.getKey().toStringNoTime());
        sb.append(' ');
        sb.append(dateFormat.format(new Date(e.getKey().getTimestamp())));
        sb.append('\t');
        sb.append(getPrintableValue(e.getValue()));
        sb.append("\n");
    }
    
    sb.append("--End entire ").append(tableName).append(" table--").append("\n");
    
    out.println(sb);
}
 
Example 5
Source File: GetMetricTableSplitPoints.java    From timely with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        try (ConfigurableApplicationContext ctx = new SpringApplicationBuilder(SpringBootstrap.class)
                .bannerMode(Mode.OFF).web(WebApplicationType.NONE).run(args)) {
            Configuration conf = ctx.getBean(Configuration.class);

            final Map<String, String> properties = new HashMap<>();
            Accumulo accumuloConf = conf.getAccumulo();
            properties.put("instance.name", accumuloConf.getInstanceName());
            properties.put("instance.zookeeper.host", accumuloConf.getZookeepers());
            final ClientConfiguration aconf = ClientConfiguration.fromMap(properties);
            final Instance instance = new ZooKeeperInstance(aconf);
            Connector con = instance.getConnector(accumuloConf.getUsername(),
                    new PasswordToken(accumuloConf.getPassword()));
            Scanner s = con.createScanner(conf.getMetaTable(),
                    con.securityOperations().getUserAuthorizations(con.whoami()));
            try {
                s.setRange(new Range(Meta.METRIC_PREFIX, true, Meta.TAG_PREFIX, false));
                for (Entry<Key, Value> e : s) {
                    System.out.println(e.getKey().getRow().toString().substring(Meta.METRIC_PREFIX.length()));
                }
            } finally {
                s.close();
            }
        }
    }
 
Example 6
Source File: AccumuloInputOperatorTest.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
@Override
public Scanner getScanner(Connector conn)
{
  Authorizations auths = new Authorizations();
  Scanner scan = null;
  try {
    scan = conn.createScanner(getStore().getTableName(), auths);
  } catch (TableNotFoundException e) {
    logger.error("table not found ");
    DTThrowable.rethrow(e);
  }
  scan.setRange(new Range());
  // scan.fetchColumnFamily("attributes");

  return scan;
}
 
Example 7
Source File: PcjTablesIT.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Scan accumulo for the results that are stored in a PCJ table. The
 * multimap stores a set of deserialized binding sets that were in the PCJ
 * table for every variable order that is found in the PCJ metadata.
 */
private static Multimap<String, BindingSet> loadPcjResults(final Connector accumuloConn, final String pcjTableName) throws PcjException, TableNotFoundException, BindingSetConversionException {
    final Multimap<String, BindingSet> fetchedResults = HashMultimap.create();

    // Get the variable orders the data was written to.
    final PcjTables pcjs = new PcjTables();
    final PcjMetadata pcjMetadata = pcjs.getPcjMetadata(accumuloConn, pcjTableName);

    // Scan Accumulo for the stored results.
    for(final VariableOrder varOrder : pcjMetadata.getVarOrders()) {
        final Scanner scanner = accumuloConn.createScanner(pcjTableName, new Authorizations());
        scanner.fetchColumnFamily( new Text(varOrder.toString()) );

        for(final Entry<Key, Value> entry : scanner) {
            final byte[] serializedResult = entry.getKey().getRow().getBytes();
            final BindingSet result = converter.convert(serializedResult, varOrder);
            fetchedResults.put(varOrder.toString(), result);
        }
    }

    return fetchedResults;
}
 
Example 8
Source File: PcjTables.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Get an {@link Iterator} over the {@link BindingSet}s that are stored in the PCJ table.
 *
 * @param accumuloConn - A connection to the Accumulo that hsots the PCJ table. (not null)
 * @param pcjTableName - The name of the PCJ table that will be scanned. (not null)
 * @param auths - the user's authorizations that will be used to scan the table. (not null)
 * @return An iterator over all of the {@link BindingSet}s that are stored as
 *   results for the PCJ.
 * @throws PCJStorageException The binding sets could not be fetched.
 */
public CloseableIterator<BindingSet> listResults(final Connector accumuloConn, final String pcjTableName, final Authorizations auths) throws PCJStorageException {
    requireNonNull(pcjTableName);

    // Fetch the Variable Orders for the binding sets and choose one of them. It
    // doesn't matter which one we choose because they all result in the same output.
    final PcjMetadata metadata = getPcjMetadata(accumuloConn, pcjTableName);
    final VariableOrder varOrder = metadata.getVarOrders().iterator().next();

    try {
        // Fetch only the Binding Sets whose Variable Order matches the selected one.
        final Scanner scanner = accumuloConn.createScanner(pcjTableName, auths);
        scanner.fetchColumnFamily( new Text(varOrder.toString()) );

        // Return an Iterator that uses that scanner.
        return new ScannerBindingSetIterator(scanner, varOrder);

    } catch (final TableNotFoundException e) {
        throw new PCJStorageException(String.format("PCJ Table does not exist for name '%s'.", pcjTableName), e);
    }
}
 
Example 9
Source File: DiscoveryIteratorTest.java    From datawave with Apache License 2.0 5 votes vote down vote up
@Test
public void testHappyPath() throws Throwable {
    Connector con = new InMemoryInstance("DiscoveryIteratorTest").getConnector("root", new PasswordToken(""));
    con.tableOperations().create("index");
    writeSample(con.createBatchWriter("index", new BatchWriterConfig().setMaxLatency(0, TimeUnit.SECONDS).setMaxMemory(0).setMaxWriteThreads(1)));
    Scanner s = con.createScanner("index", new Authorizations("FOO"));
    s.addScanIterator(new IteratorSetting(50, DiscoveryIterator.class));
    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("term", 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 10
Source File: AccumuloGeoTableTest.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
@Test
@Category(UnitTest.class)
public void testGetTile() throws Exception
{

  ZooKeeperInstance zkinst = new ZooKeeperInstance(inst, zoo);
  PasswordToken pwTok = new PasswordToken(pw.getBytes());
  Connector conn = zkinst.getConnector(u, pwTok);
  Assert.assertNotNull(conn);

  PasswordToken token = new PasswordToken(pw.getBytes());

  Authorizations auths = new Authorizations(authsStr.split(","));
  long start = 0;
  long end = Long.MAX_VALUE;
  Key sKey = AccumuloUtils.toKey(start);
  Key eKey = AccumuloUtils.toKey(end);
  Range r = new Range(sKey, eKey);
  Scanner s = conn.createScanner("paris4", auths);
  s.fetchColumnFamily(new Text(Integer.toString(10)));
  s.setRange(r);

  Iterator<Entry<Key, Value>> it = s.iterator();
  while (it.hasNext())
  {
    Entry<Key, Value> ent = it.next();
    if (ent == null)
    {
      return;
    }
    System.out.println("current key   = " + AccumuloUtils.toLong(ent.getKey().getRow()));
    System.out.println("current value = " + ent.getValue().getSize());
  }

}
 
Example 11
Source File: QueriesTableAgeOffIteratorTest.java    From datawave with Apache License 2.0 5 votes vote down vote up
@Test
public void testAgeOffIterator() throws Exception {
    InMemoryInstance instance = new InMemoryInstance();
    Connector connector = instance.getConnector("root", new PasswordToken(""));
    
    connector.tableOperations().create(TABLE_NAME);
    IteratorSetting iteratorCfg = new IteratorSetting(19, "ageoff", QueriesTableAgeOffIterator.class);
    connector.tableOperations().attachIterator(TABLE_NAME, iteratorCfg, EnumSet.allOf(IteratorScope.class));
    
    long now = System.currentTimeMillis();
    // Write in a couple of keys with varying timestamps
    BatchWriter writer = connector.createBatchWriter(TABLE_NAME, new BatchWriterConfig().setMaxLatency(30, TimeUnit.MILLISECONDS).setMaxMemory(1024L)
                    .setMaxWriteThreads(1));
    
    Mutation m1 = new Mutation("row1");
    m1.put("colf1", "colq1", now, "");
    writer.addMutation(m1);
    
    Mutation m2 = new Mutation("row2");
    m2.put("colf2", "colq2", (now + 100000), "");
    writer.addMutation(m2);
    
    writer.close();
    
    // Scan the entire table, we should only see keys whose timestamps are greater than or equal to now.
    // Mutation 1 should be expired by now, we should only see Mutation 2;
    boolean sawRow2 = false;
    Scanner scanner = connector.createScanner(TABLE_NAME, new Authorizations());
    for (Entry<Key,Value> entry : scanner) {
        if (entry.getKey().getRow().toString().equals("row1"))
            Assert.fail("We saw row1 when it should be expired.");
        if (entry.getKey().getRow().toString().equals("row2"))
            sawRow2 = true;
    }
    if (!sawRow2)
        Assert.fail("We did not see row2 and we should have");
}
 
Example 12
Source File: ScannerHelper.java    From timely with Apache License 2.0 5 votes vote down vote up
public static Scanner createScanner(Connector connector, String tableName,
        Collection<Authorizations> authorizations) throws TableNotFoundException {
    if (authorizations == null || authorizations.isEmpty())
        throw new IllegalArgumentException("Authorizations must not be empty.");

    Iterator<Authorizations> iter = AuthorizationsMinimizer.minimize(authorizations).iterator();
    Scanner scanner = connector.createScanner(tableName, iter.next());
    addVisibilityFilters(iter, scanner);
    return scanner;
}
 
Example 13
Source File: AccumuloHDFSFileInputFormat.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public List<InputSplit> getSplits(JobContext jobContext) throws IOException {
    //read the params from AccumuloInputFormat
    Configuration conf = jobContext.getConfiguration();
    Instance instance = MRUtils.AccumuloProps.getInstance(jobContext);
    String user = MRUtils.AccumuloProps.getUsername(jobContext);
    AuthenticationToken password = MRUtils.AccumuloProps.getPassword(jobContext);
    String table = MRUtils.AccumuloProps.getTablename(jobContext);
    ArgumentChecker.notNull(instance);
    ArgumentChecker.notNull(table);

    //find the files necessary
    try {
        Connector connector = instance.getConnector(user, password);
        TableOperations tos = connector.tableOperations();
        String tableId = tos.tableIdMap().get(table);
        Scanner scanner = connector.createScanner("accumulo.metadata", Authorizations.EMPTY); //TODO: auths?
        scanner.setRange(new Range(new Text(tableId + "\u0000"), new Text(tableId + "\uFFFD")));
        scanner.fetchColumnFamily(new Text("file"));
        List<String> files = new ArrayList<String>();
        List<InputSplit> fileSplits = new ArrayList<InputSplit>();
        for (Map.Entry<Key, Value> entry : scanner) {
            String file = entry.getKey().getColumnQualifier().toString();
            Path path = new Path(file);
            FileSystem fs = path.getFileSystem(conf);
            FileStatus fileStatus = fs.getFileStatus(path);
            long len = fileStatus.getLen();
            BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(fileStatus, 0, len);
            files.add(file);
            fileSplits.add(new FileSplit(path, 0, len, fileBlockLocations[0].getHosts()));
        }
        System.out.println(files);
        return fileSplits;
    } catch (Exception e) {
        throw new IOException(e);
    }
}
 
Example 14
Source File: ConfigUtils.java    From rya with Apache License 2.0 5 votes vote down vote up
public static Scanner createScanner(final String tablename, final Configuration conf)
        throws AccumuloException, AccumuloSecurityException, TableNotFoundException {
    final Connector connector = ConfigUtils.getConnector(conf);
    final Authorizations auths = ConfigUtils.getAuthorizations(conf);
    return connector.createScanner(tablename, auths);

}
 
Example 15
Source File: AccumuloTestUtils.java    From accumulo-recipes with Apache License 2.0 4 votes vote down vote up
public static void dumpTable(Connector connector, String table, Authorizations auths) throws TableNotFoundException {
    Scanner scanner = connector.createScanner(table, auths);
    for (Map.Entry<Key, Value> entry : scanner) {
        System.out.println("ENTRY: " + entry);
    }
}
 
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: PcjVisibilityIT.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void visibilitySimplified() throws Exception {
    // Create a PCJ index within Rya.
    final String sparql =
            "SELECT ?customer ?worker ?city " +
            "{ " +
              "?customer <" + TALKS_TO + "> ?worker. " +
              "?worker <" + LIVES_IN + "> ?city. " +
              "?worker <" + WORKS_AT + "> <" + BURGER_JOINT + ">. " +
            "}";

    final Connector accumuloConn = super.getAccumuloConnector();
    final String instanceName = super.getMiniAccumuloCluster().getInstanceName();
    final String zookeepers = super.getMiniAccumuloCluster().getZooKeepers();

    final RyaClient ryaClient = AccumuloRyaClientFactory.build(createConnectionDetails(), accumuloConn);

    final String pcjId = ryaClient.getCreatePCJ().createPCJ(getRyaInstanceName(), sparql);

    // Grant the root user the "u" authorization.
    super.getAccumuloConnector().securityOperations().changeUserAuthorizations(getUsername(), new Authorizations("u"));

    // Setup a connection to the Rya instance that uses the "u" authorizations. This ensures
    // any statements that are inserted will have the "u" authorization on them and that the
    // PCJ updating application will have to maintain visibilities.
    final AccumuloRdfConfiguration ryaConf = new AccumuloRdfConfiguration();
    ryaConf.setTablePrefix(getRyaInstanceName());

    // Accumulo connection information.
    ryaConf.setAccumuloUser(getUsername());
    ryaConf.setAccumuloPassword(getPassword());
    ryaConf.setAccumuloInstance(super.getAccumuloConnector().getInstance().getInstanceName());
    ryaConf.setAccumuloZookeepers(super.getAccumuloConnector().getInstance().getZooKeepers());
    ryaConf.set(ConfigUtils.CLOUDBASE_AUTHS, "u");
    ryaConf.set(RdfCloudTripleStoreConfiguration.CONF_CV, "u");

    // PCJ configuration information.
    ryaConf.set(ConfigUtils.USE_PCJ, "true");
    ryaConf.set(ConfigUtils.USE_PCJ_UPDATER_INDEX, "true");
    ryaConf.set(ConfigUtils.FLUO_APP_NAME, super.getFluoConfiguration().getApplicationName());
    ryaConf.set(ConfigUtils.PCJ_STORAGE_TYPE,
            PrecomputedJoinIndexerConfig.PrecomputedJoinStorageType.ACCUMULO.toString());
    ryaConf.set(ConfigUtils.PCJ_UPDATER_TYPE,
            PrecomputedJoinIndexerConfig.PrecomputedJoinUpdaterType.FLUO.toString());

    Sail sail = null;
    RyaSailRepository ryaRepo = null;
    RepositoryConnection ryaConn = null;

    try {
        sail = RyaSailFactory.getInstance(ryaConf);
        ryaRepo = new RyaSailRepository(sail);
        ryaConn = ryaRepo.getConnection();

        // Load a few Statements into Rya.
        ryaConn.add(VF.createStatement(ALICE, TALKS_TO, BOB));
        ryaConn.add(VF.createStatement(BOB, LIVES_IN, HAPPYVILLE));
        ryaConn.add(VF.createStatement(BOB, WORKS_AT, BURGER_JOINT));

        // Wait for Fluo to finish processing.
        super.getMiniFluo().waitForObservers();

        // Fetch the exported result and show that its column visibility has been simplified.
        final String pcjTableName = new PcjTableNameFactory().makeTableName(getRyaInstanceName(), pcjId);
        final Scanner scan = accumuloConn.createScanner(pcjTableName, new Authorizations("u"));
        scan.fetchColumnFamily(new Text("customer;worker;city"));

        final Entry<Key, Value> result = scan.iterator().next();
        final Key key = result.getKey();
        assertEquals(new Text("u"), key.getColumnVisibility());

    } finally {
        if(ryaConn != null) {
            try {
                ryaConn.close();
            } finally { }
        }

        if(ryaRepo != null) {
            try {
                ryaRepo.shutDown();
            } finally { }
        }

        if(sail != null) {
            try {
                sail.shutDown();
            } finally { }
        }
    }
}
 
Example 18
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 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();
}