Home | About | Sematext search-lucene.com search-hadoop.com
 Search Hadoop and all its subprojects:

Switch to Threaded View
Accumulo, mail # user - ROW ID Iterator - sanity check


Copy link to this message
-
Re: ROW ID Iterator - sanity check
Adam Fuchs 2012-05-19, 23:49
One issue here is you are mixing Iterator and Iterable in the same object.
Usually, an Iterable will return an iterator at the beginning of some
logical sequence, but your iterable returns the same iterator object over
and over again. This state sharing would make it so that you can really
only iterate over the iterable once. In your iterator() method you might
instead "return new RowIdIterator(scanner)", and that would properly
separate the state of the different iterators.

To test this, you could construct a unit test that starts with a
MockInstance, adds some data, then checks to see that the row ids come out
as expected with code similar to your main method.

We can also talk about how to make this more efficient with an iterator if
you like.

Cheers,
Adam
On Sat, May 19, 2012 at 6:10 PM, David Medinets <[EMAIL PROTECTED]>wrote:

> I wanted a program to display Row Id values in the simplest way
> possible. Please let me know if I have overlooked something. First, i
> wrapped the RowIterator like this;
>
> package com.codebits.accumulo;
>
> import java.util.Iterator;
> import java.util.Map.Entry;
>
> import org.apache.accumulo.core.client.RowIterator;
> import org.apache.accumulo.core.client.Scanner;
> import org.apache.accumulo.core.data.Key;
> import org.apache.accumulo.core.data.Value;
>
> public class RowIdIterator implements Iterator<String>, Iterable<String> {
>
>        Scanner scanner = null;
>        RowIterator iterator = null;
>
>        public RowIdIterator(Scanner scanner) {
>                super();
>                this.scanner = scanner;
>                this.iterator = new RowIterator(scanner);
>        }
>
>        @Override
>        public boolean hasNext() {
>                return iterator.hasNext();
>        }
>
>        @Override
>        public String next() {
>                Iterator<Entry<Key, Value>> entry = iterator.next();
>                return entry.next().getKey().getRow().toString();
>        }
>
>        @Override
>        public void remove() {
>        }
>
>        @Override
>        public Iterator<String> iterator() {
>                return this;
>        }
> }
>
> And then I used a driver program like this;
>
> package com.codebits.accumulo;
>
> import org.apache.accumulo.core.client.AccumuloException;
> import org.apache.accumulo.core.client.AccumuloSecurityException;
> import org.apache.accumulo.core.client.Connector;
> import org.apache.accumulo.core.client.Scanner;
> import org.apache.accumulo.core.client.TableNotFoundException;
> import org.apache.accumulo.core.client.ZooKeeperInstance;
> import org.apache.accumulo.core.security.Authorizations;
>
> public class RowIdInteratorDriver {
>
>  public static void main(String[] args) throws AccumuloException,
> AccumuloSecurityException, TableNotFoundException {
>        String instanceName = "development";
>        String zooKeepers = "localhost";
>        String user = "root";
>        byte[] pass = "password".getBytes();
>        String tableName = "test_row_iterator";
>        Authorizations authorizations = new Authorizations();
>
>        ZooKeeperInstance instance = new
> ZooKeeperInstance(instanceName, zooKeepers);
>        Connector connector = instance.getConnector(user, pass);
>        Scanner scanner = connector.createScanner(tableName,
> authorizations);
>
>        for (String rowId : new RowIdIterator(scanner)) {
>                System.out.println("ROW ID: " + rowId);
>        }
>  }
>
> }
>
> This code works:
>
> ROW ID: R001
> ROW ID: R002
> ROW ID: R003
>
> My concern is that scanner that I am passing into the iterator. How is
> that testable? And, of course, the class name is confusing..
>