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
132
133
134
135
136
137
| %%%-------------------------------------------------------------------
| %%% @author bin.shen
| %%% @copyright (C) 2016, <COMPANY>
| %%% @doc
| %%%
| %%% @end
| %%% Created : 29. Nov 2016 9:46 AM
| %%%-------------------------------------------------------------------
| -module(test).
| -author("bin.shen").
|
| %% API
| -export([init/0]).
|
| %%-include_lib("eunit/include/eunit.hrl").
| %%-include("../priv/mongodb-erlang/include/mongo_protocol.hrl").
|
| %-include("/Users/bin.shen/git/rabbitmq-erlang-client/include/amqp_client.hrl").
|
|
| init() ->
| %% A = <<1,2>>,
| %% B = <<3,4,5>>,
| %% C = <<A/binary, B/binary>>,
| %% io:format("~p~n", [C]){port, 27017},
|
| %% application:ensure_all_started(mongodb),
|
|
| application:start (bson),
| application:start (crypto),
| application:start (poolboy),
| application:start (mongodb),
|
|
| %% Database = <<"test1">>,
| %% {ok, Connection} = mc_worker_api:connect ([{database, Database}]),
| %% io:format("Connection ~p~n", [Connection]),
| %%
| %% Collection = <<"test2">>,
| %% mc_worker_api:insert(Connection, Collection, [
| %% {<<"name">>, <<"Yankees">>, <<"home">>, {<<"city">>, <<"New York">>, <<"state">>, <<"NY">>}, <<"league">>, <<"American">>},
| %% {<<"name">>, <<"Mets">>, <<"home">>, {<<"city">>, <<"New York">>, <<"state">>, <<"NY">>}, <<"league">>, <<"National">>},
| %% {<<"name">>, <<"Phillies">>, <<"home">>, {<<"city">>, <<"Philadelphia">>, <<"state">>, <<"PA">>}, <<"league">>, <<"National">>},
| %% {<<"name">>, <<"Red Sox">>, <<"home">>, {<<"city">>, <<"Boston">>, <<"state">>, <<"MA">>}, <<"league">>, <<"American">>}
| %% ]),
|
| %% Database = <<"test1">>,
| %% case mc_worker_api:connect([
| %% {database, Database},
| %% {host, "127.0.0.1"},
| %% {port, 27017}
| %% ]) of
| %% {ok, Connection} ->
| %% io:format("Connection ~p~n", [Connection]),
| %%
| %% Cursor = mc_worker_api:find(Connection, <<"test2">>, {}),
| %% io:format("~p cursor: ~p~n", [self(), Cursor]),
| %% mc_cursor:close(Cursor);
| %%
| %% {error, Reason} ->
| %% io:format("unable to connect to ~p: ~p~n", [Database, Reason])
| %% end,
|
| {ok, Connection} = mc_worker_api:connect([
| {database, <<"moral_db">>},
| {login, <<"moral_user">>},
| {password, <<"m2o0r1a6l_passw0rd">>},
| {host, "121.40.92.176"},
| {port, 27017}
| ]),
|
| io:format("Connection is XXX : ~p~n", [Connection]),
| io:format("*****************###"),
|
| mc_worker_api:insert(Connection, <<"alerts">>, [
| {<<"mac">>, <<"accf23b87fbf">>, <<"x1">>, <<"1">>, <<"x2">>, <<"2">>}
| ]),
|
| %% Cursor = mc_worker_api:find_one(Connection, <<"test2">>, {}),
|
|
| %% Cursor = mc_worker_api:find(Connection, <<"alerts">>, {}),
| %% Result = mc_cursor:rest(Cursor),
| %% io:format("~p~n", [Result]),
| %% mc_cursor:close(Cursor),
| %% mc_worker_api:disconnect(Connection),
|
| %%process({}) ->
| %% ok;
| %%
| %%process(Cursor) ->
| %% io:format("----Cursor:~p~n", [Cursor]),
| %% Record = mc_worker_api:(Cursor),
| %% io:format("Record:~p~n", [Record]),
| %% case Record of
| %% {} ->
| %% no_more;
| %% _ ->
| %% process(Cursor)
| %% end,
|
| %% {ok, Connection} = amqp_connection:start(#amqp_params_network{}),
| %% %% Open a channel on the connection
| %% {ok, Channel} = amqp_connection:open_channel(Connection),
|
| %% %% Declare a queue
| %% #'queue.declare_ok'{queue = Q} = amqp_channel:call(Channel, #'queue.declare'{}),
| %%
| %% %% Publish a message
| %% Payload = <<"foobar">>,
| %% Publish = #'basic.publish'{exchange = <<>>, routing_key = Q},
| %% amqp_channel:cast(Channel, Publish, #amqp_msg{payload = Payload}),
| %%
| %% %% Get the message back from the queue
| %% Get = #'basic.get'{queue = Q},
| %% {#'basic.get_ok'{delivery_tag = Tag}, Content}
| %% = amqp_channel:call(Channel, Get),
| %%
| %% %% Do something with the message payload
| %% %% (some work here)
| %%
| %% %% Ack the message
| %% amqp_channel:cast(Channel, #'basic.ack'{delivery_tag = Tag}),
| %%
| %% %% Close the channel
| %% amqp_channel:close(Channel),
| %% %% Close the connection
| %% amqp_connection:close(Connection),
|
| %% application:start(emongo),
| %% emongo:add_pool(pool1, "192.168.1.102", 27017, "test1", 1),
| %% emongo:insert(pool1, "test2", {name:2334}),
|
| io:format("*****************###"),
|
| ok.
|
|