Search in sources :

Example 11 with RowIterator

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

the class AccumuloRowInputFormat method getRecordReader.

@Override
public RecordReader<Text, PeekingIterator<Entry<Key, Value>>> getRecordReader(InputSplit split, JobConf job, Reporter reporter) throws IOException {
    log.setLevel(getLogLevel(job));
    RecordReaderBase<Text, PeekingIterator<Entry<Key, Value>>> recordReader = new RecordReaderBase<>() {

        RowIterator rowIterator;

        @Override
        public void initialize(InputSplit inSplit, JobConf job) throws IOException {
            super.initialize(inSplit, job);
            rowIterator = new RowIterator(scannerIterator);
        }

        @Override
        public boolean next(Text key, PeekingIterator<Entry<Key, Value>> value) throws IOException {
            if (!rowIterator.hasNext())
                return false;
            value.initialize(rowIterator.next());
            numKeysRead = rowIterator.getKVCount();
            key.set((currentKey = value.peek().getKey()).getRow());
            return true;
        }

        @Override
        public Text createKey() {
            return new Text();
        }

        @Override
        public PeekingIterator<Entry<Key, Value>> createValue() {
            return new PeekingIterator<>();
        }
    };
    recordReader.initialize(split, job);
    return recordReader;
}
Also used : Entry(java.util.Map.Entry) RowIterator(org.apache.accumulo.core.client.RowIterator) Value(org.apache.accumulo.core.data.Value) Text(org.apache.hadoop.io.Text) PeekingIterator(org.apache.accumulo.core.util.PeekingIterator) InputSplit(org.apache.hadoop.mapred.InputSplit) JobConf(org.apache.hadoop.mapred.JobConf) Key(org.apache.accumulo.core.data.Key)

Example 12 with RowIterator

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

the class AccumuloRowInputFormat method createRecordReader.

@Override
public RecordReader<Text, PeekingIterator<Entry<Key, Value>>> createRecordReader(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {
    log.setLevel(getLogLevel(context));
    return new RecordReaderBase<>() {

        RowIterator rowIterator;

        @Override
        public void initialize(InputSplit inSplit, TaskAttemptContext attempt) throws IOException {
            super.initialize(inSplit, attempt);
            rowIterator = new RowIterator(scannerIterator);
            currentK = new Text();
            currentV = null;
        }

        @Override
        public boolean nextKeyValue() throws IOException, InterruptedException {
            if (!rowIterator.hasNext())
                return false;
            currentV = new PeekingIterator<>(rowIterator.next());
            numKeysRead = rowIterator.getKVCount();
            currentKey = currentV.peek().getKey();
            currentK = new Text(currentKey.getRow());
            return true;
        }
    };
}
Also used : RowIterator(org.apache.accumulo.core.client.RowIterator) TaskAttemptContext(org.apache.hadoop.mapreduce.TaskAttemptContext) Text(org.apache.hadoop.io.Text) InputSplit(org.apache.hadoop.mapreduce.InputSplit)

Example 13 with RowIterator

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

the class InputConfigurator method binOffline.

public static Map<String, Map<KeyExtent, List<Range>>> binOffline(TableId tableId, List<Range> ranges, ClientContext context) throws AccumuloException, TableNotFoundException {
    Map<String, Map<KeyExtent, List<Range>>> binnedRanges = new HashMap<>();
    if (context.getTableState(tableId) != TableState.OFFLINE) {
        context.clearTableListCache();
        if (context.getTableState(tableId) != TableState.OFFLINE) {
            throw new AccumuloException("Table is online tableId:" + tableId + " cannot scan table in offline mode ");
        }
    }
    for (Range range : ranges) {
        Text startRow;
        if (range.getStartKey() != null)
            startRow = range.getStartKey().getRow();
        else
            startRow = new Text();
        Range metadataRange = new Range(new KeyExtent(tableId, startRow, null).toMetaRow(), true, null, false);
        Scanner scanner = context.createScanner(MetadataTable.NAME, Authorizations.EMPTY);
        TabletColumnFamily.PREV_ROW_COLUMN.fetch(scanner);
        scanner.fetchColumnFamily(LastLocationColumnFamily.NAME);
        scanner.fetchColumnFamily(CurrentLocationColumnFamily.NAME);
        scanner.fetchColumnFamily(FutureLocationColumnFamily.NAME);
        scanner.setRange(metadataRange);
        RowIterator rowIter = new RowIterator(scanner);
        KeyExtent lastExtent = null;
        while (rowIter.hasNext()) {
            Iterator<Map.Entry<Key, Value>> row = rowIter.next();
            String last = "";
            KeyExtent extent = null;
            String location = null;
            while (row.hasNext()) {
                Map.Entry<Key, Value> entry = row.next();
                Key key = entry.getKey();
                if (key.getColumnFamily().equals(LastLocationColumnFamily.NAME)) {
                    last = entry.getValue().toString();
                }
                if (key.getColumnFamily().equals(CurrentLocationColumnFamily.NAME) || key.getColumnFamily().equals(FutureLocationColumnFamily.NAME)) {
                    location = entry.getValue().toString();
                }
                if (TabletColumnFamily.PREV_ROW_COLUMN.hasColumns(key)) {
                    extent = KeyExtent.fromMetaPrevRow(entry);
                }
            }
            if (location != null)
                return null;
            if (!extent.tableId().equals(tableId)) {
                throw new AccumuloException("Saw unexpected table Id " + tableId + " " + extent);
            }
            if (lastExtent != null && !extent.isPreviousExtent(lastExtent)) {
                throw new AccumuloException(" " + lastExtent + " is not previous extent " + extent);
            }
            binnedRanges.computeIfAbsent(last, k -> new HashMap<>()).computeIfAbsent(extent, k -> new ArrayList<>()).add(range);
            if (extent.endRow() == null || range.afterEndKey(new Key(extent.endRow()).followingKey(PartialKey.ROW))) {
                break;
            }
            lastExtent = extent;
        }
    }
    return binnedRanges;
}
Also used : TableId(org.apache.accumulo.core.data.TableId) Text(org.apache.hadoop.io.Text) MetadataTable(org.apache.accumulo.core.metadata.MetadataTable) Writable(org.apache.hadoop.io.Writable) TextUtil(org.apache.accumulo.core.util.TextUtil) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) BatchScanner(org.apache.accumulo.core.client.BatchScanner) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) NamespacePermission(org.apache.accumulo.core.security.NamespacePermission) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) CurrentLocationColumnFamily(org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.CurrentLocationColumnFamily) Map(java.util.Map) Configuration(org.apache.hadoop.conf.Configuration) Value(org.apache.accumulo.core.data.Value) SamplerConfiguration(org.apache.accumulo.core.client.sample.SamplerConfiguration) TableState(org.apache.accumulo.core.manager.state.tables.TableState) ClientContext(org.apache.accumulo.core.clientImpl.ClientContext) Collection(java.util.Collection) Set(java.util.Set) AccumuloClient(org.apache.accumulo.core.client.AccumuloClient) Base64(java.util.Base64) List(java.util.List) Pair(org.apache.accumulo.core.util.Pair) ClientSideIteratorScanner(org.apache.accumulo.core.client.ClientSideIteratorScanner) IsolatedScanner(org.apache.accumulo.core.client.IsolatedScanner) Scanner(org.apache.accumulo.core.client.Scanner) DataInputStream(java.io.DataInputStream) SamplerConfigurationImpl(org.apache.accumulo.core.sample.impl.SamplerConfigurationImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) MapWritable(org.apache.hadoop.io.MapWritable) SortedKeyValueIterator(org.apache.accumulo.core.iterators.SortedKeyValueIterator) FutureLocationColumnFamily(org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.FutureLocationColumnFamily) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LastLocationColumnFamily(org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.LastLocationColumnFamily) TablePermission(org.apache.accumulo.core.security.TablePermission) StringUtils(org.apache.hadoop.util.StringUtils) StringTokenizer(java.util.StringTokenizer) Objects.requireNonNull(java.util.Objects.requireNonNull) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) Key(org.apache.accumulo.core.data.Key) Iterator(java.util.Iterator) UTF_8(java.nio.charset.StandardCharsets.UTF_8) KeyExtent(org.apache.accumulo.core.dataImpl.KeyExtent) IOException(java.io.IOException) Authorizations(org.apache.accumulo.core.security.Authorizations) Maps(com.google.common.collect.Maps) AccumuloException(org.apache.accumulo.core.client.AccumuloException) Range(org.apache.accumulo.core.data.Range) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) TabletColumnFamily(org.apache.accumulo.core.metadata.schema.MetadataSchema.TabletsSection.TabletColumnFamily) PartialKey(org.apache.accumulo.core.data.PartialKey) Collections(java.util.Collections) RowIterator(org.apache.accumulo.core.client.RowIterator) AccumuloException(org.apache.accumulo.core.client.AccumuloException) BatchScanner(org.apache.accumulo.core.client.BatchScanner) ClientSideIteratorScanner(org.apache.accumulo.core.client.ClientSideIteratorScanner) IsolatedScanner(org.apache.accumulo.core.client.IsolatedScanner) Scanner(org.apache.accumulo.core.client.Scanner) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Text(org.apache.hadoop.io.Text) Range(org.apache.accumulo.core.data.Range) KeyExtent(org.apache.accumulo.core.dataImpl.KeyExtent) RowIterator(org.apache.accumulo.core.client.RowIterator) Value(org.apache.accumulo.core.data.Value) Map(java.util.Map) HashMap(java.util.HashMap) Key(org.apache.accumulo.core.data.Key) PartialKey(org.apache.accumulo.core.data.PartialKey)

Example 14 with RowIterator

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

the class AccumuloRowInputFormat method getRecordReader.

@Override
public RecordReader<Text, PeekingIterator<Entry<Key, Value>>> getRecordReader(InputSplit split, JobConf job, Reporter reporter) throws IOException {
    AccumuloRecordReader<Text, PeekingIterator<Entry<Key, Value>>> recordReader = new AccumuloRecordReader<>(CLASS) {

        RowIterator rowIterator;

        @Override
        public void initialize(InputSplit inSplit, JobConf job) throws IOException {
            super.initialize(inSplit, job);
            rowIterator = new RowIterator(scannerIterator);
        }

        @Override
        public boolean next(Text key, PeekingIterator<Entry<Key, Value>> value) {
            if (!rowIterator.hasNext())
                return false;
            value.initialize(rowIterator.next());
            numKeysRead = rowIterator.getKVCount();
            key.set((currentKey = value.peek().getKey()).getRow());
            return true;
        }

        @Override
        public Text createKey() {
            return new Text();
        }

        @Override
        public PeekingIterator<Entry<Key, Value>> createValue() {
            return new PeekingIterator<>();
        }
    };
    recordReader.initialize(split, job);
    return recordReader;
}
Also used : Entry(java.util.Map.Entry) RowIterator(org.apache.accumulo.core.client.RowIterator) Value(org.apache.accumulo.core.data.Value) Text(org.apache.hadoop.io.Text) PeekingIterator(org.apache.accumulo.core.util.PeekingIterator) AccumuloRecordReader(org.apache.accumulo.hadoopImpl.mapred.AccumuloRecordReader) InputSplit(org.apache.hadoop.mapred.InputSplit) JobConf(org.apache.hadoop.mapred.JobConf) Key(org.apache.accumulo.core.data.Key)

Example 15 with RowIterator

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

the class ConditionalWriterIT method testThreads.

@Test
public void testThreads() throws Exception {
    // test multiple threads using a single conditional writer
    String tableName = getUniqueNames(1)[0];
    try (AccumuloClient client = Accumulo.newClient().from(getClientProps()).build()) {
        NewTableConfiguration ntc = new NewTableConfiguration();
        switch(random.nextInt(3)) {
            case 1:
                ntc = ntc.withSplits(nss("4"));
                break;
            case 2:
                ntc = ntc.withSplits(nss("3", "5"));
                break;
        }
        client.tableOperations().create(tableName, ntc);
        try (ConditionalWriter cw = client.createConditionalWriter(tableName)) {
            ArrayList<ByteSequence> rows = new ArrayList<>();
            for (int i = 0; i < 1000; i++) {
                rows.add(new ArrayByteSequence(FastFormat.toZeroPaddedString(abs(random.nextLong()), 16, 16, new byte[0])));
            }
            ArrayList<ConditionalMutation> mutations = new ArrayList<>();
            for (ByteSequence row : rows) mutations.add(new Stats(row).toMutation());
            ArrayList<ByteSequence> rows2 = new ArrayList<>();
            Iterator<Result> results = cw.write(mutations.iterator());
            while (results.hasNext()) {
                Result result = results.next();
                assertEquals(Status.ACCEPTED, result.getStatus());
                rows2.add(new ArrayByteSequence(result.getMutation().getRow()));
            }
            Collections.sort(rows);
            Collections.sort(rows2);
            assertEquals(rows, rows2);
            AtomicBoolean failed = new AtomicBoolean(false);
            ExecutorService tp = Executors.newFixedThreadPool(5);
            for (int i = 0; i < 5; i++) {
                tp.submit(new MutatorTask(tableName, client, rows, cw, failed));
            }
            tp.shutdown();
            while (!tp.isTerminated()) {
                tp.awaitTermination(1, TimeUnit.MINUTES);
            }
            assertFalse("A MutatorTask failed with an exception", failed.get());
        }
        try (Scanner scanner = client.createScanner(tableName, Authorizations.EMPTY)) {
            RowIterator rowIter = new RowIterator(scanner);
            while (rowIter.hasNext()) {
                Iterator<Entry<Key, Value>> row = rowIter.next();
                new Stats(row);
            }
        }
    }
}
Also used : AccumuloClient(org.apache.accumulo.core.client.AccumuloClient) IsolatedScanner(org.apache.accumulo.core.client.IsolatedScanner) Scanner(org.apache.accumulo.core.client.Scanner) ArrayList(java.util.ArrayList) AlphaNumKeyConstraint(org.apache.accumulo.test.constraints.AlphaNumKeyConstraint) Result(org.apache.accumulo.core.client.ConditionalWriter.Result) ConditionalWriter(org.apache.accumulo.core.client.ConditionalWriter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConditionalMutation(org.apache.accumulo.core.data.ConditionalMutation) Entry(java.util.Map.Entry) NewTableConfiguration(org.apache.accumulo.core.client.admin.NewTableConfiguration) RowIterator(org.apache.accumulo.core.client.RowIterator) ExecutorService(java.util.concurrent.ExecutorService) ArrayByteSequence(org.apache.accumulo.core.data.ArrayByteSequence) ByteSequence(org.apache.accumulo.core.data.ByteSequence) ArrayByteSequence(org.apache.accumulo.core.data.ArrayByteSequence) Test(org.junit.Test)

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