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

The following examples show how to use org.apache.accumulo.core.client.Scanner#fetchColumn() . 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: IndexLookup.java    From presto with Apache License 2.0 6 votes vote down vote up
private long getNumRowsInTable(String metricsTable, Authorizations auths)
        throws TableNotFoundException
{
    // Create scanner against the metrics table, pulling the special column and the rows column
    Scanner scanner = connector.createScanner(metricsTable, auths);
    scanner.setRange(METRICS_TABLE_ROWID_RANGE);
    scanner.fetchColumn(METRICS_TABLE_ROWS_CF_AS_TEXT, CARDINALITY_CQ_AS_TEXT);

    // Scan the entry and get the number of rows
    long numRows = -1;
    for (Entry<Key, Value> entry : scanner) {
        if (numRows > 0) {
            throw new PrestoException(FUNCTION_IMPLEMENTATION_ERROR, "Should have received only one entry when scanning for number of rows in metrics table");
        }
        numRows = Long.parseLong(entry.getValue().toString());
    }
    scanner.close();

    LOG.debug("Number of rows in table is %d", numRows);
    return numRows;
}
 
Example 2
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 3
Source File: AccumuloIndexAgeDisplay.java    From datawave with Apache License 2.0 6 votes vote down vote up
/**
 * Add one or more column families or column family and qualifier to a scanner.
 * 
 * @param scanner
 *            to add columns to.
 * @return The scanner with columns
 */
private Scanner addColumnsToScanner(Scanner scanner) {
    if ((null != columns) && (!columns.equals(("")))) {
        String[] cols = columns.split(",");
        for (String colStr : cols) {
            String[] parts = colStr.split(":");
            if (parts.length == 1) {
                scanner.fetchColumnFamily(new Text(parts[0]));
            } else if (parts.length == 2) {
                scanner.fetchColumn(new Text(parts[0]), new Text(parts[1]));
            }
        }
    }
    
    return (scanner);
}
 
Example 4
Source File: CountIT.java    From accumulo-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
  Scanner scanner = client.createScanner(tableName, new Authorizations());
  scanner.fetchColumn(new Text("dir"), new Text("counts"));
  assertFalse(scanner.iterator().hasNext());

  ScannerOpts scanOpts = new ScannerOpts();
  BatchWriterOpts bwOpts = new BatchWriterOpts();
  FileCount fc = new FileCount(client, tableName, Authorizations.EMPTY, new ColumnVisibility(),
      scanOpts, bwOpts);
  fc.run();

  ArrayList<Pair<String,String>> expected = new ArrayList<>();
  expected.add(new Pair<>(QueryUtil.getRow("").toString(), "1,0,3,3"));
  expected.add(new Pair<>(QueryUtil.getRow("/local").toString(), "2,1,2,3"));
  expected.add(new Pair<>(QueryUtil.getRow("/local/user1").toString(), "0,2,0,2"));
  expected.add(new Pair<>(QueryUtil.getRow("/local/user2").toString(), "0,0,0,0"));

  int i = 0;
  for (Entry<Key,Value> e : scanner) {
    assertEquals(e.getKey().getRow().toString(), expected.get(i).getFirst());
    assertEquals(e.getValue().toString(), expected.get(i).getSecond());
    i++;
  }
  assertEquals(i, expected.size());
}
 
Example 5
Source File: AccumuloWindowStore.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
@Override
public long getCommittedWindowId(String appId, int operatorId)
{
  byte[] value = null;
  Authorizations auths = new Authorizations();
  Scanner scan = null;
  String columnKey = appId + "_" + operatorId + "_" + lastWindowColumnName;
  lastWindowColumnBytes = columnKey.getBytes();
  try {
    scan = connector.createScanner(tableName, auths);
  } catch (TableNotFoundException e) {
    logger.error("error getting committed window id", e);
    DTThrowable.rethrow(e);
  }
  scan.setRange(new Range(new Text(rowBytes)));
  scan.fetchColumn(new Text(columnFamilyBytes), new Text(lastWindowColumnBytes));
  for (Entry<Key, Value> entry : scan) {
    value = entry.getValue().get();
  }
  if (value != null) {
    long longval = toLong(value);
    return longval;
  }
  return -1;
}
 
Example 6
Source File: AccumuloTestHelper.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
public static AccumuloTuple getAccumuloTuple(String row, String colFam,
    String colName)
{
  Authorizations auths = new Authorizations();

  Scanner scan = null;
  try {
    scan = con.createScanner("tab1", auths);
  } catch (TableNotFoundException e) {
    logger.error("error in test helper");
    DTThrowable.rethrow(e);
  }

  scan.setRange(new Range(new Text(row)));
  scan.fetchColumn(new Text(colFam), new Text(colName));
  // assuming only one row
  for (Entry<Key, Value> entry : scan) {
    AccumuloTuple tuple = new AccumuloTuple();
    tuple.setRow(entry.getKey().getRow().toString());
    tuple.setColFamily(entry.getKey().getColumnFamily().toString());
    tuple.setColName(entry.getKey().getColumnQualifier().toString());
    tuple.setColValue(entry.getValue().toString());
    return tuple;
  }
  return null;
}
 
Example 7
Source File: AccumuloRyaDAO.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public String getNamespace(final String pfx) throws RyaDAOException {
    try {
        final Scanner scanner = connector.createScanner(tableLayoutStrategy.getNs(),
                ALL_AUTHORIZATIONS);
        scanner.fetchColumn(INFO_NAMESPACE_TXT, EMPTY_TEXT);
        scanner.setRange(new Range(new Text(pfx)));
        final Iterator<Map.Entry<Key, Value>> iterator = scanner
                .iterator();

        if (iterator.hasNext()) {
            return new String(iterator.next().getValue().get(), StandardCharsets.UTF_8);
        }
    } catch (final Exception e) {
        throw new RyaDAOException(e);
    }
    return null;
}
 
Example 8
Source File: AccumuloTemporalIndexer.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Get intervals stored in the repository matching the given interval.
 * Indexing Intervals  will probably change or be removed.
 * Currently predicate and subject constraints are filtered on the client.
 */
@Override
public CloseableIteration<Statement, QueryEvaluationException> queryIntervalEquals(
        final TemporalInterval query, final StatementConstraints contraints)
        throws QueryEvaluationException {
    final Scanner scanner = getScanner();
    if (scanner != null) {
        // get rows where the start and end match.
        final Range range = Range.prefix(new Text(query.getAsKeyBeginning()));
        scanner.setRange(range);
        if (contraints.hasContext()) {
            scanner.fetchColumn(new Text(contraints.getContext().toString()), new Text(KeyParts.CQ_BEGIN));
        } else {
            scanner.fetchColumn(new Text(""), new Text(KeyParts.CQ_BEGIN));
        }
    }
    // Iterator<Entry<Key, Value>> iter = scanner.iterator();
    // while (iter.hasNext()) {
    // System.out.println("queryIntervalEquals results:"+iter.next());
    // }
    //return getConstrainedIteratorWrapper(scanner, contraints);
    return getIteratorWrapper(scanner);
}
 
Example 9
Source File: AccumuloTemporalIndexer.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * find intervals stored in the repository before the given Interval. Find interval endings that are
 * before the given beginning.
 * Indexing Intervals  will probably change or be removed.
 * Currently predicate and subject constraints are filtered on the client.
 */
@Override
public CloseableIteration<Statement, QueryEvaluationException> queryIntervalBefore(
        final TemporalInterval queryInterval, final StatementConstraints constraints) throws QueryEvaluationException
{
    final Scanner scanner = getScanner();
    if (scanner != null) {
        // get rows where the end date is less than the queryInterval.getBefore()
        final Range range = new Range(null, false, new Key(new Text(queryInterval.getHasBeginning().getAsKeyBytes())), false);
        scanner.setRange(range);
         if (constraints.hasContext()) {
            scanner.fetchColumn(new Text(constraints.getContext().toString()), new Text(KeyParts.CQ_END));
        } else {
            scanner.fetchColumn(new Text(""), new Text(KeyParts.CQ_END));
        }
    }
    return getIteratorWrapper(scanner);
}
 
Example 10
Source File: AccumuloTemporalIndexer.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Interval after given interval.  Find intervals that begin after the endings of the given interval.
 * Use the special following prefix mechanism to avoid matching the beginning date.
 * Indexing Intervals  will probably change or be removed.
 * Currently predicate and subject and context constraints are filtered on the client.
 */
@Override
public CloseableIteration<Statement, QueryEvaluationException> queryIntervalAfter(
        final TemporalInterval queryInterval, final StatementConstraints constraints)
        throws QueryEvaluationException {

    final Scanner scanner = getScanner();
    if (scanner != null) {
        // get rows where the start date is greater than the queryInterval.getEnd()
        final Range range = new Range(new Key(Range.followingPrefix(new Text(queryInterval.getHasEnd().getAsKeyBytes()))), false, null, true);
        scanner.setRange(range);

        if (constraints.hasContext()) {
            scanner.fetchColumn(new Text(constraints.getContext().toString()), new Text(KeyParts.CQ_BEGIN));
        } else {
            scanner.fetchColumn(new Text(""), new Text(KeyParts.CQ_BEGIN));
        }
    }
    // TODO currently predicate, subject and context constraints are filtered on the clients
    return getIteratorWrapper(scanner);
}
 
Example 11
Source File: AccumuloRyaInstanceDetailsRepository.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isInitialized() throws RyaDetailsRepositoryException {
    Scanner scanner = null;
    try {
        scanner = connector.createScanner(detailsTableName, new Authorizations());
        scanner.fetchColumn(COL_FAMILY, COL_QUALIFIER);
        return scanner.iterator().hasNext();
    } catch (final TableNotFoundException e) {
        return false;
    } finally {
        if(scanner != null) {
            scanner.close();
        }
    }
}
 
Example 12
Source File: AccumuloRyaInstanceDetailsRepository.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public RyaDetails getRyaInstanceDetails() throws NotInitializedException, RyaDetailsRepositoryException {
    // Preconditions.
    if(!isInitialized()) {
        throw new NotInitializedException("Could not fetch the details for the Rya instanced named '" +
                instanceName + "' because it has not been initialized yet.");
    }

    // Read it from the table.
    Scanner scanner = null;
    try {
        // Fetch the value from the table.
        scanner = connector.createScanner(detailsTableName, new Authorizations());
        scanner.fetchColumn(COL_FAMILY, COL_QUALIFIER);
        final Entry<Key, Value> entry = scanner.iterator().next();

        // Deserialize it.
        final byte[] bytes = entry.getValue().get();
        return serializer.deserialize( bytes );

    } catch (final TableNotFoundException e) {
        throw new RyaDetailsRepositoryException("Could not get the details from the table.", e);
    } finally {
        if(scanner != null) {
            scanner.close();
        }
    }
}
 
Example 13
Source File: AccumuloFeatureStore.java    From accumulo-recipes with Apache License 2.0 5 votes vote down vote up
protected ScannerBase metricScanner(AccumuloFeatureConfig xform, Date start, Date end, String group, String type, String name, TimeUnit timeUnit, Auths auths) {
    checkNotNull(xform);

    try {

        group = defaultString(group);
        timeUnit = (timeUnit == null ? TimeUnit.MINUTES : timeUnit);

        Scanner scanner = connector.createScanner(tableName + REVERSE_SUFFIX, auths.getAuths());
        scanner.setRange(buildRange(type, start, end, timeUnit));

        if (group != null && name != null) {
            scanner.fetchColumn(new Text(combine(timeUnit.toString(), xform.featureName())), new Text(combine(group, name)));
        } else {
            scanner.fetchColumnFamily(new Text(combine(timeUnit.toString(), xform.featureName())));

            String cqRegex = null;
            if (group != null) {
                cqRegex = combine(group, "(.*)");
            } else if (name != null)
                cqRegex = combine("(.*)", name);
            if (cqRegex != null) {
                IteratorSetting regexIterator = new IteratorSetting(Constants.DEFAULT_ITERATOR_PRIORITY - 1, "regex", RegExFilter.class);
                scanner.addScanIterator(regexIterator);
            }
        }

        return scanner;
    } catch (TableNotFoundException e) {
        throw new RuntimeException(e);
    }
}
 
Example 14
Source File: DataStoreImpl.java    From timely with Apache License 2.0 4 votes vote down vote up
@Override
public Set<Tag> getColumnFamilies(String metric, Map<String, String> requestedTags) throws TableNotFoundException {

    Scanner meta = null;
    try {
        Map<String, String> tags = (requestedTags == null) ? new LinkedHashMap<>() : requestedTags;
        LOG.trace("Looking for requested tags: {}", tags);
        meta = connector.createScanner(metaTable, Authorizations.EMPTY);
        Text start = new Text(Meta.VALUE_PREFIX + metric);
        Text end = new Text(Meta.VALUE_PREFIX + metric + "\\x0000");
        end.append(new byte[] { (byte) 0xff }, 0, 1);
        meta.setRange(new Range(start, end));
        // Only look for the meta entries that match our tags, if any
        boolean onlyFirstRow = false;
        Entry<String, String> first = null;
        // Set the columns on the meta scanner based on the first tag
        // in the set of tags passed in the query. If no tags are present
        // then we are only going to return the first tag name present in the
        // meta table.
        Iterator<Entry<String, String>> tagIter = tags.entrySet().iterator();
        if (tagIter.hasNext()) {
            first = tagIter.next();
            if (isTagValueRegex(first.getValue())) {
                meta.fetchColumnFamily(new Text(first.getKey()));
            } else {
                meta.fetchColumn(new Text(first.getKey()), new Text(first.getValue()));
            }
        } else {
            // grab all of the values found for the first tag for the metric
            onlyFirstRow = true;
        }
        final boolean ONLY_RETURN_FIRST_TAG = onlyFirstRow;
        Iterator<Entry<Key, Value>> iter = meta.iterator();
        Iterator<Pair<String, String>> knownKeyValues = new Iterator<Pair<String, String>>() {

            Text firstTag = null;
            Text tagName = null;
            Text tagValue = null;

            @Override
            public boolean hasNext() {
                if (iter.hasNext()) {
                    Entry<Key, Value> metaEntry = iter.next();
                    if (null == firstTag) {
                        firstTag = metaEntry.getKey().getColumnFamily();
                    }
                    tagName = metaEntry.getKey().getColumnFamily();
                    tagValue = metaEntry.getKey().getColumnQualifier();
                    LOG.trace("Found tag entry {}={}", tagName, tagValue);

                    if (ONLY_RETURN_FIRST_TAG && !tagName.equals(firstTag)) {
                        return false;
                    }
                    return true;
                }
                return false;
            }

            @Override
            public Pair<String, String> next() {
                LOG.trace("Returning tag {}={}", tagName, tagValue);
                return new Pair<>(tagName.toString(), tagValue.toString());
            }
        };
        // Expand the list of tags in the meta table for this metric that
        // matches
        // the pattern of the first tag in the query. The resulting set of tags
        // will be used to fetch specific columns from the metric table.
        return expandTagValues(first, knownKeyValues);
    } finally {
        if (meta != null) {
            meta.close();
        }
    }
}
 
Example 15
Source File: AccumuloMrsPyramidMetadataReader.java    From mrgeo with Apache License 2.0 4 votes vote down vote up
private MrsPyramidMetadata loadMetadata() throws IOException
{

  Properties mrgeoAccProps = AccumuloConnector.getAccumuloProperties();
  String authsString = mrgeoAccProps.getProperty(MrGeoAccumuloConstants.MRGEO_ACC_KEY_AUTHS);
  Properties p1 = null;
  Properties p2 = null;

  if (dataProvider == null)
  {
    //log.info("no data provider used");
    mrgeoAccProps = AccumuloConnector.getAccumuloProperties();
  }
  else
  {

    // get authorizations from dataProvider
    p1 = AccumuloUtils.providerPropertiesToProperties(dataProvider.getProviderProperties());
    p2 = dataProvider.getQueryProperties();
    if (p1 != null)
    {
      mrgeoAccProps.putAll(p1);
    }
    if (p2 != null)
    {
      mrgeoAccProps.putAll(p2);
    }

  }

  if (p1 != null)
  {
    if (p1.getProperty(MrGeoAccumuloConstants.MRGEO_ACC_KEY_AUTHS) != null &&
        p1.getProperty(MrGeoAccumuloConstants.MRGEO_ACC_KEY_AUTHS).length() > 0)
    {
      authsString = p1.getProperty(MrGeoAccumuloConstants.MRGEO_ACC_KEY_AUTHS);
    }
  }
  if (mrgeoAccProps.getProperty(DataProviderFactory.PROVIDER_PROPERTY_USER_ROLES) != null &&
      mrgeoAccProps.getProperty(DataProviderFactory.PROVIDER_PROPERTY_USER_ROLES).length() > 0)
  {
    authsString = mrgeoAccProps.getProperty(DataProviderFactory.PROVIDER_PROPERTY_USER_ROLES);
  }

  auths = AccumuloUtils.createAuthorizationsFromDelimitedString(authsString);

  if (conn == null)
  {
    try
    {
      conn = AccumuloConnector.getConnector(mrgeoAccProps);
    }
    catch (DataProviderException dpe)
    {
      throw new IOException("No connection to Accumulo!", dpe);
    }
  }

  if (name == null || name.length() == 0)
  {
    throw new IOException("Can not load metadata, resource name is empty!");
  }

  Scanner scan = null;
  try
  {
    scan = conn.createScanner(name, auths);
  }
  catch (Exception e)
  {
    throw new IOException("Can not connect to table " + name + " with auths " + auths + " - " + e.getMessage(), e);
  }

  MrsPyramidMetadata retMeta = null;
  Range range = new Range(MrGeoAccumuloConstants.MRGEO_ACC_METADATA, MrGeoAccumuloConstants.MRGEO_ACC_METADATA + " ");
  scan.setRange(range);
  scan.fetchColumn(new Text(MrGeoAccumuloConstants.MRGEO_ACC_METADATA),
      new Text(MrGeoAccumuloConstants.MRGEO_ACC_CQALL));
  for (Entry<Key, Value> entry : scan)
  {
    ByteArrayInputStream bis = new ByteArrayInputStream(entry.getValue().get());
    retMeta = MrsPyramidMetadata.load(bis);
    bis.close();
    break;
  }

  return retMeta;
}