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.fluo.core.util.ByteUtil.java

License:Apache License

public static byte[] toByteArray(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  .  ja v a  2 s . com
    return bytes;
}

From source file:org.apache.fluo.integration.client.FluoAdminImplIT.java

License:Apache License

@Test
public void testInitializeConfig() throws Exception {

    // stop oracle to avoid spurious exceptions when initializing
    oserver.stop();/*  w  w  w. j ava2 s .  c o  m*/

    FluoConfiguration localConfig = new FluoConfiguration(config);
    localConfig.setProperty("fluo.test123", "${fluo.connection.application.name}");
    Assert.assertEquals(localConfig.getApplicationName(), localConfig.getString("fluo.test123"));

    try (FluoAdmin admin = new FluoAdminImpl(localConfig)) {

        InitializationOptions opts = new InitializationOptions().setClearZookeeper(true).setClearTable(true);
        admin.initialize(opts);

        // verify locality groups were set on the table
        Instance inst = new ZooKeeperInstance(config.getAccumuloInstance(), config.getAccumuloZookeepers());
        Connector conn = inst.getConnector(config.getAccumuloUser(),
                new PasswordToken(config.getAccumuloPassword()));
        Map<String, Set<Text>> localityGroups = conn.tableOperations()
                .getLocalityGroups(config.getAccumuloTable());
        Assert.assertEquals("Unexpected locality group count.", 1, localityGroups.size());
        Entry<String, Set<Text>> localityGroup = localityGroups.entrySet().iterator().next();
        Assert.assertEquals("'notify' locality group not found.", ColumnConstants.NOTIFY_LOCALITY_GROUP_NAME,
                localityGroup.getKey());
        Assert.assertEquals("'notify' locality group does not contain exactly 1 column family.", 1,
                localityGroup.getValue().size());
        Text colFam = localityGroup.getValue().iterator().next();
        Assert.assertTrue("'notify' locality group does not contain the correct column family.",
                ColumnConstants.NOTIFY_CF.contentEquals(colFam.getBytes(), 0, colFam.getLength()));
    }

    try (FluoClientImpl client = new FluoClientImpl(localConfig)) {
        FluoConfiguration sharedConfig = client.getSharedConfiguration();
        Assert.assertEquals(localConfig.getApplicationName(), sharedConfig.getString("fluo.test123"));
        Assert.assertEquals(localConfig.getApplicationName(), sharedConfig.getApplicationName());
    }
}

From source file:org.apache.fluo.recipes.spark.AccumuloRangePartitioner.java

License:Apache License

public AccumuloRangePartitioner(Collection<Text> listSplits) {
    this.splits = new ArrayList<>(listSplits.size());
    for (Text text : listSplits) {
        splits.add(Bytes.of(text.getBytes(), 0, text.getLength()));
    }/*from   www . j a  v  a2  s . c o  m*/
}

From source file:org.apache.giraph.aggregators.TextAppendAggregator.java

License:Apache License

@Override
public void aggregate(Text value) {
    byte[] valueBytes = value.getBytes();
    getAggregatedValue().append(valueBytes, 0, valueBytes.length);
}

From source file:org.apache.giraph.types.ops.TextTypeOps.java

License:Apache License

@Override
public void set(Text to, Text from) {
    to.set(from.getBytes());
}

From source file:org.apache.hama.pipes.BinaryProtocol.java

License:Apache License

/**
 * Write the given object to the stream. If it is a Text or BytesWritable,
 * write it directly. Otherwise, write it to a buffer and then write the
 * length and data to the stream./*from   w ww .ja v  a 2  s .co  m*/
 * 
 * @param obj the object to write
 * @throws IOException
 */
protected void writeObject(Writable obj) throws IOException {
    // For Text and BytesWritable, encode them directly, so that they end up
    // in C++ as the natural translations.
    if (obj instanceof Text) {
        Text t = (Text) obj;
        int len = t.getLength();
        WritableUtils.writeVInt(stream, len);
        stream.write(t.getBytes(), 0, len);
    } else if (obj instanceof BytesWritable) {
        BytesWritable b = (BytesWritable) obj;
        int len = b.getLength();
        WritableUtils.writeVInt(stream, len);
        stream.write(b.getBytes(), 0, len);
    } else {
        buffer.reset();
        obj.write(buffer);
        int length = buffer.getLength();
        WritableUtils.writeVInt(stream, length);
        stream.write(buffer.getData(), 0, length);
    }
}

From source file:org.apache.hama.pipes.protocol.BinaryProtocol.java

License:Apache License

/**
 * Write the given object to the stream. If it is a IntWritable, LongWritable,
 * FloatWritable, DoubleWritable, Text or BytesWritable, write it directly.
 * Otherwise, write it to a buffer and then write the length and data to the
 * stream./*  w  ww .ja  v  a  2s. c o m*/
 * 
 * @param obj the object to write
 * @throws IOException
 */
protected void writeObject(Writable obj) throws IOException {
    // For basic types IntWritable, LongWritable, Text and BytesWritable,
    // encode them directly, so that they end up
    // in C++ as the natural translations.
    if (obj instanceof Text) {
        Text t = (Text) obj;
        int len = t.getLength();
        WritableUtils.writeVInt(this.outStream, len);
        this.outStream.write(t.getBytes(), 0, len);

    } else if (obj instanceof BytesWritable) {
        BytesWritable b = (BytesWritable) obj;
        int len = b.getLength();
        WritableUtils.writeVInt(this.outStream, len);
        this.outStream.write(b.getBytes(), 0, len);

    } else if (obj instanceof IntWritable) {
        WritableUtils.writeVInt(this.outStream, ((IntWritable) obj).get());

    } else if (obj instanceof LongWritable) {
        WritableUtils.writeVLong(this.outStream, ((LongWritable) obj).get());

    } else {
        // Note: FloatWritable and DoubleWritable are written here
        obj.write(this.outStream);
    }
}

From source file:org.apache.hcatalog.data.JsonSerDe.java

License:Apache License

/**
 * Takes JSON string in Text form, and has to return an object representation above
 * it that's readable by the corresponding object inspector.
 *
 * For this implementation, since we're using the jackson parser, we can construct
 * our own object implementation, and we use HCatRecord for it
 *//* w ww  . j a  v  a  2 s  .  co m*/
@Override
public Object deserialize(Writable blob) throws SerDeException {

    Text t = (Text) blob;
    JsonParser p;
    List<Object> r = new ArrayList<Object>(Collections.nCopies(columnNames.size(), null));
    try {
        p = jsonFactory.createJsonParser(new ByteArrayInputStream((t.getBytes())));
        if (p.nextToken() != JsonToken.START_OBJECT) {
            throw new IOException("Start token not found where expected");
        }
        JsonToken token;
        while (((token = p.nextToken()) != JsonToken.END_OBJECT) && (token != null)) {
            // iterate through each token, and create appropriate object here.
            populateRecord(r, token, p, schema);
        }
    } catch (JsonParseException e) {
        LOG.warn("Error [{}] parsing json text [{}].", e, t);
        LOG.debug(null, e);
        throw new SerDeException(e);
    } catch (IOException e) {
        LOG.warn("Error [{}] parsing json text [{}].", e, t);
        LOG.debug(null, e);
        throw new SerDeException(e);
    }

    return new DefaultHCatRecord(r);
}

From source file:org.apache.hive.common.util.HiveStringUtils.java

License:Apache License

public static int getTextUtfLength(Text t) {
    byte[] data = t.getBytes();
    int len = 0;//from w ww  .j  av  a 2s . com
    for (int i = 0; i < t.getLength(); i++) {
        if (isUtfStartByte(data[i])) {
            len++;
        }
    }
    return len;
}

From source file:org.apache.hyracks.hdfs.lib.TextKeyValueParserFactory.java

License:Apache License

@Override
public IKeyValueParser<LongWritable, Text> createKeyValueParser(final IHyracksTaskContext ctx)
        throws HyracksDataException {

    final ArrayTupleBuilder tb = new ArrayTupleBuilder(1);
    final FrameTupleAppender appender = new FrameTupleAppender(new VSizeFrame(ctx));

    return new IKeyValueParser<LongWritable, Text>() {

        @Override/*  w ww.  ja  va 2 s.  com*/
        public void open(IFrameWriter writer) {

        }

        @Override
        public void parse(LongWritable key, Text value, IFrameWriter writer, String fileString)
                throws HyracksDataException {
            tb.reset();
            tb.addField(value.getBytes(), 0, value.getLength());
            FrameUtils.appendToWriter(writer, appender, tb.getFieldEndOffsets(), tb.getByteArray(), 0,
                    tb.getSize());
        }

        @Override
        public void close(IFrameWriter writer) throws HyracksDataException {
            appender.write(writer, false);
        }

    };
}