Example usage for org.apache.hadoop.io Text compareTo

List of usage examples for org.apache.hadoop.io Text compareTo

Introduction

In this page you can find the example usage for org.apache.hadoop.io Text compareTo.

Prototype

@Override
public int compareTo(BinaryComparable other) 

Source Link

Document

Compare bytes from {#getBytes()}.

Usage

From source file:it.crs4.seal.tsv_sort.TextSampler.java

License:Apache License

public int compare(int i, int j) {
    Text left = records.get(i);
    Text right = records.get(j);//  w  ww .  ja v a 2 s .com
    return left.compareTo(right);
}

From source file:mapreducesentiment.SentimentKeyWritableComparable.java

@Override
public int compareTo(Object o) {
    SentimentKeyWritableComparable sentiment = (SentimentKeyWritableComparable) o;
    Text thisValue = this.productId;
    Text thatValue = sentiment.productId;

    return this.equals(o) ? 0 : (thatValue.compareTo(thisValue) == 0 ? -1 : 1);
}

From source file:mvm.rya.api.query.strategy.wholerow.HashedPoWholeRowTriplePatternStrategyTest.java

License:Apache License

private void assertContains(ByteRange value, byte[] row) {
    Text rowText = new Text(row);
    Text startText = new Text(value.getStart());
    Text endText = new Text(value.getEnd());
    assertTrue((startText.compareTo(rowText) <= 0) && (endText.compareTo(rowText) >= 0));
}

From source file:mvm.rya.api.query.strategy.wholerow.HashedPoWholeRowTriplePatternStrategyTest.java

License:Apache License

private void assertContainsFalse(ByteRange value, byte[] row) {
    Text rowText = new Text(row);
    Text startText = new Text(value.getStart());
    Text endText = new Text(value.getEnd());
    assertFalse((startText.compareTo(rowText) <= 0) && (endText.compareTo(rowText) >= 0));
}

From source file:org.apache.accumulo.core.client.admin.FindMax.java

License:Apache License

private static Text findMidPoint(Text minBS, Text maxBS) {
    ByteArrayOutputStream startOS = new ByteArrayOutputStream();
    startOS.write(0); // add a leading zero so bigint does not think its negative
    startOS.write(minBS.getBytes(), 0, minBS.getLength());

    ByteArrayOutputStream endOS = new ByteArrayOutputStream();
    endOS.write(0);// add a leading zero so bigint does not think its negative
    endOS.write(maxBS.getBytes(), 0, maxBS.getLength());

    // make the numbers of the same magnitude
    if (startOS.size() < endOS.size())
        appendZeros(startOS, endOS.size() - startOS.size());
    else if (endOS.size() < startOS.size())
        appendZeros(endOS, startOS.size() - endOS.size());

    BigInteger min = new BigInteger(startOS.toByteArray());
    BigInteger max = new BigInteger(endOS.toByteArray());

    BigInteger mid = max.subtract(min).divide(BigInteger.valueOf(2)).add(min);

    byte[] ba = mid.toByteArray();

    Text ret = new Text();

    if (ba.length == startOS.size()) {
        if (ba[0] != 0)
            throw new RuntimeException();

        // big int added a zero so it would not be negative, drop it
        ret.set(ba, 1, ba.length - 1);// www  .  ja  v a2 s  .c  o m
    } else {
        int expLen = Math.max(minBS.getLength(), maxBS.getLength());
        // big int will drop leading 0x0 bytes
        for (int i = ba.length; i < expLen; i++) {
            ret.append(new byte[] { 0 }, 0, 1);
        }

        ret.append(ba, 0, ba.length);
    }

    // remove trailing 0x0 bytes
    while (ret.getLength() > 0 && ret.getBytes()[ret.getLength() - 1] == 0 && ret.compareTo(minBS) > 0) {
        Text t = new Text();
        t.set(ret.getBytes(), 0, ret.getLength() - 1);
        ret = t;
    }

    return ret;
}

From source file:org.apache.accumulo.core.client.admin.FindMax.java

License:Apache License

private static Text _findMax(Scanner scanner, Text start, boolean inclStart, Text end, boolean inclEnd) {

    // System.out.printf("findMax(%s, %s, %s, %s)%n", Key.toPrintableString(start.getBytes(), 0, start.getLength(), 1000), inclStart,
    // Key.toPrintableString(end.getBytes(), 0, end.getLength(), 1000), inclEnd);

    int cmp = start.compareTo(end);

    if (cmp >= 0) {
        if (inclStart && inclEnd && cmp == 0) {
            scanner.setRange(new Range(start, true, end, true));
            Iterator<Entry<Key, Value>> iter = scanner.iterator();
            if (iter.hasNext())
                return iter.next().getKey().getRow();
        }//from  w  w  w  . jav  a 2s  . c  o  m

        return null;
    }

    Text mid = findMidPoint(start, end);
    // System.out.println("mid = :"+Key.toPrintableString(mid.getBytes(), 0, mid.getLength(), 1000)+":");

    scanner.setRange(new Range(mid, mid.equals(start) ? inclStart : true, end, inclEnd));

    Iterator<Entry<Key, Value>> iter = scanner.iterator();

    if (iter.hasNext()) {
        Key next = iter.next().getKey();

        int count = 0;
        while (count < 10 && iter.hasNext()) {
            next = iter.next().getKey();
            count++;
        }

        if (!iter.hasNext())
            return next.getRow();

        Text ret = _findMax(scanner, next.followingKey(PartialKey.ROW).getRow(), true, end, inclEnd);
        if (ret == null)
            return next.getRow();
        else
            return ret;
    } else {

        return _findMax(scanner, start, inclStart, mid, mid.equals(start) ? inclStart : false);
    }
}

From source file:org.apache.accumulo.core.client.impl.BulkImport.java

License:Apache License

public static List<KeyExtent> findOverlappingTablets(ClientContext context, KeyExtentCache extentCache,
        Text startRow, Text endRow, FileSKVIterator reader)
        throws IOException, AccumuloException, AccumuloSecurityException, TableNotFoundException {
    List<KeyExtent> result = new ArrayList<>();
    Collection<ByteSequence> columnFamilies = Collections.emptyList();
    Text row = startRow;
    if (row == null)
        row = new Text();
    while (true) {
        // log.debug(filename + " Seeking to row " + row);
        reader.seek(new Range(row, null), columnFamilies, false);
        if (!reader.hasTop()) {
            // log.debug(filename + " not found");
            break;
        }/* w w w.j  a  va2  s.co m*/
        row = reader.getTopKey().getRow();
        KeyExtent extent = extentCache.lookup(row);
        // log.debug(filename + " found row " + row + " at location " + tabletLocation);
        result.add(extent);
        row = extent.getEndRow();
        if (row != null && (endRow == null || row.compareTo(endRow) < 0)) {
            row = new Text(row);
            row.append(byte0, 0, byte0.length);
        } else
            break;
    }

    return result;
}

From source file:org.apache.accumulo.core.client.impl.TabletLocatorImpl.java

License:Apache License

private void lookupTabletLocation(ClientContext context, Text row, boolean retry, LockCheckerSession lcSession)
        throws AccumuloException, AccumuloSecurityException, TableNotFoundException {
    Text metadataRow = new Text(tableId);
    metadataRow.append(new byte[] { ';' }, 0, 1);
    metadataRow.append(row.getBytes(), 0, row.getLength());
    TabletLocation ptl = parent.locateTablet(context, metadataRow, false, retry);

    if (ptl != null) {
        TabletLocations locations = locationObtainer.lookupTablet(context, ptl, metadataRow, lastTabletRow,
                parent);/*from   w  ww  . ja  va  2s  . c  o m*/
        while (locations != null && locations.getLocations().isEmpty()
                && locations.getLocationless().isEmpty()) {
            // try the next tablet, the current tablet does not have any tablets that overlap the row
            Text er = ptl.tablet_extent.getEndRow();
            if (er != null && er.compareTo(lastTabletRow) < 0) {
                // System.out.println("er "+er+"  ltr "+lastTabletRow);
                ptl = parent.locateTablet(context, er, true, retry);
                if (ptl != null)
                    locations = locationObtainer.lookupTablet(context, ptl, metadataRow, lastTabletRow, parent);
                else
                    break;
            } else {
                break;
            }
        }

        if (locations == null)
            return;

        // cannot assume the list contains contiguous key extents... so it is probably
        // best to deal with each extent individually

        Text lastEndRow = null;
        for (TabletLocation tabletLocation : locations.getLocations()) {

            KeyExtent ke = tabletLocation.tablet_extent;
            TabletLocation locToCache;

            // create new location if current prevEndRow == endRow
            if ((lastEndRow != null) && (ke.getPrevEndRow() != null) && ke.getPrevEndRow().equals(lastEndRow)) {
                locToCache = new TabletLocation(new KeyExtent(ke.getTableId(), ke.getEndRow(), lastEndRow),
                        tabletLocation.tablet_location, tabletLocation.tablet_session);
            } else {
                locToCache = tabletLocation;
            }

            // save endRow for next iteration
            lastEndRow = locToCache.tablet_extent.getEndRow();

            updateCache(locToCache, lcSession);
        }
    }

}

From source file:org.apache.accumulo.core.clientImpl.bulk.BulkImport.java

License:Apache License

public static List<KeyExtent> findOverlappingTablets(KeyExtentCache extentCache, FileSKVIterator reader)
        throws IOException, AccumuloException, AccumuloSecurityException, TableNotFoundException {

    Text startRow = null;//from  w  w w .j  a v a  2s . com
    Text endRow = null;

    List<KeyExtent> result = new ArrayList<>();
    Collection<ByteSequence> columnFamilies = Collections.emptyList();
    Text row = startRow;
    if (row == null)
        row = new Text();
    while (true) {
        // log.debug(filename + " Seeking to row " + row);
        reader.seek(new Range(row, null), columnFamilies, false);
        if (!reader.hasTop()) {
            // log.debug(filename + " not found");
            break;
        }
        row = reader.getTopKey().getRow();
        KeyExtent extent = extentCache.lookup(row);
        // log.debug(filename + " found row " + row + " at location " + tabletLocation);
        result.add(extent);
        row = extent.getEndRow();
        if (row != null && (endRow == null || row.compareTo(endRow) < 0)) {
            row = nextRow(row);
        } else
            break;
    }

    return result;
}

From source file:org.apache.accumulo.core.clientImpl.BulkImport.java

License:Apache License

public static List<KeyExtent> findOverlappingTablets(ClientContext context, KeyExtentCache extentCache,
        FileSKVIterator reader)//from  ww w. j av a  2  s.  c o  m
        throws IOException, AccumuloException, AccumuloSecurityException, TableNotFoundException {

    Text startRow = null;
    Text endRow = null;

    List<KeyExtent> result = new ArrayList<>();
    Collection<ByteSequence> columnFamilies = Collections.emptyList();
    Text row = startRow;
    if (row == null)
        row = new Text();
    while (true) {
        // log.debug(filename + " Seeking to row " + row);
        reader.seek(new Range(row, null), columnFamilies, false);
        if (!reader.hasTop()) {
            // log.debug(filename + " not found");
            break;
        }
        row = reader.getTopKey().getRow();
        KeyExtent extent = extentCache.lookup(row);
        // log.debug(filename + " found row " + row + " at location " + tabletLocation);
        result.add(extent);
        row = extent.getEndRow();
        if (row != null && (endRow == null || row.compareTo(endRow) < 0)) {
            row = nextRow(row);
        } else
            break;
    }

    return result;
}