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:com.asakusafw.runtime.stage.directio.StringTemplate.java

License:Apache License

/**
 * Returns a generated name.//from  w w  w  . j ava2  s . co m
 * @return the generated name
 */
public final String apply() {
    nameBuffer.clear();
    for (int i = 0; i < formatters.length; i++) {
        Text text = formatters[i].representation;
        nameBuffer.append(text.getBytes(), 0, text.getLength());
    }
    return nameBuffer.toString();
}

From source file:com.asakusafw.runtime.value.StringOption.java

License:Apache License

private static boolean equalsTexts(Text a, Text b) {
    return ByteArrayUtil.equals(a.getBytes(), 0, a.getLength(), b.getBytes(), 0, b.getLength());
}

From source file:com.asakusafw.runtime.value.StringOption.java

License:Apache License

private static int compareTexts(Text a, Text b) {
    return ByteArrayUtil.compare(a.getBytes(), 0, a.getLength(), b.getBytes(), 0, b.getLength());
}

From source file:com.asakusafw.runtime.value.StringOptionUtil.java

License:Apache License

/**
 * Returns a {@link Reader} to read the text contents in the {@link StringOption}.
 * @param option the target {@link StringOption}
 * @return the created reader/*from   www . j a va 2 s .  co  m*/
 * @throws NullPointerException if the {@link StringOption} is/represents {@code null}
 */
public static Reader asReader(StringOption option) {
    Text text = option.get();
    InputBuffer buffer = new InputBuffer();
    buffer.reset(text.getBytes(), 0, text.getLength());
    return new InputStreamReader(buffer, ENCODING);
}

From source file:com.asakusafw.runtime.value.StringOptionUtil.java

License:Apache License

/**
 * Trims the leading/trailing classical whitespace characters in the {@link StringOption}.
 * This only removes the following characters:
 * <ul>//w  w w  .  ja  va2 s .co m
 * <li> {@code "\t" (HT:U+0009)} </li>
 * <li> {@code "\n" (LF:U+000a)} </li>
 * <li> {@code "\r" (CR:U+000d)} </li>
 * <li> {@code " " (SP:U+0020)} </li>
 * </ul>
 * This directly modifies the target {@link StringOption}.
 * @param option the target {@link StringOption}
 * @throws NullPointerException if the {@link StringOption} is/represents {@code null}
 */
public static void trim(StringOption option) {
    Text text = option.get();
    byte[] bytes = text.getBytes();
    int length = text.getLength();
    int start = 0;
    int last = length - 1;
    for (; start <= last; start++) {
        if (isTrimTarget(bytes[start]) == false) {
            break;
        }
    }
    for (; last >= start; last--) {
        if (isTrimTarget(bytes[last]) == false) {
            break;
        }
    }
    if (start == 0 && last == length - 1) {
        return;
    }
    text.set(bytes, start, last + 1 - start);
}

From source file:com.asakusafw.runtime.value.StringOptionUtil.java

License:Apache License

private static void append(StringOption target, Text text) {
    target.get().append(text.getBytes(), 0, text.getLength());
}

From source file:com.asp.tranlog.TsvImporterMapper.java

License:Apache License

/**
 * Convert a line of TSV text into an HBase table row.
 *///from ww w  .  ja va 2 s.c  o m
@Override
public void map(LongWritable offset, Text value, Context context) throws IOException {

    byte[] lineBytes = value.getBytes();

    // String lineStr = new String(value.getBytes(), 0, value.getLength(),
    // "gb18030");
    // byte[] lineBytes = new Text(lineStr).getBytes();

    int i = 0;
    try {
        ImportTsv.TsvParser.ParsedLine parsed = parser.parse(lineBytes, value.getLength());

        // ImportTsv.TsvParser.ParsedLine parsed = parser.parse(
        // lineBytes, Text.utf8Length(lineStr));

        byte[] rowKeyBytes = createRowkeyByteArray(lineBytes, parsed);
        ImmutableBytesWritable rowKey = new ImmutableBytesWritable(rowKeyBytes);

        Put put = new Put(rowKeyBytes);
        put.setWriteToWAL(false);

        for (i = 0; i < parsed.getColumnCount(); i++) {

            KeyValue kv = null;
            if (columnTypes[i] == ImportTsv.COL_TYPE_STRING) {
                kv = new KeyValue(rowKeyBytes, parser.getFamily(i), parser.getQualifier(i), 0,
                        parser.getQualifier(i).length, ts, KeyValue.Type.Put, lineBytes,
                        parsed.getColumnOffset(i), parsed.getColumnLength(i));
            } else {
                byte[] colBytes = convertColBytes(lineBytes, parsed, i);
                if (colBytes == null)
                    throw new ImportTsv.TsvParser.BadTsvLineException("Failed to get bytes for column " + i);
                kv = new KeyValue(rowKeyBytes, parser.getFamily(i), parser.getQualifier(i), ts, colBytes);
            }
            if (kv == null)
                throw new ImportTsv.TsvParser.BadTsvLineException("Failed to get bytes for column " + i);
            put.add(kv);
        }
        context.write(rowKey, put);
    } catch (ImportTsv.TsvParser.BadTsvLineException badLine) {
        if (skipBadLines) {
            System.err.println("Bad line: " + new String(lineBytes, "gb18030") + ":" + i + "\n");
            LOG.error("Bad line: " + new String(lineBytes, "gb18030") + "," + i);
            incrementBadLineCount(1);
            return;
        } else {
            throw new IOException(badLine);
        }
    } catch (IllegalArgumentException e) {
        if (skipBadLines) {
            System.err.println("Bad line: " + new String(lineBytes, "gb18030") + ":" + i + "\n");
            LOG.error("Bad line: " + new String(lineBytes, "gb18030") + "," + i);
            incrementBadLineCount(1);
            return;
        } else {
            throw new IOException(e);
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:com.axiomine.largecollections.kryo.serializers.TextSerializer.java

License:Apache License

public void write(Kryo kryo, Output output, Text object) {
    byte[] b = object.getBytes();
    output.writeInt(b.length, true);//from   w ww .ja va2  s  .c o m
    output.write(b);
}

From source file:com.bah.culvert.accumulo.database.AccumuloTableAdapter.java

License:Apache License

@Override
public byte[][] getStartKeys() {
    try {/*from  ww  w  . ja  va2 s.c o  m*/
        Collection<Text> splits = conn.tableOperations().getSplits(this.getTableName());
        // splits only define intertable boundaries - so we need to add the zero
        // for the start
        byte[] start = new byte[0];

        byte[][] byteSplits = new byte[splits.size() + 1][];
        byteSplits[0] = start;
        int i = 1;
        for (Text t : splits) {
            byteSplits[i++] = t.getBytes();
        }
        return byteSplits;
    } catch (TableNotFoundException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.bah.culvert.accumulo.database.AccumuloTableAdapter.java

License:Apache License

@Override
public byte[][] getEndKeys() {
    try {//from  w w w. j a  v a 2  s.c  o m

        Collection<Text> splits = conn.tableOperations().getSplits(this.getTableName());
        byte[][] byteSplits = new byte[splits.size() + 1][];
        int i = 0;
        for (Text t : splits) {
            byteSplits[i++] = t.getBytes();
        }

        // specify the end as null since Accumulo says that null - go to end of
        // range
        byteSplits[i] = null;
        return byteSplits;
    } catch (TableNotFoundException e) {
        throw new RuntimeException(e);
    }
}