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

The following examples show how to use org.apache.accumulo.core.client.Scanner#close() . 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: 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 2
Source File: IndexMetadataTableWrapper.java    From AccumuloGraph with Apache License 2.0 6 votes vote down vote up
public <T extends Element> Set<String> getIndexedKeys(Class<T> elementClass) {
  if (elementClass == null) {
    throw ExceptionFactory.classForElementCannotBeNull();
  }

  IndexedItemsListParser parser = new IndexedItemsListParser(elementClass);

  Scanner scan = null;
  try {
    scan = getScanner();
    scan.fetchColumnFamily(new Text(IndexMetadataEntryType.__INDEX_KEY__.name()));

    Set<String> keys = new HashSet<String>();
    for (IndexedItem item : parser.parse(scan)) {
      keys.add(item.getKey());
    }

    return keys;

  } finally {
    if (scan != null) {
      scan.close();
    }
  }
}
 
Example 3
Source File: IndexMetadataTableWrapper.java    From AccumuloGraph with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
public Iterable<Index<? extends Element>> getIndices() {
  List<Index<? extends Element>> indexes = new ArrayList<Index<? extends Element>>();

  IndexedItemsListParser parser = new IndexedItemsListParser();

  Scanner scan = null;
  try {
    scan = getScanner();
    scan.fetchColumnFamily(new Text(IndexMetadataEntryType.__INDEX_NAME__.name()));

    for (IndexedItem item : parser.parse(scan)) {
      indexes.add(new AccumuloIndex(globals,
          item.getKey(), item.getElementClass()));
    }

    return indexes;

  } finally {
    if (scan != null) {
      scan.close();
    }
  }
}
 
Example 4
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 5
Source File: AccumuloGraph.java    From vertexium with Apache License 2.0 6 votes vote down vote up
private long getRowCountFromTable(String tableName, Text signalColumn, Authorizations authorizations) {
    try {
        LOGGER.debug("BEGIN getRowCountFromTable(%s)", tableName);
        Scanner scanner = createScanner(tableName, null, authorizations);
        try {
            scanner.fetchColumnFamily(signalColumn);

            IteratorSetting countingIterator = new IteratorSetting(
                100,
                CountingIterator.class.getSimpleName(),
                CountingIterator.class
            );
            scanner.addScanIterator(countingIterator);

            GRAPH_LOGGER.logStartIterator(tableName, scanner);

            long count = 0;
            for (Map.Entry<Key, Value> entry : scanner) {
                Long countForKey = LongCombiner.FIXED_LEN_ENCODER.decode(entry.getValue().get());
                LOGGER.debug("getRowCountFromTable(%s): %s: %d", tableName, entry.getKey().getRow(), countForKey);
                count += countForKey;
            }
            LOGGER.debug("getRowCountFromTable(%s): TOTAL: %d", tableName, count);
            return count;
        } finally {
            scanner.close();
        }
    } catch (TableNotFoundException ex) {
        throw new VertexiumException("Could not get count from table: " + tableName, ex);
    }
}
 
Example 6
Source File: AbstractFunctionalQuery.java    From datawave with Apache License 2.0 6 votes vote down vote up
protected Multimap<String,Key> removeMetadataEntries(Set<String> fields, Text cf) throws AccumuloSecurityException, AccumuloException,
                TableNotFoundException {
    Multimap<String,Key> metadataEntries = HashMultimap.create();
    MultiTableBatchWriter multiTableWriter = connector.createMultiTableBatchWriter(new BatchWriterConfig());
    BatchWriter writer = multiTableWriter.getBatchWriter(QueryTestTableHelper.METADATA_TABLE_NAME);
    for (String field : fields) {
        Mutation mutation = new Mutation(new Text(field));
        Scanner scanner = connector.createScanner(QueryTestTableHelper.METADATA_TABLE_NAME, new Authorizations());
        scanner.fetchColumnFamily(cf);
        scanner.setRange(new Range(new Text(field)));
        boolean foundEntries = false;
        for (Map.Entry<Key,Value> entry : scanner) {
            foundEntries = true;
            metadataEntries.put(field, entry.getKey());
            mutation.putDelete(entry.getKey().getColumnFamily(), entry.getKey().getColumnQualifier(), entry.getKey().getColumnVisibilityParsed());
        }
        scanner.close();
        if (foundEntries) {
            writer.addMutation(mutation);
        }
    }
    writer.close();
    connector.tableOperations().compact(QueryTestTableHelper.METADATA_TABLE_NAME, new Text("\0"), new Text("~"), true, true);
    return metadataEntries;
}
 
Example 7
Source File: ElementTableWrapper.java    From AccumuloGraph with Apache License 2.0 6 votes vote down vote up
/**
 * Read the given properties for the given element.
 * If propertyKeys is null, read all properties.
 * If the element has no properties, return an empty Map.
 * If the element does not exist, return null.
 * @param id
 * @param propertyKeys
 * @return
 */
public Map<String, Object> readProperties(Element element, String[] propertyKeys) {
  Scanner s = getScanner();
  s.setRange(Range.exact((String) element.getId()));

  // If propertyKeys is null, we read everything.
  // Otherwise, limit to the given attributes.
  if (propertyKeys != null) {
    s.fetchColumnFamily(new Text(Constants.LABEL));

    for (String key : propertyKeys) {
      s.fetchColumnFamily(new Text(key));
    }
  }

  Map<String, Object> props = new PropertyParser().parse(s);
  s.close();

  return props;
}
 
Example 8
Source File: ElementTableWrapper.java    From AccumuloGraph with Apache License 2.0 6 votes vote down vote up
/**
 * Get all property keys for the given element id.
 * @param id
 * @return
 */
public Set<String> readPropertyKeys(Element element) {
  Scanner s = getScanner();

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

  Set<String> keys = new HashSet<String>();

  for (Entry<Key, Value> entry : s) {
    String cf = entry.getKey().getColumnFamily().toString();
    keys.add(cf);
  }

  s.close();

  // Remove some special keys.
  keys.remove(Constants.IN_EDGE);
  keys.remove(Constants.LABEL);
  keys.remove(Constants.OUT_EDGE);

  return keys;
}
 
Example 9
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 10
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 11
Source File: IndexMetadataTableWrapper.java    From AccumuloGraph with Apache License 2.0 5 votes vote down vote up
public <T extends Element> Index<T> getIndex(String indexName,
    Class<T> indexClass) {
  IndexedItemsListParser parser = new IndexedItemsListParser();

  Scanner scan = null;
  try {
    scan = getScanner();
    scan.fetchColumnFamily(new Text(IndexMetadataEntryType.__INDEX_NAME__.name()));

    for (IndexedItem item : parser.parse(scan)) {
      if (item.getKey().equals(indexName)) {
        if (item.getElementClass().equals(indexClass)) {
          return new AccumuloIndex<T>(globals, indexName,
              indexClass);
        }
        else {
          throw ExceptionFactory.indexDoesNotSupportClass(indexName, indexClass);
        }
      }
    }
    return null;

  } finally {
    if (scan != null) {
      scan.close();
    }
  }
}
 
Example 12
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 13
Source File: EdgeTableWrapper.java    From AccumuloGraph with Apache License 2.0 5 votes vote down vote up
public void loadEndpointsAndLabel(AccumuloEdge edge) {
  Scanner s = getScanner();

  try {
    s.setRange(new Range(edge.getId().toString()));
    s.fetchColumnFamily(new Text(Constants.LABEL));
    Iterator<Entry<Key,Value>> iter = s.iterator();
    if (!iter.hasNext()) {
      dump();
      throw new AccumuloGraphException("Unable to find edge row: "+edge);
    }

    Entry<Key, Value> entry = iter.next();

    String cq = entry.getKey().getColumnQualifier().toString();
    String[] ids = cq.split(Constants.ID_DELIM);

    String label = AccumuloByteSerializer.deserialize(entry.getValue().get());

    edge.setVertices(new AccumuloVertex(globals, ids[0]),
        new AccumuloVertex(globals, ids[1]));
    edge.setLabel(label);

  } finally {
    s.close();
  }
}
 
Example 14
Source File: AccumuloOperations.java    From geowave with Apache License 2.0 5 votes vote down vote up
public CloseableIterator<GeoWaveRow> getDataIndexResults(
    final short adapterId,
    final String... additionalAuthorizations) {
  final byte[] family = StringUtils.stringToBinary(ByteArrayUtils.shortToString(adapterId));

  // to have backwards compatibility before 1.8.0 we can assume BaseScanner is autocloseable
  final Scanner scanner;
  try {
    scanner = createScanner(DataIndexUtils.DATA_ID_INDEX.getName(), additionalAuthorizations);
    scanner.setRange(new Range());
    scanner.fetchColumnFamily(new Text(family));
    return new CloseableIteratorWrapper(new Closeable() {
      @Override
      public void close() throws IOException {
        scanner.close();
      }
    },
        Streams.stream(scanner).map(
            entry -> DataIndexUtils.deserializeDataIndexRow(
                entry.getKey().getRow().getBytes(),
                adapterId,
                entry.getValue().get(),
                false)).iterator());
  } catch (final TableNotFoundException e) {
    LOGGER.error("unable to find data index table", e);
  }
  return new CloseableIterator.Empty<>();
}
 
Example 15
Source File: AccumuloClient.java    From presto with Apache License 2.0 5 votes vote down vote up
private Optional<String> getDefaultTabletLocation(String fulltable)
{
    try {
        String tableId = connector.tableOperations().tableIdMap().get(fulltable);

        // Create a scanner over the metadata table, fetching the 'loc' column of the default tablet row
        Scanner scan = connector.createScanner("accumulo.metadata", connector.securityOperations().getUserAuthorizations(username));
        scan.fetchColumnFamily(new Text("loc"));
        scan.setRange(new Range(tableId + '<'));

        // scan the entry
        Optional<String> location = Optional.empty();
        for (Entry<Key, Value> entry : scan) {
            if (location.isPresent()) {
                throw new PrestoException(FUNCTION_IMPLEMENTATION_ERROR, "Scan for default tablet returned more than one entry");
            }

            location = Optional.of(entry.getValue().toString());
        }

        scan.close();
        return location;
    }
    catch (Exception e) {
        // Swallow this exception so the query does not fail due to being unable to locate the tablet server for the default tablet.
        // This is purely an optimization, but we will want to log the error.
        LOG.error("Failed to get tablet location, returning dummy location", e);
        return Optional.empty();
    }
}
 
Example 16
Source File: DataStoreImpl.java    From timely with Apache License 2.0 4 votes vote down vote up
@Override
public SearchLookupResponse lookup(SearchLookupRequest msg) throws TimelyException {
    long startMillis = System.currentTimeMillis();
    SearchLookupResponse result = new SearchLookupResponse();
    result.setType("LOOKUP");
    result.setMetric(msg.getQuery());
    Map<String, String> tags = new TreeMap<>();
    for (Tag tag : msg.getTags()) {
        tags.put(tag.getKey(), tag.getValue());
    }
    result.setTags(tags);
    result.setLimit(msg.getLimit());
    Map<String, Pattern> tagPatterns = new HashMap<>();
    tags.forEach((k, v) -> {
        tagPatterns.put(k, Pattern.compile(v));
    });
    try {
        final Scanner scanner = connector.createScanner(metaTable, Authorizations.EMPTY);
        try {
            List<Result> resultField = new ArrayList<>();
            Key start = new Key(Meta.VALUE_PREFIX + msg.getQuery());
            Key end = start.followingKey(PartialKey.ROW);
            Range range = new Range(start, end);
            scanner.setRange(range);
            tags.keySet().forEach(k -> scanner.fetchColumnFamily(new Text(k)));
            int total = 0;
            for (Entry<Key, Value> entry : scanner) {
                Meta metaEntry = Meta.parse(entry.getKey(), entry.getValue());
                if (matches(metaEntry.getTagKey(), metaEntry.getTagValue(), tagPatterns)) {
                    if (resultField.size() < msg.getLimit()) {
                        Result r = new Result();
                        r.putTag(metaEntry.getTagKey(), metaEntry.getTagValue());
                        resultField.add(r);
                    }
                    total++;
                }
            }
            result.setResults(resultField);
            result.setTotalResults(total);
            result.setTime((int) (System.currentTimeMillis() - startMillis));
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }
    } catch (Exception ex) {
        LOG.error("Error during lookup: " + ex.getMessage(), ex);
        throw new TimelyException(HttpResponseStatus.INTERNAL_SERVER_ERROR.code(),
                "Error during lookup: " + ex.getMessage(), ex.getMessage(), ex);
    }
    return result;
}
 
Example 17
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 18
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 19
Source File: DeleteHistoricalLegacyStreamingPropertyValueData.java    From vertexium with Apache License 2.0 4 votes vote down vote up
public void execute(Options options, Authorizations authorizations) {
    try {
        org.apache.accumulo.core.security.Authorizations accumuloAuthorizations = graph.toAccumuloAuthorizations(authorizations);
        Scanner scanner = graph.getConnector().createScanner(graph.getDataTableName(), accumuloAuthorizations);
        BatchWriter writer = graph.getConnector().createBatchWriter(
            graph.getDataTableName(),
            graph.getConfiguration().createBatchWriterConfig()
        );
        String lastRowIdPrefix = null;
        List<Key> rowsToDelete = new ArrayList<>();
        try {
            int rowCount = 0;
            for (Map.Entry<Key, Value> row : scanner) {
                if (rowCount % 10000 == 0) {
                    writer.flush();
                    LOGGER.debug("looking at row: %s (row count: %d)", row.getKey().getRow().toString(), rowCount);
                }
                rowCount++;
                if (!EMPTY_TEXT.equals(row.getKey().getColumnFamily())) {
                    continue;
                }
                if (!EMPTY_TEXT.equals(row.getKey().getColumnQualifier())) {
                    continue;
                }

                String rowId = row.getKey().getRow().toString();
                String[] rowIdParts = rowId.split("" + DataTableRowKey.VALUE_SEPARATOR);
                if (rowIdParts.length < 3) {
                    continue;
                }

                if (lastRowIdPrefix == null || !isSameProperty(lastRowIdPrefix, rowId)) {
                    deleteRows(writer, rowsToDelete, options);
                    rowsToDelete.clear();
                    lastRowIdPrefix = rowIdParts[0]
                        + DataTableRowKey.VALUE_SEPARATOR
                        + rowIdParts[1]
                        + DataTableRowKey.VALUE_SEPARATOR
                        + rowIdParts[2];
                }
                rowsToDelete.add(row.getKey());
            }
            deleteRows(writer, rowsToDelete, options);
        } finally {
            writer.flush();
            scanner.close();
        }
    } catch (Exception ex) {
        throw new VertexiumException("Could not delete old SPV data", ex);
    }
}
 
Example 20
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();
        }
    }
}