diff --git a/README.md b/README.md index 381e7e2..12fbf64 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ A proper list contains: * `auth`: authentication options, `{auth, {basic, #{username => <<"greptime_user">>, password => <<"greptime_pwd">>}}}` for example. ### Write and datatypes +The metric name can be a string or binary. If you want to set the database, the metric name can be set in the form of `{dbname, metric}`. The data will be written into `greptime-public` by default. Write each row by `greptimedb:write/3` function. Every row contains: diff --git a/src/greptimedb.erl b/src/greptimedb.erl index 3ee35fb..c3e7b25 100644 --- a/src/greptimedb.erl +++ b/src/greptimedb.erl @@ -37,13 +37,10 @@ start_client(Options0) -> {error, Reason} end. -write(#{protocol := Protocol} = Client, Metric, Points) -> +write(Client, Metric, Points) -> try - case Protocol of - http -> - Request = greptimedb_encoder:insert_request(Client, Metric, Points), - rpc_call(Client, Request) - end + Request = greptimedb_encoder:insert_request(Client, Metric, Points), + rpc_call(Client, Request) catch E:R:S -> logger:error("[GreptimeDB] write ~0p failed: ~0p ~0p ~0p ~p", @@ -51,12 +48,9 @@ write(#{protocol := Protocol} = Client, Metric, Points) -> {error, R} end. -write_stream(#{protocol := Protocol} = Client) -> +write_stream(Client) -> try - case Protocol of - http -> - rpc_write_stream(Client) - end + rpc_write_stream(Client) catch E:R:S -> logger:error("[GreptimeDB] create write stream failed: ~0p ~0p ~p", [E, R, S]), @@ -67,11 +61,8 @@ ddl(_Client) -> todo. -spec stop_client(Client :: map()) -> ok | term(). -stop_client(#{pool := Pool, protocol := Protocol}) -> - case Protocol of - http -> - ecpool:stop_sup_pool(Pool) - end. +stop_client(#{pool := Pool}) -> + ecpool:stop_sup_pool(Pool). %%%=================================================================== %%% Internal functions diff --git a/src/greptimedb_encoder.erl b/src/greptimedb_encoder.erl index ba9cbf4..a5a6771 100644 --- a/src/greptimedb_encoder.erl +++ b/src/greptimedb_encoder.erl @@ -17,10 +17,9 @@ -export([insert_request/3]). -define(TS_COLUMN, <<"greptime_timestamp">>). --define(DEFAULT_CATALOG, "greptime"). --define(DEFAULT_SCHEMA, "public"). +-define(DEFAULT_DBNAME, "greptime-public"). -insert_request(#{cli_opts := Options} = _Client, {Catalog, Schema, Table}, Points) -> +insert_request(#{cli_opts := Options} = _Client, {DbName, Table}, Points) -> RowCount = length(Points), Columns = lists:map(fun(Column) -> pad_null_mask(Column, RowCount) end, collect_columns(Points)), @@ -28,10 +27,9 @@ insert_request(#{cli_opts := Options} = _Client, {Catalog, Schema, Table}, Point Header = case AuthHeader of {} -> - #{catalog => Catalog, schema => Schema}; + #{dbname => DbName}; Scheme -> - #{catalog => Catalog, - schema => Schema, + #{dbname => DbName, authorization => #{auth_scheme => Scheme}} end, @@ -42,7 +40,7 @@ insert_request(#{cli_opts := Options} = _Client, {Catalog, Schema, Table}, Point row_count => RowCount}}, #{header => Header, request => Request}; insert_request(Client, Table, Points) -> - insert_request(Client, {?DEFAULT_CATALOG, ?DEFAULT_SCHEMA, Table}, Points). + insert_request(Client, {?DEFAULT_DBNAME, Table}, Points). %%%=================================================================== %%% Internal functions diff --git a/test/greptimedb_SUITE.erl b/test/greptimedb_SUITE.erl index d120683..22a443e 100644 --- a/test/greptimedb_SUITE.erl +++ b/test/greptimedb_SUITE.erl @@ -52,12 +52,10 @@ t_collect_columns(_) -> Request = greptimedb_encoder:insert_request(Client, Metric, Points), case Request of #{header := - #{catalog := Catalog, - schema := Schema, + #{dbname := DbName, authorization := Auth}, request := {insert, #{columns := Columns}}} -> - ?assertEqual(Catalog, "greptime"), - ?assertEqual(Schema, "public"), + ?assertEqual(DbName, "greptime-public"), ?assertEqual(8, length(Columns)), ?assertEqual(Auth, #{auth_scheme => AuthInfo}),