kafka.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. Leader Broker
  34. Replicas []Broker
  35. Isr []Broker
  36. // An error that may have occurred while attempting to read the partition
  37. // metadata.
  38. //
  39. // The error contains both the kafka error code, and an error message
  40. // returned by the kafka broker. Programs may use the standard errors.Is
  41. // function to test the error against kafka error codes.
  42. Error error
  43. }
  44. // Marshal encodes v into a binary representation of the value in the kafka data
  45. // format.
  46. //
  47. // If v is a, or contains struct types, the kafka struct fields are interpreted
  48. // and may contain one of these values:
  49. //
  50. // nullable valid on bytes and strings, encodes as a nullable value
  51. // compact valid on strings, encodes as a compact string
  52. //
  53. // The kafka struct tags should not contain min and max versions. If you need to
  54. // encode types based on specific versions of kafka APIs, use the Version type
  55. // instead.
  56. func Marshal(v interface{}) ([]byte, error) {
  57. return protocol.Marshal(-1, v)
  58. }
  59. // Unmarshal decodes a binary representation from b into v.
  60. //
  61. // See Marshal for details.
  62. func Unmarshal(b []byte, v interface{}) error {
  63. return protocol.Unmarshal(b, -1, v)
  64. }
  65. // Version represents a version number for kafka APIs.
  66. type Version int16
  67. // Marshal is like the top-level Marshal function, but will only encode struct
  68. // fields for which n falls within the min and max versions specified on the
  69. // struct tag.
  70. func (n Version) Marshal(v interface{}) ([]byte, error) {
  71. return protocol.Marshal(int16(n), v)
  72. }
  73. // Unmarshal is like the top-level Unmarshal function, but will only decode
  74. // struct fields for which n falls within the min and max versions specified on
  75. // the struct tag.
  76. func (n Version) Unmarshal(b []byte, v interface{}) error {
  77. return protocol.Unmarshal(b, int16(n), v)
  78. }