Guest User

Untitled

a guest
Nov 25th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. import datetime
  2. from io import BytesIO
  3. from pprint import pprint
  4. from uuid import uuid4
  5.  
  6. import fastavro
  7.  
  8. fastavro._reader.LOGICAL_READERS['long-timestamp-millis'] = lambda d, w, r: d
  9.  
  10. schema = {
  11. "fields": [
  12. {
  13. "name": "date",
  14. "type": {'type': 'int', 'logicalType': 'date'}
  15. },
  16. {
  17. "name": "timestamp-millis",
  18. "type": {'type': 'long', 'logicalType': 'timestamp-millis'}
  19. },
  20. {
  21. "name": "timestamp-micros",
  22. "type": {'type': 'long', 'logicalType': 'timestamp-micros'}
  23. },
  24. {
  25. "name": "uuid",
  26. "type": {'type': 'string', 'logicalType': 'uuid'}
  27. },
  28. {
  29. "name": "time-millis",
  30. "type": {'type': 'int', 'logicalType': 'time-millis'}
  31. },
  32. {
  33. "name": "time-micros",
  34. "type": {'type': 'long', 'logicalType': 'time-micros'}
  35. }
  36. ],
  37. "namespace": "namespace",
  38. "name": "name",
  39. "type": "record"
  40. }
  41.  
  42.  
  43. def serialize(schema, data):
  44. bytes_writer = BytesIO()
  45. fastavro.schemaless_writer(bytes_writer, schema, data)
  46. return bytes_writer.getvalue()
  47.  
  48.  
  49. def deserialize(schema, binary):
  50. bytes_writer = BytesIO()
  51. bytes_writer.write(binary)
  52. bytes_writer.seek(0)
  53.  
  54. res = fastavro.schemaless_reader(bytes_writer, schema)
  55. return res
  56.  
  57.  
  58. data1 = {
  59. 'date': datetime.date.today(),
  60. 'timestamp-millis': datetime.datetime.now(),
  61. 'timestamp-micros': datetime.datetime.now(),
  62. 'uuid': uuid4(),
  63. 'time-millis': datetime.datetime.now().time(),
  64. 'time-micros': datetime.datetime.now().time(),
  65.  
  66. }
  67. binary = serialize(schema, data1)
  68. data2 = deserialize(schema, binary)
  69. pprint(data2)
Add Comment
Please, Sign In to add comment