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

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

Introduction

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

Prototype

@Override
public byte[] getBytes() 

Source Link

Document

Returns the raw bytes; however, only data up to #getLength() is valid.

Usage

From source file:org.apache.accumulo.core.file.rfile.bcfile.Utils.java

License:Apache License

/**
 * Write a String as a VInt n, followed by n Bytes as in Text format.
 *//*  w w  w .ja  v a  2  s  .  co m*/
public static void writeString(DataOutput out, String s) throws IOException {
    if (s != null) {
        Text text = new Text(s);
        byte[] buffer = text.getBytes();
        int len = text.getLength();
        writeVInt(out, len);
        out.write(buffer, 0, len);
    } else {
        writeVInt(out, -1);
    }
}

From source file:org.apache.accumulo.core.iterators.conf.ColumnSet.java

License:Apache License

static void encode(StringBuilder sb, Text t) {
    for (int i = 0; i < t.getLength(); i++) {
        int b = (0xff & t.getBytes()[i]);

        // very inefficient code
        if ((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9') || b == '_'
                || b == '-') {
            sb.append((char) b);
        } else {/*from w w w  .  j a  v  a  2  s .co  m*/
            sb.append('%');
            sb.append(String.format("%02x", b));
        }
    }
}

From source file:org.apache.accumulo.core.iterators.system.TimeSettingIteratorTest.java

License:Apache License

@Test
public void testEndKeyRangeAtMinLongValue() throws IOException {
    Text row = new Text("a");
    Text colf = new Text("b");
    Text colq = new Text("c");
    Text cv = new Text();

    for (boolean inclusiveEndRange : new boolean[] { true, false }) {
        TreeMap<Key, Value> sources = new TreeMap<>();
        sources.put(//from w  ww .j  a v  a 2 s .  co  m
                new Key(row.getBytes(), colf.getBytes(), colq.getBytes(), cv.getBytes(), Long.MIN_VALUE, true),
                new Value("00".getBytes()));
        sources.put(new Key(row.getBytes(), colf.getBytes(), colq.getBytes(), cv.getBytes(), Long.MIN_VALUE),
                new Value("11".getBytes()));

        TimeSettingIterator it = new TimeSettingIterator(new SortedMapIterator(sources), 111L);
        IteratorSetting is = new IteratorSetting(1, TimeSettingIterator.class);
        it.init(null, is.getOptions(), null);

        Key startKey = new Key();
        Key endKey = new Key(row, colf, colq, cv, Long.MIN_VALUE);
        Range testRange = new Range(startKey, false, endKey, inclusiveEndRange);
        it.seek(testRange, new HashSet<ByteSequence>(), false);

        assertTrue(it.hasTop());
        assertTrue(it.getTopValue().equals(new Value("00".getBytes())));
        assertTrue(it.getTopKey().getTimestamp() == 111L);
        it.next();
        assertTrue(it.hasTop());
        assertTrue(it.getTopValue().equals(new Value("11".getBytes())));
        assertTrue(it.getTopKey().getTimestamp() == 111L);
        it.next();
        assertFalse(it.hasTop());
    }
}

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

License:Apache License

@Override
protected Key buildKey(Text partition, Text term, Text docID) {
    Text colq = new Text(term);
    colq.append(nullByte, 0, 1);/*  w w w .ja v a  2  s.  c om*/
    colq.append(docID.getBytes(), 0, docID.getLength());
    colq.append(nullByte, 0, 1);
    return new Key(partition, indexColf, colq);
}

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  ww.ja  v a  2  s  .  c  o m*/
    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  . j  a v  a 2  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  ww.j a  v a 2  s  . c  om*/
    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.iterators.user.IndexedDocIteratorTest.java

License:Apache License

private TreeMap<Key, Value> createSortedMap(float hitRatio, int numRows, int numDocsPerRow,
        Text[] columnFamilies, Text[] otherColumnFamilies, HashSet<Text> docs, Text[] negatedColumns) {
    StringBuilder sb = new StringBuilder();
    Random r = new Random();
    Value v = new Value(new byte[0]);
    TreeMap<Key, Value> map = new TreeMap<>();
    boolean[] negateMask = new boolean[columnFamilies.length];

    for (int i = 0; i < columnFamilies.length; i++) {
        negateMask[i] = false;/*from  w  w w. j  av a2  s  .  co  m*/
        if (negatedColumns.length > 0)
            for (Text ng : negatedColumns)
                if (columnFamilies[i].equals(ng))
                    negateMask[i] = true;
    }
    for (int i = 0; i < numRows; i++) {
        Text row = new Text(String.format("%06d", i));
        for (int startDocID = docid; docid - startDocID < numDocsPerRow; docid++) {
            sb.setLength(0);
            sb.append("fake doc contents");
            boolean docHits = true;
            Text doc = new Text("type");
            doc.append(nullByte, 0, 1);
            doc.append(String.format("%010d", docid).getBytes(), 0, 10);
            for (int j = 0; j < columnFamilies.length; j++) {
                if (r.nextFloat() < hitRatio) {
                    Text colq = new Text(columnFamilies[j]);
                    colq.append(nullByte, 0, 1);
                    colq.append(doc.getBytes(), 0, doc.getLength());
                    colq.append(nullByte, 0, 1);
                    colq.append("stuff".getBytes(), 0, "stuff".length());
                    Key k = new Key(row, indexColf, colq);
                    map.put(k, v);
                    sb.append(" ");
                    sb.append(columnFamilies[j]);
                    if (negateMask[j])
                        docHits = false;
                } else {
                    if (!negateMask[j])
                        docHits = false;
                }
            }
            if (docHits) {
                docs.add(doc);
            }
            for (Text cf : otherColumnFamilies) {
                if (r.nextFloat() < hitRatio) {
                    Text colq = new Text(cf);
                    colq.append(nullByte, 0, 1);
                    colq.append(doc.getBytes(), 0, doc.getLength());
                    colq.append(nullByte, 0, 1);
                    colq.append("stuff".getBytes(), 0, "stuff".length());
                    Key k = new Key(row, indexColf, colq);
                    map.put(k, v);
                    sb.append(" ");
                    sb.append(cf);
                }
            }
            sb.append(" docID=").append(doc);
            Key k = new Key(row, docColf, new Text(String.format("%010d", docid).getBytes()));
            map.put(k, new Value(sb.toString().getBytes()));
        }
    }
    return map;
}

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

License:Apache License

/**
 * Make a new key with all parts (including delete flag) coming from {@code originalKey} but use {@code newColFam} as the column family.
 *//*from   www.j  a  v a2 s . c  o  m*/
protected Key replaceColumnFamily(Key originalKey, Text newColFam) {
    byte[] row = originalKey.getRowData().toArray();
    byte[] cf = newColFam.getBytes();
    byte[] cq = originalKey.getColumnQualifierData().toArray();
    byte[] cv = originalKey.getColumnVisibilityData().toArray();
    long timestamp = originalKey.getTimestamp();
    Key newKey = new Key(row, 0, row.length, cf, 0, newColFam.getLength(), cq, 0, cq.length, cv, 0, cv.length,
            timestamp);
    newKey.setDeleted(originalKey.isDeleted());
    return newKey;
}

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

License:Apache License

/**
 * Make a new key with all parts (including delete flag) coming from {@code originalKey} but use {@code newColQual} as the column qualifier.
 */// w  w w . ja  v  a2  s.  c  o m
protected Key replaceColumnQualifier(Key originalKey, Text newColQual) {
    byte[] row = originalKey.getRowData().toArray();
    byte[] cf = originalKey.getColumnFamilyData().toArray();
    byte[] cq = newColQual.getBytes();
    byte[] cv = originalKey.getColumnVisibilityData().toArray();
    long timestamp = originalKey.getTimestamp();
    Key newKey = new Key(row, 0, row.length, cf, 0, cf.length, cq, 0, newColQual.getLength(), cv, 0, cv.length,
            timestamp);
    newKey.setDeleted(originalKey.isDeleted());
    return newKey;
}