forked from HernanRivasAcosta/kafkerl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkafkerl.erl
131 lines (109 loc) · 4.78 KB
/
kafkerl.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
-module(kafkerl).
-author('hernanrivasacosta@gmail.com').
-export([start/0]).
-export([produce/3, produce/4, produce/5,
consume/2, consume/3, consume/4, stop_consuming/2, stop_consuming/3,
request_metadata/0, request_metadata/1, request_metadata/2,
partitions/0, partitions/1]).
-export([version/0]).
%% Types
-type offset() :: integer().
-type callback() :: pid() |
fun() |
{atom(), atom()} |
{atom(), atom(), [any()]}.
-type option() :: {buffer_size, integer() | infinity} |
{dump_location, string()} |
{consumer, callback()} |
{min_bytes, integer()} |
{max_wait, integer()} |
{offset, offset()} |
{fetch_interval, false | integer()}.
-type options() :: [option()].
-type server_ref() :: atom() | pid().
-type error() :: {error, atom() | {atom(), any()}}.
-type topic() :: binary().
-type partition() :: integer().
-type payload() :: binary() | [binary()].
-type basic_message() :: {topic(), partition(), payload()}.
-export_type([server_ref/0, error/0, options/0, callback/0,
topic/0, partition/0, payload/0, basic_message/0]).
%%==============================================================================
%% API
%%==============================================================================
-spec start() -> ok | {error, term()}.
start() ->
ok = application:load(?MODULE),
application:start(?MODULE).
%%==============================================================================
%% Access API
%%==============================================================================
%% Produce API
-spec produce(server_ref(), basic_message(), options()) -> ok;
(topic(), partition(), payload()) -> ok.
produce(_ServerRef, {Topic, Partition, Message}, Options) ->
produce(?MODULE, Topic, Partition, Message, Options);
produce(Topic, Partition, Message) ->
produce(?MODULE, Topic, Partition, Message, []).
-spec produce(server_ref(), topic(), partition(), payload()) -> ok;
(topic(), partition(), payload(), options()) -> ok.
produce(Topic, Partition, Message, Options) when is_list(Options) ->
produce(?MODULE, {Topic, Partition, Message}, Options);
produce(ServerRef, Topic, Partition, Message) ->
produce(ServerRef, {Topic, Partition, Message}, []).
-spec produce(server_ref(), topic(), partition(), payload(), options()) -> ok.
produce(ServerRef, Topic, Partition, Message, Options) ->
kafkerl_connector:send(ServerRef, {Topic, Partition, Message}, Options).
%% Consume API
-spec consume(topic(), partition()) -> ok | error().
consume(Topic, Partition) ->
consume(?MODULE, Topic, Partition, []).
-spec consume(topic(), partition(), options()) -> ok | error();
(server_ref(), topic(), partition()) -> ok | error().
consume(Topic, Partition, Options) when is_list(Options) ->
consume(?MODULE, Topic, Partition, Options);
consume(ServerRef, Topic, Partition) ->
consume(ServerRef, Topic, Partition, []).
-spec consume(server_ref(), topic(), partition(), options()) ->
ok | {[payload()], offset()} | error().
consume(ServerRef, Topic, Partition, Options) ->
case {proplists:get_value(consumer, Options, undefined),
proplists:get_value(fetch_interval, Options, false)} of
{undefined, false} ->
NewOptions = [{consumer, self()} | Options],
ok = kafkerl_connector:fetch(ServerRef, Topic, Partition, NewOptions),
kafkerl_utils:gather_consume_responses();
{undefined, _} ->
{error, fetch_interval_specified_with_no_consumer};
_ ->
kafkerl_connector:fetch(ServerRef, Topic, Partition, Options)
end.
-spec stop_consuming(topic(), partition()) -> ok.
stop_consuming(Topic, Partition) ->
stop_consuming(?MODULE, Topic, Partition).
-spec stop_consuming(server_ref(), topic(), partition()) -> ok.
stop_consuming(ServerRef, Topic, Partition) ->
kafkerl_connector:stop_fetch(ServerRef, Topic, Partition).
%% Metadata API
-spec request_metadata() -> ok.
request_metadata() ->
request_metadata(?MODULE).
-spec request_metadata(atom() | [topic()]) -> ok.
request_metadata(Topics) when is_list(Topics) ->
request_metadata(?MODULE, Topics);
request_metadata(ServerRef) ->
kafkerl_connector:request_metadata(ServerRef).
-spec request_metadata(atom(), [topic()]) -> ok.
request_metadata(ServerRef, Topics) ->
kafkerl_connector:request_metadata(ServerRef, Topics).
%% Partitions
-spec partitions() -> [{topic(), [partition()]}] | error().
partitions() ->
partitions(?MODULE).
-spec partitions(server_ref()) -> [{topic(), [partition()]}] | error().
partitions(ServerRef) ->
kafkerl_connector:get_partitions(ServerRef).
%% Utils
-spec version() -> {integer(), integer(), integer()}.
version() ->
{2, 0, 0}.