kafka.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package kafka
  2. import "github.com/segmentio/kafka-go/protocol"
  3. // Broker represents a kafka broker in a kafka cluster.
  4. type Broker struct {
  5. Host string
  6. Port int
  7. ID int
  8. Rack string
  9. }
  10. // Topic represents a topic in a kafka cluster.
  11. type Topic struct {
  12. // Name of the topic.
  13. Name string
  14. // True if the topic is internal.
  15. Internal bool
  16. // The list of partition currently available on this topic.
  17. Partitions []Partition
  18. // An error that may have occurred while attempting to read the topic
  19. // metadata.
  20. //
  21. // The error contains both the kafka error code, and an error message
  22. // returned by the kafka broker. Programs may use the standard errors.Is
  23. // function to test the error against kafka error codes.
  24. Error error
  25. }
  26. // Partition carries the metadata associated with a kafka partition.
  27. type Partition struct {
  28. // Name of the topic that the partition belongs to, and its index in the
  29. // topic.
  30. Topic string
  31. ID int
  32. // Leader, replicas, and ISR for the partition.
  33. //
  34. // When no physical host is known to be running a broker, the Host and Port
  35. // fields will be set to the zero values. The logical broker ID is always
  36. // set to the value known to the kafka cluster, even if the broker is not
  37. // currently backed by a physical host.
  38. Leader Broker
  39. Replicas []Broker
  40. Isr []Broker
  41. // Available only with metadata API level >= 6:
  42. OfflineReplicas []Broker
  43. // An error that may have occurred while attempting to read the partition
  44. // metadata.
  45. //
  46. // The error contains both the kafka error code, and an error message
  47. // returned by the kafka broker. Programs may use the standard errors.Is
  48. // function to test the error against kafka error codes.
  49. Error error
  50. }
  51. // Marshal encodes v into a binary representation of the value in the kafka data
  52. // format.
  53. //
  54. // If v is a, or contains struct types, the kafka struct fields are interpreted
  55. // and may contain one of these values:
  56. //
  57. // nullable valid on bytes and strings, encodes as a nullable value
  58. // compact valid on strings, encodes as a compact string
  59. //
  60. // The kafka struct tags should not contain min and max versions. If you need to
  61. // encode types based on specific versions of kafka APIs, use the Version type
  62. // instead.
  63. func Marshal(v interface{}) ([]byte, error) {
  64. return protocol.Marshal(-1, v)
  65. }
  66. // Unmarshal decodes a binary representation from b into v.
  67. //
  68. // See Marshal for details.
  69. func Unmarshal(b []byte, v interface{}) error {
  70. return protocol.Unmarshal(b, -1, v)
  71. }
  72. // Version represents a version number for kafka APIs.
  73. type Version int16
  74. // Marshal is like the top-level Marshal function, but will only encode struct
  75. // fields for which n falls within the min and max versions specified on the
  76. // struct tag.
  77. func (n Version) Marshal(v interface{}) ([]byte, error) {
  78. return protocol.Marshal(int16(n), v)
  79. }
  80. // Unmarshal is like the top-level Unmarshal function, but will only decode
  81. // struct fields for which n falls within the min and max versions specified on
  82. // the struct tag.
  83. func (n Version) Unmarshal(b []byte, v interface{}) error {
  84. return protocol.Unmarshal(b, int16(n), v)
  85. }