下面列出了怎么用org.apache.hadoop.io.VIntWritable的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void readFields(DataInput in) throws IOException {
VLongWritable count = new VLongWritable();
count.readFields(in);
this.count = count.get();
VIntWritable nUidsReader = new VIntWritable();
nUidsReader.readFields(in);
final int nUids = nUidsReader.get();
ImmutableSortedSet.Builder<IndexMatch> setBuilder = ImmutableSortedSet.naturalOrder();
for (int i = 0; i < nUids; ++i) {
IndexMatch index = new IndexMatch();
index.readFields(in);
setBuilder.add(index);
}
this.uids = setBuilder.build();
}
@Override
public void write(DataOutput out) throws IOException {
new VLongWritable(count).write(out);
new VIntWritable(uids.size()).write(out);
for (IndexMatch uid : uids)
uid.write(out);
}
/**
* Creates a shard stats for a field name/datatype pair.
*
* @param sumCount
* total number of values
* @param unique
* total number of unique values
*/
StatsCounters(long sumCount, long unique) {
this.count = new VLongWritable(sumCount);
int selVal;
// hyperlog unique count could be greater than total count
if (unique < sumCount) {
this.uniqueCount = new VLongWritable(unique);
selVal = (int) ((float) unique / (float) sumCount * SELECTIVITY_MULTIPLIER);
} else {
// use total count if unique is > total
this.uniqueCount = new VLongWritable(sumCount);
selVal = SELECTIVITY_MULTIPLIER;
}
this.selectivity = new VIntWritable(selVal);
}
public void write(Writable w) throws IOException {
if (w instanceof TypedBytesWritable) {
writeTypedBytes((TypedBytesWritable) w);
} else if (w instanceof BytesWritable) {
writeBytes((BytesWritable) w);
} else if (w instanceof ByteWritable) {
writeByte((ByteWritable) w);
} else if (w instanceof BooleanWritable) {
writeBoolean((BooleanWritable) w);
} else if (w instanceof IntWritable) {
writeInt((IntWritable) w);
} else if (w instanceof VIntWritable) {
writeVInt((VIntWritable) w);
} else if (w instanceof LongWritable) {
writeLong((LongWritable) w);
} else if (w instanceof VLongWritable) {
writeVLong((VLongWritable) w);
} else if (w instanceof FloatWritable) {
writeFloat((FloatWritable) w);
} else if (w instanceof DoubleWritable) {
writeDouble((DoubleWritable) w);
} else if (w instanceof Text) {
writeText((Text) w);
} else if (w instanceof ArrayWritable) {
writeArray((ArrayWritable) w);
} else if (w instanceof MapWritable) {
writeMap((MapWritable) w);
} else if (w instanceof SortedMapWritable) {
writeSortedMap((SortedMapWritable) w);
} else if (w instanceof Record) {
writeRecord((Record) w);
} else {
writeWritable(w); // last resort
}
}
public Class<? extends Writable> readType() throws IOException {
Type type = in.readType();
if (type == null) {
return null;
}
switch (type) {
case BYTES:
return BytesWritable.class;
case BYTE:
return ByteWritable.class;
case BOOL:
return BooleanWritable.class;
case INT:
return VIntWritable.class;
case LONG:
return VLongWritable.class;
case FLOAT:
return FloatWritable.class;
case DOUBLE:
return DoubleWritable.class;
case STRING:
return Text.class;
case VECTOR:
return ArrayWritable.class;
case MAP:
return MapWritable.class;
case WRITABLE:
return Writable.class;
default:
throw new RuntimeException("unknown type");
}
}
/**
* Create new XdmValue from value type and Writables.
*
*/
public static XdmValue newValue(ValueType valueType, Object value) {
if (value instanceof Text) {
return ValueFactory.newValue(valueType, ((Text)value).toString());
} else if (value instanceof BytesWritable) {
return ValueFactory.newValue(valueType, ((BytesWritable)value).getBytes());
} else if (value instanceof IntWritable) {
return ValueFactory.newValue(valueType, ((IntWritable)value).get());
} else if (value instanceof LongWritable) {
return ValueFactory.newValue(valueType, ((LongWritable)value).get());
} else if (value instanceof VIntWritable) {
return ValueFactory.newValue(valueType, ((VIntWritable)value).get());
} else if (value instanceof VLongWritable) {
return ValueFactory.newValue(valueType, ((VLongWritable)value).get());
} else if (value instanceof BooleanWritable) {
return ValueFactory.newValue(valueType, ((BooleanWritable)value).get());
} else if (value instanceof FloatWritable) {
return ValueFactory.newValue(valueType, ((FloatWritable)value).get());
} else if (value instanceof DoubleWritable) {
return ValueFactory.newValue(valueType, ((DoubleWritable)value).get());
} else if (value instanceof MarkLogicNode) {
return ValueFactory.newValue(valueType, ((MarkLogicNode)value).get());
} else {
throw new UnsupportedOperationException("Value " +
value.getClass().getName() + " is unsupported.");
}
}
public void write(Writable w) throws IOException {
if (w instanceof TypedBytesWritable) {
writeTypedBytes((TypedBytesWritable) w);
} else if (w instanceof BytesWritable) {
writeBytes((BytesWritable) w);
} else if (w instanceof ByteWritable) {
writeByte((ByteWritable) w);
} else if (w instanceof BooleanWritable) {
writeBoolean((BooleanWritable) w);
} else if (w instanceof IntWritable) {
writeInt((IntWritable) w);
} else if (w instanceof VIntWritable) {
writeVInt((VIntWritable) w);
} else if (w instanceof LongWritable) {
writeLong((LongWritable) w);
} else if (w instanceof VLongWritable) {
writeVLong((VLongWritable) w);
} else if (w instanceof FloatWritable) {
writeFloat((FloatWritable) w);
} else if (w instanceof DoubleWritable) {
writeDouble((DoubleWritable) w);
} else if (w instanceof Text) {
writeText((Text) w);
} else if (w instanceof ArrayWritable) {
writeArray((ArrayWritable) w);
} else if (w instanceof MapWritable) {
writeMap((MapWritable) w);
} else if (w instanceof SortedMapWritable) {
writeSortedMap((SortedMapWritable) w);
} else if (w instanceof Record) {
writeRecord((Record) w);
} else {
writeWritable(w); // last resort
}
}
public Class<? extends Writable> readType() throws IOException {
Type type = in.readType();
if (type == null) {
return null;
}
switch (type) {
case BYTES:
return BytesWritable.class;
case BYTE:
return ByteWritable.class;
case BOOL:
return BooleanWritable.class;
case INT:
return VIntWritable.class;
case LONG:
return VLongWritable.class;
case FLOAT:
return FloatWritable.class;
case DOUBLE:
return DoubleWritable.class;
case STRING:
return Text.class;
case VECTOR:
return ArrayWritable.class;
case MAP:
return MapWritable.class;
case WRITABLE:
return Writable.class;
default:
throw new RuntimeException("unknown type");
}
}
public static Writable toWritable(Object object) {
if (object == null) {
return null; //return NullWritable.get();
}
if (object instanceof Writable) {
return (Writable) object;
}
if (object instanceof String) {
return new Text((String) object);
}
if (object instanceof Long) {
return new VLongWritable((Long) object);
}
if (object instanceof Integer) {
return new VIntWritable((Integer) object);
}
if (object instanceof Byte) {
return new ByteWritable((Byte) object);
}
if (object instanceof Double) {
return new DoubleWritable((Double) object);
}
if (object instanceof Float) {
return new FloatWritable((Float) object);
}
if (object instanceof Boolean) {
return new BooleanWritable((Boolean) object);
}
if (object instanceof byte[]) {
return new BytesWritable((byte[]) object);
}
return new BytesWritable(object.toString().getBytes());
}
public StatsCounters() {
this.count = new VLongWritable();
this.uniqueCount = new VLongWritable();
this.selectivity = new VIntWritable();
}
StatsHyperLogSummary() {
this.count = new VLongWritable();
this.hyperLog = new BytesWritable();
this.uniqueCount = new VIntWritable();
}
public void writeVInt(VIntWritable viw) throws IOException {
out.writeInt(viw.get());
}
public VIntWritable readVInt() throws IOException {
return readVInt(null);
}
public void writeVInt(VIntWritable viw) throws IOException {
out.writeInt(viw.get());
}
public VIntWritable readVInt() throws IOException {
return readVInt(null);
}
@Test
public void testVInteger() {
writableTypeToJson(new VIntWritable(Integer.MAX_VALUE));
}
/**
* Creates a stats HyperLog summary object.
*
* @param sumCount
* total number of field name/dataype pair entries
* @param logPlus
* populated hyperlog object
* @param uniqueCount
* actual count of unique values (debug only)
* @throws IOException
* serialization error
*/
StatsHyperLogSummary(long sumCount, HyperLogLogPlus logPlus, int uniqueCount) throws IOException {
this.count = new VLongWritable(sumCount);
this.hyperLog = new BytesWritable(logPlus.getBytes());
this.uniqueCount = new VIntWritable(uniqueCount);
}