Guest User

Untitled

a guest
Aug 8th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. Deprecated code when using avro-maven-plugin version 1.6.1
  2. public class AvroAddressTest {
  3. public int tempRand;
  4.  
  5. static String[] NAMES = { "Karthik", "Sam", "Joe", "Jess", "Tom",
  6. "Huck", "Hector", "Duke", "Jill", "Natalie", "Chirsta", "Ramya" };
  7.  
  8. static String[] EMAILS = { "kar@gmail.com", "steve@gmail.com",
  9. "garry@gmail.com", "kumar@hotmail.com", "dave@hotmail.com",
  10. "will@hotmail.com", "rick@ymail.com", "vinod@ymail.com",
  11. "basu@ymail.com", "sachin@ymail.com", "chester@ymail.com",
  12. "anand@ymail.com" };
  13.  
  14. static String[] PHONE_NUMBERS = { "9940099321", "9940099456",
  15. "9934099333", "9940099567", "9940077654", "9940088323",
  16. "9940097543", "9940099776", "9940000981", "9940088444",
  17. "9940099409", "9940033987" };
  18.  
  19.  
  20.  
  21.  
  22. static int[] AGES = { 32, 43, 23, 21, 55, 34, 33, 31, 22, 41, 56, 62 };
  23. static boolean[] STU = { true, false, true, true, false, false, true, false, true, false, false, true };
  24.  
  25.  
  26. public void serializeGeneric() throws IOException {
  27. // Create a datum to serialize.
  28. Schema schema = new Schema.Parser().parse(getClass()
  29. .getResourceAsStream("/AddressRec.avsc"));
  30. GenericRecord datum = new GenericData.Record(schema);
  31.  
  32. Random random = new Random();
  33.  
  34. int randInt = random.nextInt(NAMES.length);
  35.  
  36. datum.put("name", new Utf8(NAMES[randInt]));
  37. datum.put("email", new Utf8(EMAILS[randInt]));
  38. datum.put("phone", new Utf8(PHONE_NUMBERS[randInt]));
  39. datum.put("age", AGES[randInt]);
  40. datum.put("student", STU[randInt]);
  41. //datum.put("door",new Utf8(NAMES[randInt]) );
  42.  
  43. // Serialize it.
  44. ByteArrayOutputStream out = new ByteArrayOutputStream();
  45. DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(
  46. schema);
  47. Encoder encoder = EncoderFactory.get().binaryEncoder(out, null);
  48. writer.write(datum, encoder);
  49. encoder.flush();
  50. out.close();
  51. System.out.println("nSerialization: " + out);
  52.  
  53. // Deserialize it.
  54. DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(
  55. schema);
  56. BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(
  57. out.toByteArray(), null);
  58. GenericRecord result = reader.read(null, decoder);
  59. System.out.printf(
  60. "Deserialized output:nName: %s, Email: %s, Phone: %s, Age: %d, Student?: %snn",
  61. result.get("name"), result.get("email"), result.get("phone"),
  62. result.get("age"), result.get("student"));
  63. }
  64.  
  65. public void serializeSpecific() throws IOException {
  66. // Create a datum to serialize.
  67. AddressRec datum = new AddressRec();
  68. Random random = new Random();
  69. int randInt = random.nextInt(NAMES.length);
  70.  
  71. datum.**name** = new Utf8(NAMES[randInt]);
  72. datum.**email** = new Utf8(EMAILS[randInt]);
  73. datum.**phone** = new Utf8(PHONE_NUMBERS[randInt]);
  74. datum.**age** = AGES[randInt];
  75. datum.**student** = STU[randInt];
  76.  
  77. File tmpFile = File.createTempFile("AddressRecAvroExample", ".avro");
  78. // Serialize it.
  79. DataFileWriter<AddressRec> writer = new DataFileWriter<AddressRec>(
  80. new SpecificDatumWriter<AddressRec>(AddressRec.class));
  81. writer.create(AddressRec.SCHEMA$, tmpFile);
  82. writer.append(datum);
  83. writer.close();
  84.  
  85. System.out.println("nSerialization to tempfile: " + tmpFile);
  86.  
  87. // Deserialize it.
  88. FileReader<AddressRec> reader = DataFileReader.openReader(tmpFile,
  89. new SpecificDatumReader<AddressRec>(AddressRec.class));
  90. while (reader.hasNext()) {
  91. AddressRec result = reader.next();
  92. System.out.printf("Deserialized output:nName: %s, Email: %s, Phone: %s, Age: %d, Student?: %snn",
  93. result.**name**, result.**email**, result.**phone**,
  94. result.**age**, result.**student**);
  95. }
  96. reader.close();
  97. }
  98.  
  99. @Test
  100. public void serializeTest() throws IOException {
  101. serializeGeneric();
  102. serializeSpecific();
  103. }
  104.  
  105. {
  106. "type": "record",
  107. "name": "AddressRec",
  108. "namespace":"com.mycompany.samples.avro",
  109. "fields": [
  110. {"name": "name", "type": "string"},
  111. {"name": "email", "type": "string"},
  112. {"name": "phone", "type": "string"},
  113. {"name": "age", "type": "int"},
  114. {"name": "student", "type": "boolean"}
  115.  
  116. ]
Add Comment
Please, Sign In to add comment