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

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

Introduction

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

Prototype

public void set(byte[] utf8, int start, int len) 

Source Link

Document

Set the Text to range of bytes

Usage

From source file:org.apache.accumulo.core.dataImpl.KeyExtent.java

License:Apache License

/**
 * Populates the extent's fields based on a flatted extent
 *
 *///from   www  .ja v a 2s  .  c o m
private void decodeMetadataRow(Text flattenedExtent) {
    int semiPos = -1;
    int ltPos = -1;

    for (int i = 0; i < flattenedExtent.getLength(); i++) {
        if (flattenedExtent.getBytes()[i] == ';' && semiPos < 0) {
            // want the position of the first semicolon
            semiPos = i;
        }

        if (flattenedExtent.getBytes()[i] == '<') {
            ltPos = i;
        }
    }

    if (semiPos < 0 && ltPos < 0) {
        throw new IllegalArgumentException("Metadata row does not contain ; or <  " + flattenedExtent);
    }

    if (semiPos < 0) {

        if (ltPos != flattenedExtent.getLength() - 1) {
            throw new IllegalArgumentException("< must come at end of Metadata row  " + flattenedExtent);
        }

        String decodedString = new String(
                Arrays.copyOfRange(flattenedExtent.getBytes(), 0, flattenedExtent.getLength() - 1), UTF_8);
        TableId tableId = TableId.of(decodedString);
        this.setTableId(tableId);
        this.setEndRow(null, false, false);
    } else {

        TableId tableId = TableId
                .of(new String(Arrays.copyOfRange(flattenedExtent.getBytes(), 0, semiPos), UTF_8));

        Text endRow = new Text();
        endRow.set(flattenedExtent.getBytes(), semiPos + 1, flattenedExtent.getLength() - (semiPos + 1));

        this.setTableId(tableId);

        this.setEndRow(endRow, false, false);
    }
}

From source file:org.apache.accumulo.core.iterators.user.IndexedDocIterator.java

License:Apache License

public static Text parseDocID(Key key) {
    Text colq = key.getColumnQualifier();
    int firstZeroIndex = colq.find("\0");
    if (firstZeroIndex < 0) {
        throw new IllegalArgumentException("bad docid: " + key.toString());
    }//from   w  w w.  ja v a 2 s.c om
    int secondZeroIndex = colq.find("\0", firstZeroIndex + 1);
    if (secondZeroIndex < 0) {
        throw new IllegalArgumentException("bad docid: " + key.toString());
    }
    int thirdZeroIndex = colq.find("\0", secondZeroIndex + 1);
    if (thirdZeroIndex < 0) {
        throw new IllegalArgumentException("bad docid: " + key.toString());
    }
    Text docID = new Text();
    try {
        docID.set(colq.getBytes(), firstZeroIndex + 1, thirdZeroIndex - 1 - firstZeroIndex);
    } catch (ArrayIndexOutOfBoundsException e) {
        throw new IllegalArgumentException("bad indices for docid: " + key.toString() + " " + firstZeroIndex
                + " " + secondZeroIndex + " " + thirdZeroIndex);
    }
    return docID;
}

From source file:org.apache.accumulo.core.iterators.user.IndexedDocIterator.java

License:Apache License

@Override
protected Text getTerm(Key key) {
    if (indexColf.compareTo(key.getColumnFamily().getBytes(), 0, indexColf.getLength()) < 0) {
        // We're past the index column family, so return a term that will sort lexicographically last.
        // The last unicode character should suffice
        return new Text("\uFFFD");
    }//from w  w  w .  ja  v a2  s.  c  o m
    Text colq = key.getColumnQualifier();
    int zeroIndex = colq.find("\0");
    Text term = new Text();
    term.set(colq.getBytes(), 0, zeroIndex);
    return term;
}

From source file:org.apache.accumulo.core.iterators.user.IndexedDocIterator.java

License:Apache License

protected Key buildDocKey() {
    if (log.isTraceEnabled())
        log.trace("building doc key for " + currentPartition + " " + currentDocID);
    int zeroIndex = currentDocID.find("\0");
    if (zeroIndex < 0)
        throw new IllegalArgumentException("bad current docID");
    Text colf = new Text(docColf);
    colf.append(nullByte, 0, 1);/*from w  w  w .  j a v  a 2s.co  m*/
    colf.append(currentDocID.getBytes(), 0, zeroIndex);
    docColfSet = Collections
            .singleton((ByteSequence) new ArrayByteSequence(colf.getBytes(), 0, colf.getLength()));
    if (log.isTraceEnabled())
        log.trace(zeroIndex + " " + currentDocID.getLength());
    Text colq = new Text();
    colq.set(currentDocID.getBytes(), zeroIndex + 1, currentDocID.getLength() - zeroIndex - 1);
    Key k = new Key(currentPartition, colf, colq);
    if (log.isTraceEnabled())
        log.trace("built doc key for seek: " + k.toString());
    return k;
}

From source file:org.apache.accumulo.core.replication.ReplicationTarget.java

License:Apache License

/**
 * Convenience method to serialize a ReplicationTarget to {@link Text} using the {@link Writable} methods without caring about performance penalties due to
 * excessive object creation//from  ww  w  . j  a  v  a  2  s . c  o m
 *
 * @return The serialized representation of the object
 */
public Text toText() {
    DataOutputBuffer buffer = new DataOutputBuffer();

    try {
        this.write(buffer);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    Text t = new Text();
    // Throw it in a text for the mutation
    t.set(buffer.getData(), 0, buffer.getLength());
    return t;
}

From source file:org.apache.accumulo.core.replication.ReplicationTargetTest.java

License:Apache License

@Test
public void staticFromTextHelper() throws Exception {
    ReplicationTarget expected = new ReplicationTarget("foo", "bar", "1");
    DataOutputBuffer buffer = new DataOutputBuffer();
    expected.write(buffer);//  w w  w .j  a va  2 s.c  o  m
    Text t = new Text();
    t.set(buffer.getData(), 0, buffer.getLength());

    Assert.assertEquals(expected, ReplicationTarget.from(t));
}

From source file:org.apache.accumulo.core.replication.ReplicationTargetTest.java

License:Apache License

@Test
public void staticToTextHelper() throws Exception {
    ReplicationTarget expected = new ReplicationTarget("foo", "bar", "1");
    DataOutputBuffer buffer = new DataOutputBuffer();
    expected.write(buffer);//from w  w  w .ja  va2  s  . com
    Text t = new Text();
    t.set(buffer.getData(), 0, buffer.getLength());

    Assert.assertEquals(t, expected.toText());
}

From source file:org.apache.accumulo.core.replication.ReplicationTargetTest.java

License:Apache License

@Test
public void staticFromStringHelper() throws Exception {
    ReplicationTarget expected = new ReplicationTarget("foo", "bar", "1");
    DataOutputBuffer buffer = new DataOutputBuffer();
    expected.write(buffer);/*from   w ww.j  av  a 2 s .c o  m*/
    Text t = new Text();
    t.set(buffer.getData(), 0, buffer.getLength());

    Assert.assertEquals(expected, ReplicationTarget.from(t.toString()));
}

From source file:org.apache.accumulo.core.summary.Gatherer.java

License:Apache License

private static Text removeTrailingZeroFromRow(Key k) {
    if (k != null) {
        Text t = new Text();
        ByteSequence row = k.getRowData();
        Preconditions.checkArgument(row.length() >= 1 && row.byteAt(row.length() - 1) == 0);
        t.set(row.getBackingArray(), row.offset(), row.length() - 1);
        return t;
    } else {/*from  w w  w  .ja  va 2 s  . c om*/
        return null;
    }
}

From source file:org.apache.accumulo.core.util.ByteBufferUtil.java

License:Apache License

public static Text toText(ByteBuffer byteBuffer) {
    if (byteBuffer == null)
        return null;

    if (byteBuffer.hasArray()) {
        Text result = new Text();
        result.set(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(),
                byteBuffer.remaining());
        return result;
    } else {//from w w  w  . j a v a  2  s .  c o m
        return new Text(toBytes(byteBuffer));
    }
}