Search in sources :

Example 16 with RowIterator

use of org.apache.accumulo.core.client.RowIterator in project accumulo by apache.

the class TabletStateChangeIteratorIT method copyTable.

/**
 * Create a copy of the source table by first gathering all the rows of the source in a list of
 * mutations. Then create the copy of the table and apply the mutations to the copy.
 */
private void copyTable(AccumuloClient client, String source, String copy) throws AccumuloException, AccumuloSecurityException, TableNotFoundException, TableExistsException {
    try {
        dropTables(client, copy);
    } catch (TableNotFoundException ex) {
    // ignored
    }
    log.info("Gathering rows to copy {} ", source);
    List<Mutation> mutations = new ArrayList<>();
    try (Scanner scanner = client.createScanner(source, Authorizations.EMPTY)) {
        RowIterator rows = new RowIterator(new IsolatedScanner(scanner));
        while (rows.hasNext()) {
            Iterator<Entry<Key, Value>> row = rows.next();
            Mutation m = null;
            while (row.hasNext()) {
                Entry<Key, Value> entry = row.next();
                Key k = entry.getKey();
                if (m == null)
                    m = new Mutation(k.getRow());
                m.put(k.getColumnFamily(), k.getColumnQualifier(), k.getColumnVisibilityParsed(), k.getTimestamp(), entry.getValue());
            }
            mutations.add(m);
        }
    }
    // metadata should be stable with only 7 rows (1 replication + 2 for each table)
    log.debug("Gathered {} rows to create copy {}", mutations.size(), copy);
    assertEquals("Metadata should have 7 rows (1 repl + 2 for each table)", 7, mutations.size());
    client.tableOperations().create(copy);
    try (BatchWriter writer = client.createBatchWriter(copy)) {
        for (Mutation m : mutations) {
            writer.addMutation(m);
        }
    }
    log.info("Finished creating copy " + copy);
}
Also used : MetaDataTableScanner(org.apache.accumulo.server.manager.state.MetaDataTableScanner) IsolatedScanner(org.apache.accumulo.core.client.IsolatedScanner) Scanner(org.apache.accumulo.core.client.Scanner) ArrayList(java.util.ArrayList) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) Entry(java.util.Map.Entry) RowIterator(org.apache.accumulo.core.client.RowIterator) Value(org.apache.accumulo.core.data.Value) Mutation(org.apache.accumulo.core.data.Mutation) BatchWriter(org.apache.accumulo.core.client.BatchWriter) IsolatedScanner(org.apache.accumulo.core.client.IsolatedScanner) Key(org.apache.accumulo.core.data.Key)

Aggregations

RowIterator (org.apache.accumulo.core.client.RowIterator)16 Key (org.apache.accumulo.core.data.Key)13 Value (org.apache.accumulo.core.data.Value)13 Scanner (org.apache.accumulo.core.client.Scanner)12 Entry (java.util.Map.Entry)11 Text (org.apache.hadoop.io.Text)10 IsolatedScanner (org.apache.accumulo.core.client.IsolatedScanner)9 AccumuloException (org.apache.accumulo.core.client.AccumuloException)6 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)6 PartialKey (org.apache.accumulo.core.data.PartialKey)6 Range (org.apache.accumulo.core.data.Range)6 ArrayList (java.util.ArrayList)5 KeyExtent (org.apache.accumulo.core.data.impl.KeyExtent)5 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)4 BatchScanner (org.apache.accumulo.core.client.BatchScanner)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3