List of usage examples for org.apache.hadoop.io Writable write
void write(DataOutput out) throws IOException;
out
. From source file:org.apache.hama.ipc.AsyncServer.java
License:Apache License
/** * Setup response for the IPC Call./*from w ww . j a v a 2 s .co m*/ * * @param response buffer to serialize the response into * @param call {@link Call} to which we are setting up the response * @param status {@link Status} of the IPC call * @param rv return value for the IPC Call, if the call was successful * @param errorClass error class, if the the call failed * @param error error message, if the call failed * @throws IOException */ private void setupResponse(ByteArrayOutputStream response, Call call, Status status, Writable rv, String errorClass, String error) throws IOException { response.reset(); DataOutputStream out = new DataOutputStream(response); out.writeInt(call.id); // write call id out.writeInt(status.state); // write status if (status == Status.SUCCESS) { rv.write(out); } else { WritableUtils.writeString(out, errorClass); WritableUtils.writeString(out, error); } call.setResponse(ByteBuffer.wrap(response.toByteArray())); IOUtils.closeStream(out); }
From source file:org.apache.hama.ipc.Server.java
License:Apache License
/** * Setup response for the IPC Call./*from w w w .ja v a2 s.c o m*/ * * @param response buffer to serialize the response into * @param call {@link Call} to which we are setting up the response * @param status {@link Status} of the IPC call * @param rv return value for the IPC Call, if the call was successful * @param errorClass error class, if the the call failed * @param error error message, if the call failed * @throws IOException */ private void setupResponse(ByteArrayOutputStream response, Call call, Status status, Writable rv, String errorClass, String error) throws IOException { response.reset(); DataOutputStream out = new DataOutputStream(response); out.writeInt(call.id); // write call id out.writeInt(status.state); // write status if (status == Status.SUCCESS) { rv.write(out); } else { WritableUtils.writeString(out, errorClass); WritableUtils.writeString(out, error); } call.setResponse(ByteBuffer.wrap(response.toByteArray())); }
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 w w . j a va 2 s. c o 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./*from w ww.j av a2s. com*/ * * @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.hama.util.WritableUtils.java
License:Apache License
public static byte[] serialize(Writable w) { ByteArrayOutputStream out = new ByteArrayOutputStream(); DataOutput output = new DataOutputStream(out); try {/* w w w . jav a2 s .c om*/ w.write(output); } catch (IOException e) { e.printStackTrace(); } return out.toByteArray(); }
From source file:org.apache.hama.util.WritableUtils.java
License:Apache License
public static byte[] unsafeSerialize(Writable w) { UnsafeByteArrayOutputStream out = new UnsafeByteArrayOutputStream(); DataOutput output = new DataOutputStream(out); try {/*ww w .ja va 2 s .c om*/ w.write(output); } catch (IOException e) { e.printStackTrace(); } return out.toByteArray(); }
From source file:org.apache.hcatalog.mapreduce.HCatSplit.java
License:Apache License
@Override public void write(DataOutput output) throws IOException { String partitionInfoString = HCatUtil.serialize(partitionInfo); // write partitionInfo into output WritableUtils.writeString(output, partitionInfoString); WritableUtils.writeString(output, baseMapRedSplit.getClass().getName()); Writable baseSplitWritable = (Writable) baseMapRedSplit; //write baseSplit into output baseSplitWritable.write(output); //write the table schema into output String tableSchemaString = HCatUtil.serialize(tableSchema); WritableUtils.writeString(output, tableSchemaString); }
From source file:org.apache.hive.hcatalog.mapreduce.HCatSplit.java
License:Apache License
@Override public void write(DataOutput output) throws IOException { String partitionInfoString = HCatUtil.serialize(partitionInfo); // write partitionInfo into output WritableUtils.writeString(output, partitionInfoString); WritableUtils.writeString(output, baseMapRedSplit.getClass().getName()); Writable baseSplitWritable = (Writable) baseMapRedSplit; //write baseSplit into output baseSplitWritable.write(output); }
From source file:org.apache.mahout.classifier.chi_rwcs.Chi_RWCSUtils.java
License:Apache License
public static void storeWritable(Configuration conf, Path path, Writable writable) throws IOException { FileSystem fs = path.getFileSystem(conf); FSDataOutputStream out = fs.create(path); try {//from w ww .ja v a 2s . com writable.write(out); } finally { Closeables.closeQuietly(out); } }
From source file:org.apache.mahout.classifier.df.DFUtils.java
License:Apache License
public static void storeWritable(Configuration conf, Path path, Writable writable) throws IOException { FileSystem fs = path.getFileSystem(conf); FSDataOutputStream out = fs.create(path); try {/* w w w . j av a2 s.c o m*/ writable.write(out); } finally { Closeables.close(out, false); } }