Wednesday, April 11, 2012

Reading A Row With Accumulo

Just another example of reading information with Accumulo. This example needs the accumulo-core and hadoop-core jar files.
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

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.TableExistsException;
import org.apache.accumulo.core.client.TableNotFoundException;
import org.apache.accumulo.core.client.ZooKeeperInstance;
import org.apache.accumulo.core.data.Key;
import org.apache.accumulo.core.data.Range;
import org.apache.accumulo.core.data.Value;
import org.apache.accumulo.core.security.Authorizations;
import org.apache.hadoop.io.Text;

public class Accumulo_Scan_A_Row_Driver {

 public static void main(String[] args) throws AccumuloException, AccumuloSecurityException, TableNotFoundException, TableExistsException {
  String instanceName = "development";
  String zooKeepers = "localhost";
  String user = "root";
  byte[] pass = "password".getBytes();
  String tableName = "user";
  String rowId = "John";

  ZooKeeperInstance instance = new ZooKeeperInstance(instanceName, zooKeepers);
  Connector connector = instance.getConnector(user, pass);

  Scanner scan = connector.createScanner(tableName, new Authorizations());
  scan.setRange(new Range(rowId, rowId));

  Iterator<Map.Entry<Key,Value>> iterator = scan.iterator();
  while (iterator.hasNext()) {
   Map.Entry<Key,Value> entry = iterator.next();
   Key key = entry.getKey();
   Value value = entry.getValue();
   System.out.println(key + " ==> " + value);
  }
 }
}
Post a Comment