Example usage for org.apache.hadoop.io WritableUtils readVInt

List of usage examples for org.apache.hadoop.io WritableUtils readVInt

Introduction

In this page you can find the example usage for org.apache.hadoop.io WritableUtils readVInt.

Prototype

public static int readVInt(DataInput stream) throws IOException 

Source Link

Document

Reads a zero-compressed encoded integer from input stream and returns it.

Usage

From source file:org.apache.phoenix.join.HashJoinInfo.java

License:Apache License

@SuppressWarnings("unchecked")
public static HashJoinInfo deserializeHashJoinFromScan(Scan scan) {
    byte[] join = scan.getAttribute(HASH_JOIN);
    if (join == null) {
        return null;
    }/*from   w  w w . ja  v a 2  s .  c  o m*/
    ByteArrayInputStream stream = new ByteArrayInputStream(join);
    try {
        DataInputStream input = new DataInputStream(stream);
        KeyValueSchema joinedSchema = new KeyValueSchema();
        joinedSchema.readFields(input);
        int count = WritableUtils.readVInt(input);
        ImmutableBytesPtr[] joinIds = new ImmutableBytesPtr[count];
        List<Expression>[] joinExpressions = new List[count];
        JoinType[] joinTypes = new JoinType[count];
        boolean[] earlyEvaluation = new boolean[count];
        KeyValueSchema[] schemas = new KeyValueSchema[count];
        int[] fieldPositions = new int[count];
        for (int i = 0; i < count; i++) {
            joinIds[i] = new ImmutableBytesPtr();
            joinIds[i].readFields(input);
            int nExprs = WritableUtils.readVInt(input);
            joinExpressions[i] = new ArrayList<Expression>(nExprs);
            for (int j = 0; j < nExprs; j++) {
                int expressionOrdinal = WritableUtils.readVInt(input);
                Expression expression = ExpressionType.values()[expressionOrdinal].newInstance();
                expression.readFields(input);
                joinExpressions[i].add(expression);
            }
            int type = WritableUtils.readVInt(input);
            joinTypes[i] = JoinType.values()[type];
            earlyEvaluation[i] = input.readBoolean();
            schemas[i] = new KeyValueSchema();
            schemas[i].readFields(input);
            fieldPositions[i] = WritableUtils.readVInt(input);
        }
        Expression postJoinFilterExpression = null;
        int expressionOrdinal = WritableUtils.readVInt(input);
        if (expressionOrdinal != -1) {
            postJoinFilterExpression = ExpressionType.values()[expressionOrdinal].newInstance();
            postJoinFilterExpression.readFields(input);
        }
        int limit = -1;
        boolean forceProjection = false;
        // Read these and ignore if we don't find them as they were not
        // present in Apache Phoenix 3.0.0 release. This allows a newer
        // 3.1 server to work with an older 3.0 client without force
        // both to be upgraded in lock step.
        try {
            limit = WritableUtils.readVInt(input);
            forceProjection = input.readBoolean();
        } catch (EOFException ignore) {
        }
        return new HashJoinInfo(joinedSchema, joinIds, joinExpressions, joinTypes, earlyEvaluation, schemas,
                fieldPositions, postJoinFilterExpression, limit >= 0 ? limit : null, forceProjection);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            stream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:org.apache.phoenix.join.ScanProjector.java

License:Apache License

public static ScanProjector deserializeProjectorFromScan(Scan scan) {
    byte[] proj = scan.getAttribute(SCAN_PROJECTOR);
    if (proj == null) {
        return null;
    }//  w  ww.  j a  va2 s  .  c  o m
    ByteArrayInputStream stream = new ByteArrayInputStream(proj);
    try {
        DataInputStream input = new DataInputStream(stream);
        KeyValueSchema schema = new KeyValueSchema();
        schema.readFields(input);
        int count = WritableUtils.readVInt(input);
        Expression[] expressions = new Expression[count];
        for (int i = 0; i < count; i++) {
            int ordinal = WritableUtils.readVInt(input);
            expressions[i] = ExpressionType.values()[ordinal].newInstance();
            expressions[i].readFields(input);
        }
        return new ScanProjector(schema, expressions);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            stream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:org.apache.phoenix.mapreduce.FormatToKeyValueReducer.java

License:Apache License

@Override
protected void reduce(TableRowkeyPair key, Iterable<ImmutableBytesWritable> values,
        Reducer<TableRowkeyPair, ImmutableBytesWritable, TableRowkeyPair, KeyValue>.Context context)
        throws IOException, InterruptedException {
    TreeSet<KeyValue> map = new TreeSet<KeyValue>(KeyValue.COMPARATOR);
    for (ImmutableBytesWritable aggregatedArray : values) {
        DataInputStream input = new DataInputStream(new ByteArrayInputStream(aggregatedArray.get()));
        while (input.available() != 0) {
            byte type = input.readByte();
            long timestamp = WritableUtils.readVLong(input);
            int index = WritableUtils.readVInt(input);
            ImmutableBytesWritable family;
            ImmutableBytesWritable cq;/*from   w ww .j ava  2s.co  m*/
            ImmutableBytesWritable value = QueryConstants.EMPTY_COLUMN_VALUE_BYTES_PTR;
            Pair<byte[], byte[]> pair = columnIndexes.get(index);
            family = new ImmutableBytesWritable(pair.getFirst());
            cq = new ImmutableBytesWritable(pair.getSecond());
            int len = WritableUtils.readVInt(input);
            if (len > 0) {
                byte[] array = new byte[len];
                input.read(array);
                value = new ImmutableBytesWritable(array);
            }
            KeyValue kv;
            KeyValue.Type kvType = KeyValue.Type.codeToType(type);
            switch (kvType) {
            case Put: // not null value
                kv = builder.buildPut(key.getRowkey(), family, cq, timestamp, value);
                break;
            case DeleteColumn: // null value
                kv = builder.buildDeleteColumns(key.getRowkey(), family, cq, timestamp);
                break;
            default:
                throw new IOException("Unsupported KeyValue type " + kvType);
            }
            map.add(kv);
        }
        Closeables.closeQuietly(input);
    }
    context.setStatus("Read " + map.getClass());
    int index = 0;
    for (KeyValue kv : map) {
        context.write(key, kv);
        if (++index % 100 == 0)
            context.setStatus("Wrote " + index);
    }
}

From source file:org.apache.phoenix.mapreduce.PhoenixInputSplit.java

License:Apache License

@Override
public void readFields(DataInput input) throws IOException {
    regionLocation = WritableUtils.readString(input);
    regionSize = WritableUtils.readVLong(input);
    int count = WritableUtils.readVInt(input);
    scans = Lists.newArrayListWithExpectedSize(count);
    for (int i = 0; i < count; i++) {
        byte[] protoScanBytes = new byte[WritableUtils.readVInt(input)];
        input.readFully(protoScanBytes);
        ClientProtos.Scan protoScan = ClientProtos.Scan.parseFrom(protoScanBytes);
        Scan scan = ProtobufUtil.toScan(protoScan);
        scans.add(scan);//from  ww w.ja v a  2s . co  m
    }
    init();
}

From source file:org.apache.phoenix.query.KeyRange.java

License:Apache License

@Override
public void readFields(DataInput in) throws IOException {
    int len = WritableUtils.readVInt(in);
    if (len == 0) {
        lowerRange = KeyRange.UNBOUND;//from  w  w w  .j  a v  a2 s .c  om
        lowerInclusive = false;
    } else {
        if (len < 0) {
            lowerInclusive = false;
            lowerRange = new byte[-len - 1];
            in.readFully(lowerRange);
        } else {
            lowerInclusive = true;
            lowerRange = new byte[len - 1];
            in.readFully(lowerRange);
        }
    }
    len = WritableUtils.readVInt(in);
    if (len == 0) {
        upperRange = KeyRange.UNBOUND;
        upperInclusive = false;
    } else {
        if (len < 0) {
            upperInclusive = false;
            upperRange = new byte[-len - 1];
            in.readFully(upperRange);
        } else {
            upperInclusive = true;
            upperRange = new byte[len - 1];
            in.readFully(upperRange);
        }
    }
    init();
}

From source file:org.apache.phoenix.schema.RowKeyValueAccessor.java

License:Apache License

@Override
public void readFields(DataInput input) throws IOException {
    // Decode hasSeparator and isFixedLength from vint storing offset array length
    int length = WritableUtils.readVInt(input);
    hasSeparator = (length & 0x02) != 0;
    isFixedLength = (length & 0x01) != 0;
    length >>= 2;//from  w w w.ja va 2s . co m
    offsets = ByteUtil.deserializeVIntArray(input, length);
}

From source file:org.apache.phoenix.schema.ValueSchema.java

License:Apache License

@Override
public void readFields(DataInput in) throws IOException {
    int minNullable = WritableUtils.readVInt(in);
    int nFields = WritableUtils.readVInt(in);
    boolean rowKeyOrderOptimizable = false;
    if (nFields < 0) {
        rowKeyOrderOptimizable = true;/*w w w .j a  va 2 s  .  com*/
        nFields *= -1;
    }
    List<Field> fields = Lists.newArrayListWithExpectedSize(nFields);
    for (int i = 0; i < nFields; i++) {
        Field field = new Field();
        field.readFields(in);
        fields.add(field);
    }
    init(minNullable, fields, rowKeyOrderOptimizable);
}

From source file:org.apache.phoenix.util.ByteUtil.java

License:Apache License

public static byte[][] toByteArrays(byte[] b, int offset, int length) {
    ByteArrayInputStream bytesIn = new ByteArrayInputStream(b, offset, b.length - offset);
    DataInputStream in = new DataInputStream(bytesIn);
    byte[][] byteArrays = new byte[length][];
    try {/*  ww w.j a  v a 2s  . c om*/
        for (int i = 0; i < length; i++) {
            int bLength = WritableUtils.readVInt(in);
            if (bLength == 0) {
                byteArrays[i] = null;
            } else {
                byteArrays[i] = new byte[bLength];
                int rLength = in.read(byteArrays[i], 0, bLength);
                assert (rLength == bLength); // For find bugs
            }
        }
        if (in.read() != -1) {
            throw new IllegalStateException("Expected only " + length + " byte arrays, but found more");
        }
        return byteArrays;
    } catch (IOException e) {
        throw new RuntimeException(e); // not possible
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            throw new RuntimeException(e); // not possible
        }
    }
}

From source file:org.apache.phoenix.util.ByteUtil.java

License:Apache License

/**
 * Deserialize a byte array into a int array.  
 * @param b byte array storing serialized vints
 * @return int array/* w w  w. ja va2s  .c om*/
 */
public static int[] deserializeVIntArray(byte[] b) {
    ByteArrayInputStream bytesIn = new ByteArrayInputStream(b);
    DataInputStream in = new DataInputStream(bytesIn);
    try {
        int length = WritableUtils.readVInt(in);
        return deserializeVIntArray(in, length);
    } catch (IOException e) {
        throw new RuntimeException(e); // not possible
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            throw new RuntimeException(e); // not possible
        }
    }
}

From source file:org.apache.phoenix.util.ByteUtil.java

License:Apache License

public static int[] deserializeVIntArray(DataInput in) throws IOException {
    return deserializeVIntArray(in, WritableUtils.readVInt(in));
}