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

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

Introduction

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

Prototype

@Override
public int getLength() 

Source Link

Document

Returns the number of bytes in the byte array

Usage

From source file:org.apache.accumulo.core.util.format.DefaultFormatter.java

License:Apache License

public static StringBuilder appendText(StringBuilder sb, Text t, int shownLength) {
    return appendBytes(sb, t.getBytes(), 0, t.getLength(), shownLength);
}

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

License:Apache License

public static String encodeColumnFamilies(Set<Text> colFams) {
    SortedSet<String> ecfs = new TreeSet<>();

    StringBuilder sb = new StringBuilder();

    for (Text text : colFams) {
        String ecf = encodeColumnFamily(sb, text.getBytes(), text.getLength());
        ecfs.add(ecf);/*from  w  ww. j a  va2s  . c  o  m*/
    }

    return Joiner.on(",").join(ecfs);
}

From source file:org.apache.accumulo.core.util.shell.commands.GetSplitsCommand.java

License:Apache License

private static String encode(final boolean encode, final Text text) {
    if (text == null) {
        return null;
    }/*  w w  w .ja va2s .  c  o m*/
    BinaryFormatter.getlength(text.getLength());
    return encode ? new String(Base64.encodeBase64(TextUtil.getBytes(text)), Constants.UTF8)
            : BinaryFormatter.appendText(new StringBuilder(), text).toString();
}

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

License:Apache License

public static byte[] getBytes(Text text) {
    byte[] bytes = text.getBytes();
    if (bytes.length != text.getLength()) {
        bytes = new byte[text.getLength()];
        System.arraycopy(text.getBytes(), 0, bytes, 0, bytes.length);
    }//from w  w  w.j av a 2s  . c om
    return bytes;
}

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

License:Apache License

public static ByteBuffer getByteBuffer(Text text) {
    if (text == null)
        return null;
    byte[] bytes = text.getBytes();
    return ByteBuffer.wrap(bytes, 0, text.getLength());
}

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

License:Apache License

public static Text truncate(Text text, int maxLen) {
    if (text.getLength() > maxLen) {
        Text newText = new Text();
        newText.append(text.getBytes(), 0, maxLen);
        String suffix = "... TRUNCATED";
        newText.append(suffix.getBytes(UTF_8), 0, suffix.length());
        return newText;
    }/*from  w  w w  .ja v  a  2 s.c om*/

    return text;
}

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

License:Apache License

/**
 * co//from   ww w  .  ja va  2s  . c  o m
 */
public void testGetBytes() {
    String longMessage = "This is some text";
    Text longMessageText = new Text(longMessage);
    String smallerMessage = "a";
    Text smallerMessageText = new Text(smallerMessage);
    Text someText = new Text(longMessage);
    assertTrue(someText.equals(longMessageText));
    someText.set(smallerMessageText);
    assertTrue(someText.getLength() != someText.getBytes().length);
    assertTrue(TextUtil.getBytes(someText).length == smallerMessage.length());
    assertTrue((new Text(TextUtil.getBytes(someText))).equals(smallerMessageText));
}

From source file:org.apache.accumulo.examples.filedata.KeyUtil.java

License:Apache License

/**
 * Split a text object using a null byte separator into an array of strings.
 *
 * @param t/*from w  w w  . j a  v a2 s  .co m*/
 *          null-byte separated text object
 * @return an array of strings
 */
public static String[] splitNullSepText(Text t) {
    ArrayList<String> s = new ArrayList<>();
    byte[] b = t.getBytes();
    int lastindex = 0;
    for (int i = 0; i < t.getLength(); i++) {
        if (b[i] == (byte) 0) {
            s.add(new String(b, lastindex, i - lastindex));
            lastindex = i + 1;
        }
    }
    s.add(new String(b, lastindex, t.getLength() - lastindex));
    return s.toArray(new String[s.size()]);
}

From source file:org.apache.accumulo.examples.filedata.KeyUtilTest.java

License:Apache License

public static void checkSeps(String... s) {
    Text t = KeyUtil.buildNullSepText(s);
    String[] rets = KeyUtil.splitNullSepText(t);

    int length = 0;
    for (String str : s)
        length += str.length();/*from w ww .  ja va2 s  . c  o m*/
    assertEquals(t.getLength(), length + s.length - 1);
    assertEquals(rets.length, s.length);
    for (int i = 0; i < s.length; i++)
        assertEquals(s[i], rets[i]);
}

From source file:org.apache.accumulo.examples.mapreduce.RowHash.java

License:Apache License

@Override
public int run(String[] args) throws Exception {
    Job job = Job.getInstance(getConf());
    job.setJobName(this.getClass().getName());
    job.setJarByClass(this.getClass());
    Opts opts = new Opts();
    opts.parseArgs(RowHash.class.getName(), args);
    job.setInputFormatClass(AccumuloInputFormat.class);
    opts.setAccumuloConfigs(job);/*from  w  w w  .j  a  v  a 2s . c  om*/

    String col = opts.column;
    int idx = col.indexOf(":");
    Text cf = new Text(idx < 0 ? col : col.substring(0, idx));
    Text cq = idx < 0 ? null : new Text(col.substring(idx + 1));
    if (cf.getLength() > 0)
        AccumuloInputFormat.fetchColumns(job, Collections.singleton(new Pair<>(cf, cq)));

    job.setMapperClass(HashDataMapper.class);
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(Mutation.class);

    job.setNumReduceTasks(0);

    job.setOutputFormatClass(AccumuloOutputFormat.class);

    job.waitForCompletion(true);
    return job.isSuccessful() ? 0 : 1;
}