List of usage examples for org.apache.hadoop.io FloatWritable write
@Override public void write(DataOutput out) throws IOException
From source file:com.moz.fiji.hive.io.FijiCellWritable.java
License:Apache License
/** * Reads and converts data according to the specified schema. * * @param out DataOutput to serialize this object into. * @param data data to be serialized.// w ww .j a va 2s .c o m * @param schema Schema to be used for serializing this data. * @throws IOException if there was an error writing. */ private static void writeData(DataOutput out, Object data, Schema schema) throws IOException { switch (schema.getType()) { case INT: Integer intData = (Integer) data; WritableUtils.writeVInt(out, intData); break; case LONG: Long longData = (Long) data; WritableUtils.writeVLong(out, longData); break; case DOUBLE: Double doubleData = (Double) data; DoubleWritable doubleWritable = new DoubleWritable(doubleData); doubleWritable.write(out); break; case ENUM: case STRING: String stringData = data.toString(); WritableUtils.writeString(out, stringData); break; case FLOAT: Float floatData = (Float) data; FloatWritable floatWritable = new FloatWritable(floatData); floatWritable.write(out); break; case ARRAY: List<Object> listData = (List<Object>) data; WritableUtils.writeVInt(out, listData.size()); for (Object listElement : listData) { writeData(out, listElement, schema.getElementType()); } break; case RECORD: IndexedRecord recordData = (IndexedRecord) data; WritableUtils.writeVInt(out, schema.getFields().size()); for (Schema.Field field : schema.getFields()) { WritableUtils.writeString(out, field.name()); writeData(out, recordData.get(field.pos()), field.schema()); } break; case MAP: Map<String, Object> mapData = (Map<String, Object>) data; WritableUtils.writeVInt(out, mapData.size()); for (Map.Entry<String, Object> entry : mapData.entrySet()) { WritableUtils.writeString(out, entry.getKey()); writeData(out, entry.getValue(), schema.getValueType()); } break; case UNION: final Integer tag = GenericData.get().resolveUnion(schema, data); WritableUtils.writeVInt(out, tag); Schema unionSubSchema = schema.getTypes().get(tag); writeData(out, data, unionSubSchema); break; case BYTES: byte[] bytesData = (byte[]) data; WritableUtils.writeCompressedByteArray(out, bytesData); break; case BOOLEAN: Boolean booleanData = (Boolean) data; BooleanWritable booleanWritable = new BooleanWritable(booleanData); booleanWritable.write(out); break; case NULL: // Don't need to write anything for null. break; case FIXED: default: throw new UnsupportedOperationException("Unsupported type: " + schema.getType()); } }
From source file:org.goldenorb.io.checkpoint.CheckPointDataTest.java
License:Apache License
/** * Tests the CheckPointDataOutput class by writing several different types of Writables to the checkpoint. * /*ww w . ja v a 2s . c o m*/ * @throws Exception */ @Test public void testCheckpointOutput() throws Exception { int superStep = 0; int partition = 0; OrbConfiguration orbConf = new OrbConfiguration(); orbConf.set("fs.default.name", "hdfs://localhost:" + cluster.getNameNodePort()); orbConf.setJobNumber("0"); orbConf.setFileOutputPath("test"); CheckPointDataOutput checkpointOutput = new CheckPointDataOutput(orbConf, superStep, partition); IntWritable intOutput = new IntWritable(4); intOutput.write(checkpointOutput); LongWritable longOutput = new LongWritable(9223372036854775807L); longOutput.write(checkpointOutput); Text textOutput = new Text("test"); textOutput.write(checkpointOutput); FloatWritable floatOutput = new FloatWritable(3.14159F); floatOutput.write(checkpointOutput); checkpointOutput.close(); assertThat(checkpointOutput, notNullValue()); }
From source file:tlfetl.card.TLFDWHValue.java
@Override public void write(DataOutput d) throws IOException { FloatWritable writableAmount = new FloatWritable(amount); IntWritable writableCount = new IntWritable(count); writableAmount.write(d); writableCount.write(d);//w w w .j a va 2s . c o m }