Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. package com.cloudera.example;
  2.  
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import org.apache.avro.Schema;
  6. import org.apache.avro.file.DataFileWriter;
  7. import org.apache.avro.file.SeekableInput;
  8. import org.apache.avro.mapred.FsInput;
  9. import org.apache.avro.reflect.ReflectData;
  10. import org.apache.avro.reflect.ReflectDatumWriter;
  11. import org.apache.hadoop.conf.Configuration;
  12. import org.apache.hadoop.fs.FSDataInputStream;
  13. import org.apache.hadoop.fs.FileStatus;
  14. import org.apache.hadoop.fs.FileSystem;
  15. import org.apache.hadoop.fs.Path;
  16.  
  17. public class DFWAppendTest {
  18. public static class Sample {
  19. CharSequence foo;
  20.  
  21. public Sample(CharSequence bar) {
  22. this.foo = bar;
  23. }
  24. }
  25.  
  26. public static void main(String[] args) throws Exception {
  27. Configuration conf = new Configuration();
  28. conf.set("fs.defaultFS", "hdfs://localhost");
  29. conf.setInt("dfs.replication", 1);
  30. FileSystem fs = FileSystem.get(conf);
  31. Schema sample = ReflectData.get().getSchema(Sample.class);
  32. ReflectDatumWriter<Sample> rdw = new ReflectDatumWriter<DFWAppendTest.Sample>(
  33. Sample.class);
  34. DataFileWriter<Sample> dfwo = new DataFileWriter<DFWAppendTest.Sample>(rdw);
  35. Path filePath = new Path("/sample.avro");
  36. OutputStream out = fs.create(filePath);
  37. DataFileWriter<Sample> dfw = dfwo.create(sample, out);
  38. dfw.append(new Sample("Eggs"));
  39. dfw.append(new Sample("Spam"));
  40. dfw.close();
  41. out.close();
  42. OutputStream aout = fs.append(filePath);
  43. dfw = dfwo.appendTo(new FsInput(filePath, conf), aout);
  44. dfw.append(new Sample("Monty"));
  45. dfw.append(new Sample("Python"));
  46. dfwo.close();
  47. aout.close();
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement