data_reader_writer.rst 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. Data Reader Writer
  2. ====================
  3. Aims for read or write bytes from different media, You can implement new classes to meet the needs of your personal scenarios
  4. if MinerU have not provide the suitable classes. It is easy to implement new classes, the only one requirement is to inherit from
  5. ``DataReader`` or ``DataWriter``
  6. .. code:: python
  7. class SomeReader(DataReader):
  8. def read(self, path: str) -> bytes:
  9. pass
  10. def read_at(self, path: str, offset: int = 0, limit: int = -1) -> bytes:
  11. pass
  12. class SomeWriter(DataWriter):
  13. def write(self, path: str, data: bytes) -> None:
  14. pass
  15. def write_string(self, path: str, data: str) -> None:
  16. pass
  17. Reader may curious about the difference between :doc:`io` and this section. Those two sections look very similarity at first glance.
  18. :doc:`io` provides fundamental functions, while This section thinks more at application level. Customer can build they own classes to meet
  19. their own applications need which may share same IO function. That is why we have :doc:`io`.
  20. Important Classes
  21. -----------------
  22. .. code:: python
  23. class FileBasedDataReader(DataReader):
  24. def __init__(self, parent_dir: str = ''):
  25. pass
  26. class FileBasedDataWriter(DataWriter):
  27. def __init__(self, parent_dir: str = '') -> None:
  28. pass
  29. Class ``FileBasedDataReader`` initialized with unary param ``parent_dir``, That means that every method ``FileBasedDataReader`` provided will have features as follow.
  30. Features:
  31. #. read content from the absolute path file, ``parent_dir`` will be ignored.
  32. #. read the relative path, file will first join with ``parent_dir``, then read content from the merged path
  33. .. note::
  34. ``FileBasedDataWriter`` shares the same behavior with ``FileBaseDataReader``
  35. .. code:: python
  36. class MultiS3Mixin:
  37. def __init__(self, default_prefix: str, s3_configs: list[S3Config]):
  38. pass
  39. class MultiBucketS3DataReader(DataReader, MultiS3Mixin):
  40. pass
  41. All read-related method that class ``MultiBucketS3DataReader`` provided will have features as follow.
  42. Features:
  43. #. read object with full s3-format path, for example ``s3://test_bucket/test_object``, ``default_prefix`` will be ignored.
  44. #. read object with relative path, file will join ``default_prefix`` and trim the ``bucket_name`` firstly, then read the content. ``bucket_name`` is the first element of the result after split ``default_prefix`` with delimiter ``\``
  45. .. note::
  46. ``MultiBucketS3DataWriter`` shares the same behavior with ``MultiBucketS3DataReader``
  47. .. code:: python
  48. class S3DataReader(MultiBucketS3DataReader):
  49. pass
  50. ``S3DataReader`` is build on top of MultiBucketS3DataReader which only support for bucket. So is ``S3DataWriter``.
  51. Read Examples
  52. ------------
  53. .. code:: python
  54. # file based related
  55. file_based_reader1 = FileBasedDataReader('')
  56. ## will read file abc
  57. file_based_reader1.read('abc')
  58. file_based_reader2 = FileBasedDataReader('/tmp')
  59. ## will read /tmp/abc
  60. file_based_reader2.read('abc')
  61. ## will read /var/logs/message.txt
  62. file_based_reader2.read('/var/logs/message.txt')
  63. # multi bucket s3 releated
  64. multi_bucket_s3_reader1 = MultiBucketS3DataReader("test_bucket1/test_prefix", list[S3Config(
  65. bucket_name=test_bucket1, access_key=ak, secret_key=sk, endpoint_url=endpoint_url
  66. ),
  67. S3Config(
  68. bucket_name=test_bucket_2,
  69. access_key=ak_2,
  70. secret_key=sk_2,
  71. endpoint_url=endpoint_url_2,
  72. )])
  73. ## will read s3://test_bucket1/test_prefix/abc
  74. multi_bucket_s3_reader1.read('abc')
  75. ## will read s3://test_bucket1/efg
  76. multi_bucket_s3_reader1.read('s3://test_bucket1/efg')
  77. ## will read s3://test_bucket2/abc
  78. multi_bucket_s3_reader1.read('s3://test_bucket2/abc')
  79. # s3 related
  80. s3_reader1 = S3DataReader(
  81. default_prefix_without_bucket = "test_prefix"
  82. bucket: "test_bucket",
  83. ak: "ak",
  84. sk: "sk",
  85. endpoint_url: "localhost"
  86. )
  87. ## will read s3://test_bucket/test_prefix/abc
  88. s3_reader1.read('abc')
  89. ## will read s3://test_bucket/efg
  90. s3_reader1.read('s3://test_bucket/efg')
  91. Write Examples
  92. ---------------
  93. .. code:: python
  94. # file based related
  95. file_based_writer1 = FileBasedDataWriter('')
  96. ## will write 123 to abc
  97. file_based_writer1.write('abc', '123'.encode())
  98. ## will write 123 to abc
  99. file_based_writer1.write_string('abc', '123')
  100. file_based_writer2 = FileBasedDataWriter('/tmp')
  101. ## will write 123 to /tmp/abc
  102. file_based_writer2.write_string('abc', '123')
  103. ## will write 123 to /var/logs/message.txt
  104. file_based_writer2.write_string('/var/logs/message.txt', '123')
  105. # multi bucket s3 releated
  106. multi_bucket_s3_writer1 = MultiBucketS3DataWriter("test_bucket1/test_prefix", list[S3Config(
  107. bucket_name=test_bucket1, access_key=ak, secret_key=sk, endpoint_url=endpoint_url
  108. ),
  109. S3Config(
  110. bucket_name=test_bucket_2,
  111. access_key=ak_2,
  112. secret_key=sk_2,
  113. endpoint_url=endpoint_url_2,
  114. )])
  115. ## will write 123 to s3://test_bucket1/test_prefix/abc
  116. multi_bucket_s3_writer1.write_string('abc', '123')
  117. ## will write 123 to s3://test_bucket1/test_prefix/abc
  118. multi_bucket_s3_writer1.write('abc', '123'.encode())
  119. ## will write 123 to s3://test_bucket1/efg
  120. multi_bucket_s3_writer1.write('s3://test_bucket1/efg', '123'.encode())
  121. ## will write 123 to s3://test_bucket2/abc
  122. multi_bucket_s3_writer1.write('s3://test_bucket2/abc', '123'.encode())
  123. # s3 related
  124. s3_writer1 = S3DataWriter(
  125. default_prefix_without_bucket = "test_prefix"
  126. bucket: "test_bucket",
  127. ak: "ak",
  128. sk: "sk",
  129. endpoint_url: "localhost"
  130. )
  131. ## will write 123 to s3://test_bucket/test_prefix/abc
  132. s3_writer1.write('abc', '123'.encode())
  133. ## will write 123 to s3://test_bucket/test_prefix/abc
  134. s3_writer1.write_string('abc', '123')
  135. ## will write 123 to s3://test_bucket/efg
  136. s3_writer1.write('s3://test_bucket/efg', '123'.encode())
  137. Check :doc:`../../api/classes` for more intuitions or check :doc:`../../api/data_reader_writer` for more details