format.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #pragma once
  2. ///
  3. /// @file yt/cpp/mapreduce/interface/format.h
  4. ///
  5. /// Header containing class to work with raw [YT formats](https://ytsaurus.tech/docs/en/user-guide/storage/formats).
  6. #include "node.h"
  7. #include <google/protobuf/descriptor.h>
  8. namespace NYT {
  9. ////////////////////////////////////////////////////////////////////////////////
  10. /// @deprecated
  11. struct TYamredDsvAttributes
  12. {
  13. /// Names of key columns.
  14. TVector<TString> KeyColumnNames;
  15. /// Names of subkey columns.
  16. TVector<TString> SubkeyColumnNames;
  17. };
  18. ////////////////////////////////////////////////////////////////////////////////
  19. /// @brief Class representing YT data format.
  20. ///
  21. /// Normally the user does not need to use it.
  22. /// However, the class is handy for "raw" operations and table reading and writing,
  23. /// e.g. @ref NYT::IOperationClient::RawMap and other raw operations,
  24. /// @ref NYT::IIOClient::CreateRawReader and @ref NYT::IIOClient::CreateRawWriter.
  25. /// Anyway, the static factory methods should be preferred to the constructor.
  26. ///
  27. /// @see [YT doc](https://ytsaurus.tech/docs/en/user-guide/storage/formats).
  28. struct TFormat
  29. {
  30. public:
  31. /// Format representation understandable by YT.
  32. TNode Config;
  33. public:
  34. /// @brief Construct format from given YT format representation.
  35. ///
  36. /// @note Prefer using static factory methods (e.g. @ref NYT::TFormat::YsonBinary, @ref NYT::TFormat::YsonText, @ref NYT::TFormat::Protobuf).
  37. explicit TFormat(const TNode& config = TNode());
  38. /// @brief Create text YSON format.
  39. ///
  40. /// @see [the doc](https://ytsaurus.tech/docs/en/user-guide/storage/formats#yson)
  41. static TFormat YsonText();
  42. /// @brief Create binary YSON format.
  43. ///
  44. /// @see [the doc](https://ytsaurus.tech/docs/en/user-guide/storage/formats#yson)
  45. static TFormat YsonBinary();
  46. /// @brief Create YaMR format.
  47. ///
  48. /// @deprecated
  49. static TFormat YaMRLenval();
  50. /// @brief Create protobuf format from protobuf message descriptors.
  51. ///
  52. /// @see [the doc](https://ytsaurus.tech/docs/en/api/c++/protobuf.html).
  53. static TFormat Protobuf(
  54. const TVector<const ::google::protobuf::Descriptor*>& descriptors,
  55. bool withDescriptors = false);
  56. /// @brief Create JSON format.
  57. ///
  58. /// @see [the doc](https://ytsaurus.tech/docs/en/user-guide/storage/formats#json)
  59. static TFormat Json();
  60. /// @brief Create DSV (TSKV) format.
  61. ///
  62. /// @see [the doc](https://ytsaurus.tech/docs/en/user-guide/storage/formats.html#dsv)
  63. static TFormat Dsv();
  64. /// @brief Create protobuf format for the message specified in template parameter.
  65. ///
  66. /// `T` must be inherited from `Message`.
  67. ///
  68. /// @see [the doc](https://ytsaurus.tech/docs/en/api/c++/protobuf.html).
  69. template<typename T>
  70. static inline TFormat Protobuf(bool withDescriptors = false);
  71. /// @brief Is the format text YSON?
  72. ///
  73. /// @see [the doc](https://ytsaurus.tech/docs/en/user-guide/storage/formats#yson)
  74. bool IsTextYson() const;
  75. /// @brief Is the format protobuf?
  76. ///
  77. /// @see [the doc](https://ytsaurus.tech/docs/en/api/c/protobuf.html)
  78. bool IsProtobuf() const;
  79. /// @brief Is the format YaMR?
  80. ///
  81. /// @deprecated
  82. bool IsYamredDsv() const;
  83. /// @brief For YAMR format returns its attributes in structured way.
  84. ///
  85. /// @deprecated
  86. TYamredDsvAttributes GetYamredDsvAttributes() const;
  87. };
  88. ////////////////////////////////////////////////////////////////////////////////
  89. template<typename T>
  90. TFormat TFormat::Protobuf(bool withDescriptors) {
  91. return TFormat::Protobuf({T::descriptor()}, withDescriptors);
  92. }
  93. /// @brief Create table schema from protobuf message descriptor.
  94. ///
  95. /// @param messageDescriptor Message descriptor
  96. /// @param keepFieldsWithoutExtension Add to schema fields without "column_name" or "key_column_name" extensions.
  97. TTableSchema CreateTableSchema(
  98. const ::google::protobuf::Descriptor& messageDescriptor,
  99. bool keepFieldsWithoutExtension);
  100. ////////////////////////////////////////////////////////////////////////////////
  101. } // namespace NYT