Giter VIP home page Giter VIP logo

cqex's Introduction

CQErl

CircleCI

Native Erlang client for CQL3 over Cassandra's latest binary protocol v4.

Usage · Connecting · Clusters · Performing queries · Query options · Batched queries · Reusable queries · Data types

Installation · Compatibility · Tests · License

At a glance

CQErl offers a simple Erlang interface to Cassandra using the latest CQL version. The main features include:

  • Automatic connection pooling, with hash-based allocation (a la dispcount)
  • Single or multi-cluster support
  • Batched queries
  • Variable bindings in CQL queries (named or positional)
  • Automatic query reuse when including variable bindings
  • Collection types support
  • User-defined type support
  • Tunable consistency level
  • Automatic pagination support
  • Synchronous or asynchronous queries
  • Automatic compression (using lz4 or snappy if available)
  • SSL support
  • Pluggable authentication (as long as it's SASL-based)

CQErl was designed to be as simple as possible on your side. You just provide the configuration you want as environment variables, and ask to get a client everytime you need to perform a transient piece of work (e.g. handle a web request). You do not need to (and in fact should not) keep a client in state for a long time. Under the hood, CQErl maintains a pool of persistent connections with Cassandra and this pattern is the best way to ensure proper load balancing of requests across the pool.

Usage

Connecting

If you installed cassandra and didn't change any configuration related to authentication or SSL, you should be able to connect like this

{ok, Client} = cqerl:get_client({}).

You do not need to close the connection after you've finished using it.

  1. The first argument to cqerl:get_client/2,1 is the node to which you wish to connect as {Ip, Port}. If empty, it defaults to {"127.0.0.1", 9042}, and Ip can be given as a string, or as a tuple of components, either IPv4 or IPv6.

    • If the default port is used, you can provide just the IP address as the first argument, either as a tuple, list or binary.
    • If both the default port and localhost are used, you can just provide an empty tuple as the first argument.
  2. The second possible argument (when using cqerl:get_client/2) is a list of options, that include:

    • keyspace which determines in which keyspace all subsequent requests operate, on that connection.
    • auth (mentionned below)
    • ssl (which is false by default, but can be set to a list of SSL options) and keyspace (string or binary).
    • protocol_version to connect to older Cassandra instances.

    If you've set simple username/password authentication scheme on Cassandra, you can provide those to CQErl

    {ok, Client} = cqerl:get_client({}, [{auth, {cqerl_auth_plain_handler, [{"test", "aaa"}]}}]).

    Since Cassandra implements pluggable authentication mechanisms, CQErl also allows you to provide custom authentication modules (here cqerl_auth_plain_handler). The options you pass along with it are given to the module's auth_init/3 as its first argument.

  3. You can leverage one or more clusters of cassandra nodes by setting up clusters. When set up, you can use

    1. cqerl:get_client() if you have just a single main cluster
    2. cqerl:get_client(ClusterKey) if you want to get a client from a specific, identified cluster

Using environment variables

All the options given above can be provided as environment variables, in which case they are used as default (and overridable) values to any cqerl:get_client calls. You can also provide a cassandra_nodes variable containing a list of the tuples used as the first argument to cqerl:get_client (see clusters for more explanations). So for example, in your app.config or sys.config file, you could have the following content:

[
  {cqerl, [
            {cassandra_nodes, [ { "127.0.0.1", 9042 } ]},
            {ssl, [ {cacertfile, "cassandra.pem"} ]},
            {auth, {cqerl_auth_plain_handler, [ {"test", "aaa"} ]}}
          ]},
]

Doing so will fire up connection pools as soon as the CQErl application is started. So when later on you call cqerl:get_client, chances are you will hit a preallocated connection (unless they're so busy that CQErl needs to fire up new ones). In fact, if you provide the cassandra_nodes environment variable, you can call cqerl:get_client/0, which chooses an available client at random.

Clusters

With CQErl clusters, you can configure either a single set of cassandra nodes from which you can draw a client at any time, or multiple sets that serve different purposes.

Single cluster

You can prepare a single cluster setup using this structure in your sys.config file:

[
  {cqerl, [ {cassandra_nodes, [ 
                % You can use any of the forms below to specify a cassandra node
                { "127.0.0.1", 9042 },
                { {127, 0, 0, 2}, 9042 },
                "127.0.0.3"
            ]},
            { keyspace, dev_keyspace }
          ]},
]

or, equivalently, there's an API you can use to add nodes to the single main cluster:

cqerl_cluster:add_nodes([
    { "127.0.0.1", 9042},
    { {127, 0, 0, 2}, 9042 },
    "127.0.0.3"
]).

or, with connection options:

cqerl_cluster:add_nodes([
    { "127.0.0.1", 9042},
    { {127, 0, 0, 2}, 9042 },
    "127.0.0.3"
], [
    { keyspace, dev_keyspace }
]).

When your main cluster is configured, you can just use cqerl:get_client/0 to get a client random from the cluster.

Multiple clusters

You can prepare multiple clusters using this structure in your sys.config file:

[
  {cqerl, [ {cassandra_clusters, [
                { config, {
                    [ "127.0.0.1", "127.0.0.3" ], 
                    [ { keyspace, config } ] 
                }},
                { operations, {
                    [ "127.0.0.1:9042", {"127.0.0.1", 9042} ], 
                    [ { keyspace, operations } ] 
                }}
            ]},
          ]},
]

or, equivalently, there's an API you can use to add nodes to particular clusters:

cqerl_cluster:add_nodes(config, [
    { "127.0.0.1", 9042},
    "127.0.0.3"
], [
    { keyspace, config }
]).
cqerl_cluster:add_nodes(operations, [
    { "127.0.0.1", 9042},
    "127.0.0.3"
], [
    { keyspace, operations }
]).
Options

There are two application environment variables that may be set to change query behaviour:

  • {maps, true} will cause query result rows to be returned as maps instead of proplists
  • {text_uuids, true} will cause timeuuid and uuid fields to be returned as binary strings in canonical form (eg <<"5620c844-e98d-11e5-b97b-08002719e96e">>) rather than pure binary.
Performing queries

Performing a query can be as simple as this:

{ok, Result} = cqerl:run_query(Client, "SELECT * FROM users;").

% Equivalent to
{ok, Result} = cqerl:run_query(Client, <<"SELECT * FROM users;">>).

% Also equivalent to
{ok, Result} = cqerl:run_query(Client, #cql_query{statement = <<"SELECT * FROM users;">>}).

It can also be performed asynchronously using

Tag = cqerl:send_query(Client, "SELECT * FROM users;"),
receive
    {result, Tag, Result} ->
        ok
end.

In situations where you do not need to wait for the response at all, it's perfectly fine to produce this sort of pattern:

{ok, Client} = cqerl:get_client(),
cqerl:send_query(Client, #cql_query{statement="UPDATE secrets SET value = null WHERE id = ?;",
                                    values=[{id, <<"42">>}]}).

That is, you can grab a client only the send a query, then you can get rid of it. CQErl will still perform it, the difference being that no response will be sent back to you.

Here's a rundown of the possible return values

  • SELECT queries will yield result of type #cql_result{} (more details below).
  • Queries that change the database schema will yield result of type #cql_schema_changed{type, keyspace, table}
  • Other queries will yield void if everything worked correctly.
  • In any case, errors returned by cassandra in response to a query will be the return value ({error, Reason} in the synchronous case, and {error, Tag, Reason} in the asynchronous case).
#cql_result{}

The return value of SELECT queries will be a #cql_result{} record, which can be used to obtain rows as proplists and fetch more result if available

{ok, _SchemaChange} = cqerl:run_query(Client, "CREATE TABLE users(id uuid, name varchar, password varchar);"),
{ok, void} = cqerl:run_query(Client, #cql_query{
    statement = "INSERT INTO users(id, name, password) VALUES(?, ?, ?);",
    values = [
        {id, new},
        {name, "matt"},
        {password, "qwerty"}
    ]
}),
{ok, Result} = cqerl:run_query(Client, "SELECT * FROM users;").

Row = cqerl:head(Result),
Tail = cqerl:tail(Result),
{Row, Tail} = cqerl:next(Result),
1 = cqerl:size(Result),
0 = cqerl:size(Tail),
empty_dataset = cqerl:next(Tail),
[Row] = cqerl:all_rows(Result),

<<"matt">> = proplists:get_value(name, Row),
<<"qwerty">> = proplists:get_value(password, Row).
Pagination

#cql_result{} can also be used to fetch the next page of result, if applicable, synchronously or asynchronously. This uses the automatic paging mechanism described here.

case cqerl:has_more_pages(Result) of
    true -> {ok, Result2} = cqerl:fetch_more(Result);
    false -> ok
end,

Tag2 = cqerl:fetch_more_async(Result),
receive
    {result, Tag2, Result2} -> ok
end.
#cql_schema_changed{}

#cql_schema_changed{} is returned from queries that change the database schema somehow (e.g. ALTER, DROP, CREATE, and so on). It includes:

  1. The type of change, either created, updated or dropped
  2. The name of the keyspace where the change happened, as a binary
  3. If applicable, the name of table on which the change was applied, as a binary
Providing options along queries

When performing queries, you can provide more information than just the query statement using the #cql_query{} record, which includes the following fields:

  1. The query statement, as a string or binary

  2. values for binding variables from the query statement (see next section).

  3. You can tell CQErl to consider a query reusable or not (see below for what that means). By default, it will detect binding variables and consider it reusable if it contains (named or not) any. Queries containing named binding variables will be considered reusable no matter what you set reusable to. If you explicitely set reusable to false on a query having positional variable bindings (?), you would provide values with in {Type, Value} pairs instead of {Key, Value}.

  4. You can specify how many rows you want in every result page using the page_size (integer) field. The devs at Cassandra recommend a value of 100 (which is the default).

  5. You can also specify what consistency you want the query to be executed under. Possible values include:

    • any
    • one
    • two
    • three
    • quorum
    • all
    • local_quorum
    • each_quorum
    • local_one
  6. In case you want to perform a lightweight transaction using INSERT or UPDATE, you can also specify the serial_consistency that will be use when performing it. Possible values are:

    • serial
    • local_serial
Variable bindings

In the #cql_query{} record, you can provide values as a proplists or map, where the keys are all atoms and match the column names or binding variable names in the statement, in lowercase.

Example:

% Deriving the value key from the column name
#cql_query{statement="SELECT * FROM table1 WHERE id = ?", values=[{id, SomeId}]},

% Explicitly providing a binding variable name
#cql_query{statement="SELECT * FROM table1 WHERE id = :id_value", values=[{id_value, SomeId}]},

Special cases include:

  • providing TTL and TIMESTAMP option in statements, in which case the proplist key would be [ttl] and [timestamp] respectively. Note that, while values for a column of type timestamp are provided in milliseconds, a value for the TIMESTAMP option is expected in microseconds.

  • UPDATE keyspace SET set = set + ? WHERE id = 1;. The name for this variable binding is set, the name of the column, and it's expected to be an erlang list of values.

  • UPDATE keyspace SET list = list + ? WHERE id = 1;. The name for this variable binding is list, the name of the column, and it's expected to be an erlang list of values.

  • UPDATE keyspace SET map[?] = 1 WHERE id = 1;. The name for this variable binding is key(map), where map is the name of the column.

  • UPDATE keyspace SET map['key'] = ? WHERE id = 1;. The name for this variable binding is value(map), where map is the name of the column.

  • UPDATE keyspace SET list[?] = 1 WHERE id = 1;. The name for this variable binding is idx(list), where list is the name of the column.

  • SELECT * FROM keyspace LIMIT ?. The name for the LIMIT variable is [limit].

    Also, when providing the value for a uuid-type column, you can give the value new, strong or weak, in which case CQErl will generate a random UUID (v4), with either a strong or weak number random generator.

    Finally, when providing the value for a timeuuid or timestamp column, you can give the value now, in which case CQErl will generate a normal timestamp, or a UUID (v1) matching the current date and time.

Batched queries

To perform batched queries (which can include any non-SELECT DML statements), simply put one or more #cql_query{} records in a #cql_query_batch{} record, and run it in place of a normal #cql_query{}. #cql_query_batch{} include the following fields:

  1. The consistency level to apply when executing the batch of queries.
  2. The mode of the batch, which can be logged, unlogged or counter. Running a batch in unlogged mode removes the performance penalty of enforcing atomicity. The counter mode should be used to perform batched mutation of counter values.
  3. Finally, you must specify the list of queries.
InsertQ = #cql_query{statement = "INSERT INTO users(id, name, password) VALUES(?, ?, ?);"},
{ok, void} = cqerl:run_query(Client, #cql_query_batch{
  mode=unlogged,
  queries=[
    InsertQ#cql_query{values = [{id, new},{name, "sean"},{password, "12312"}]},
    InsertQ#cql_query{values = [{id, new},{name, "jenna"},{password, "11111"}]},
    InsertQ#cql_query{values = [{id, new},{name, "kate"},{password, "foobar"}]}
  ]
}).
Reusable queries

If any of the following is true:

  • you set #cql_query{}'s reusable field to true
  • the query contains positional variable bindings (?) and you did not explicitely reusable to false
  • the query contains named variable bindings (:name) (ignores the value of reusable)

the query is considered reusable. This means that the first time this query will be performed, CQErl will ask the connected Cassandra node to prepare the query, after which, internally, a query ID will be used instead of the query statement when executing it. That particular cassandra node will hold on to the prepared query on its side and subsequent queries that use exactly the same statement will be performed faster and with less network traffic.

CQErl can tell which query has been previously prepared on which node by keeping a local cache, so all of this happens correctly and transparently.

Data types

Here is a correspondance of cassandra column types with their equivalent Erlang types (bold denotes what will used in result sets, the rest is what is accepted).

Cassandra Column Type Erlang types
ascii binary, string (only US-ASCII)
bigint integer (signed 64-bit)
blob binary
boolean true, false
counter integer (signed 64-bit)
date calendar:date()
decimal {Unscaled :: integer(), Scale :: integer()}
double float (signed 64-bit)
float float (signed 32-bit)
frozen<thing> same as <thing>
int integer (signed 32-bit)
time integer (milliseconds, signed 64-bit), now, binary or string
timestamp integer (milliseconds, signed 64-bit), now, binary or string
UDTs proplist or map depending on the maps option
uuid binary, new
varchar binary, string
varint integer (arbitrary precision)
timeuuid binary, now
inet {X1, X2, X3, X4} (IPv4), {Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8} (IPv6), string or binary
list list
map proplist or map depending on the maps option
set list
smallint integer
text binary, string
tinyint integer

Connecting to older Cassandra instances

By default, this client library assumes we're talking to a 2.2+ or 3+ instance of Cassandra. 2.1.x the latest native protocol (v4) which is required to use some of the newest datatypes and optimizations. To tell CQErl to use the older protocol version (v3), which is required to connect to a 2.1.x instance of Cassandra, you can set the protocol_version option to the integer 3, in your configuration file, i.e.

[
  {cqerl, [
            {cassandra_nodes, [ { "127.0.0.1", 9042 } ]},
            {protocol_version, 3}
          ]},
]

or in a cqerl:get_client/2 or cqerl:get_client/2 call

{ok, Client} = cqerl:get_client("127.0.0.1:9042", [{protocol_version, 3}, {keyspace, oper}]).

Installation

Just include this repository in your project's rebar.config file and run ./rebar get-deps. See rebar for more details on how to use rebar for Erlang project management.

Compatibility

As said earlier, this library uses Cassandra's newest native protocol versions (v4, or v3 optionally), which is said to perform better than the older Thrift-based interface. It also speaks CQL version 3, and uses new features available in Cassandra 2.X, such as paging, parametrization, query preparation and so on.

All this means is that this library works with Cassandra 2.1.x (2.2+ or 3+ recommended), configured to enable the native protocol. This documentation page gives details about the how to configure this protocol. In the cassandra.yaml configuration file of your Cassandra installation, the start_native_transport need to be set to true and you need to take note of the value for native_transport_port, which is the port used by this library.

Tests

CQErl includes a test suite that you can run yourself, especially if you plan to contribute to this project.

  1. Clone this repo on your machine
  2. Edit test/test.config and put your own cassandra's configurations
  3. At the project's top directory, run make test

cqex's People

Contributors

kelostrada avatar laibulle avatar matehat avatar mus0u avatar piglovesyou avatar rbino avatar rranelli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

cqex's Issues

uuid library conflict

(not sure if I should open this issue here or in the cqerl issue tracker...)

When I was starting to set up this library in my project I got the following
error when compiling my app:

> In mix.exs:
    {:uuid, ">= 1.1.3", [hex: :uuid]}

  > In deps/cqerl/rebar.config:
    {:uuid, ~r/.*/, [git: "https://github.com/okeuday/uuid.git", tag: "v1.4.1", manage
r: :rebar]}

I found out that the issue is because the :uuid dependencies are not the
same
.

Hex's one is: https://github.com/zyro/elixir-uuid and the one cqerl depends on
is https://github.com/okeuday/uuid.

From what I understood, it is not possible to have two dependencies with the
same name
.

In order to "fix" things for my project, I replaced my uuid entry in
mix.exs's deps field with:

{:uuid, github: "okeuday/uuid", tag: "v1.4.1", manager: :rebar, override: true},

(I needed the override: true because exq depends on elixir-uuid as well).

I can't see a solution to this problem without changing the name of
elixir-uuid or uuid.

To work-around the issue, I then defined a module UUID in terms of :uuid:

defmodule UUID do
  # Terrible and ugly hack
  def uuid4 do
    :uuid.get_v4
    |> :uuid.uuid_to_string
    |> List.to_string
  end
end

No Test?

It would be good idea to write tests to ensure that basic functionalities work properly with the driver.

Cannot insert date

When I try to insert date value, it fails.

** (Protocol.UndefinedError) protocol Enumerable not implemented for ~D[1959-12-22]
    (elixir) lib/enum.ex:1: Enumerable.impl_for!/1
    (elixir) lib/enum.ex:116: Enumerable.reduce/3
    (elixir) lib/enum.ex:1767: Enum.map/2
    (cqex) lib/cqex/query.ex:139: CQEx.Query.nullify/2
    (cqex) lib/cqex/query.ex:141: anonymous fn/2 in CQEx.Query.nullify/2
    (elixir) lib/enum.ex:1233: anonymous fn/3 in Enum.map/2
    (stdlib) lists.erl:1263: :lists.foldl/3
    (elixir) lib/enum.ex:1772: Enum.map/2

DESCRIBE table result:

cqlsh:lazzarone_dev> DESCRIBE users ;

CREATE TABLE lazzarone_dev.users (
    birthday date,
    id int,
    PRIMARY KEY (birthday, id)
) ...

a new release is needed

Last release has been made more than one year ago, a new release should be made and released to hex.pm.

Does not start..

If I try to run the release on debian, I got this error:

Erlang/OTP 19 [erts-8.3] [source] [64-bit] [smp:2:2] [async-threads:10] [kernel-poll:false]

2017-05-24 08:07:36 crash_report
initial_call: {supervisor,kernel,['Argument__1']}
pid: <0.1756.0>
registered_name: []
error_info: {exit,{on_load_function_failed,lz4},[{gen_server,init_it,6,[{file,"gen_server.erl"},{line,352}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,247}]}]}
ancestors: [kernel_sup,<0.1732.0>]
messages: []
links: [<0.1733.0>]
dictionary: []
trap_exit: true
status: running
heap_size: 376
stack_size: 27
reductions: 117
2017-05-24 08:07:36 supervisor_report
supervisor: {local,kernel_sup}
errorContext: start_error
reason: {on_load_function_failed,lz4}
offender: [{pid,undefined},{id,kernel_safe_sup},{mfargs,{supervisor,start_link,[{local,kernel_safe_sup},kernel,safe]}},{restart_type,permanent},{shutdown,infinity},{child_type,supervisor}]
2017-05-24 08:07:37 crash_report
initial_call: {application_master,init,['Argument__1','Argument__2','Argument__3','Argument__4']}
pid: <0.1731.0>
registered_name: []
error_info: {exit,{{shutdown,{failed_to_start_child,kernel_safe_sup,{on_load_function_failed,lz4}}},{kernel,start,[normal,[]]}},[{application_master,init,4,[{file,"application_master.erl"},{line,134}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,247}]}]}
ancestors: [<0.1730.0>]
messages: [{'EXIT',<0.1732.0>,normal}]
links: [<0.1730.0>,<0.1729.0>]
dictionary: []
trap_exit: true
status: running
heap_size: 376
stack_size: 27
reductions: 152
2017-05-24 08:07:37 std_info
application: kernel
exited: {{shutdown,{failed_to_start_child,kernel_safe_sup,{on_load_function_failed,lz4}}},{kernel,start,[normal,[]]}}
type: permanent
{"Kernel pid terminated",application_controller,"{application_start_failure,kernel,{{shutdown,{failed_to_start_child,kernel_safe_sup,{on_load_function_failed,lz4}}},{kernel,start,[normal,[]]}}}"}
Kernel pid terminated (application_controller) ({application_start_failure,kernel,{{shutdown,{failed_to_start_child,kernel_safe_sup,{on_load_function_failed,lz4}}},{kernel,start,[normal,[]]}}})

Can you please advice how to solve this issue?

How to get started?

Hello,

I have just installed Cassandra via docker, and installed cqex, but I can't see any docs about where to set the config ? like host, port, database name, user name, password?

timeout

I have just installed Cassandra 3.7, I tried to connect:

{:ok, client} = :cqerl.new_client({{x,x,x,x},9042},[{:auth, {:cqerl_auth_plain_handler, [{"user_name", "password"}]}}])

but I got this error:

** (exit) exited in: :gen_server.call(:cqerl, {:get_client, {{x,x,x,x}, 9042}, [auth: {:cqerl_auth_plain_handler, [{"user_name", "password"}]}]})
** (EXIT) time out
(stdlib) gen_server.erl:204: :gen_server.call/2

Any advice?

Does not write..

I tried to do a simple example:

def write() do
client = CQEx.Client.new!
base = Q.new
|> Q.statement("INSERT INTO mydb.locations (user_id, date, unix_time, lat, long) values (?, ?, ?, ?, ?);")
|> Q.put(:user_id, 0)
|> Q.put(:date, '2016-5-12')
|> Q.put(:unix_time, 1234567890)
|> Q.put(:lat, 3.23)
|> Q.put(:long, 3.4321)

end

but, nothing was written, is there anything I am missing?

Shall I use the same client with multiple queries?

Which is better? this example:

def last_locations(conn, params) do
  user_ids= Poison.decode!(params["user_ids"])

  locations= user_ids
  |> Enum.map(&Task.async(fn -> last_location(&1) end))
  |> Enum.map(&Task.await/1)
  
  json(conn, locations)
end

defp last_location(user_id) do
    client = CQEx.Client.new!
    query= "SELECT * from myapp.latests WHERE user_id = #{user_id};"
    for [user_id: _, lat: lat, long: long, unix_time: unix_time] <- Q.call!(client, query) 
    |> Enum.to_list,
    do: [lat, long, unix_time]
end

or this example: ? (where I am using one client for all queries)

def last_locations(conn, params) do
  user_ids= Poison.decode!(params["user_ids"])
  client = CQEx.Client.new!
  locations= user_ids
  |> Enum.map(&Task.async(fn -> last_location(&1, client) end))
  |> Enum.map(&Task.await/1)
  
  json(conn, locations)
end

defp last_location(user_id, client) do
    
    query= "SELECT * from myapp.latests WHERE user_id = #{user_id};"
    for [user_id: _, lat: lat, long: long, unix_time: unix_time] <- Q.call!(client, query) 
    |> Enum.to_list,
    do: [lat, long, unix_time]
end

Using keyspace breaks CQEx

There seems to be a bug in CQEx.Result.convert/2, the case of setting the keyspace seems not to be handled:

Calling

stmt = """
CREATE KEYSPACE foo 
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3}
"""
Query.call(client, stmt)
Query.call(client, "USE foo")

fails with

** (FunctionClauseError) no function clause matching in CQEx.Result.convert/2

    The following arguments were given to CQEx.Result.convert/2:

        # 1
        {:set_keyspace, "foo"}

        # 2
        {#PID<0.474.0>, #Reference<0.2977995123.3760455683.197727>}

    Attempted function clauses (showing 3 out of 3):

        def convert(r, _client) when is_atom(:cql_result) and is_tuple(r) and tuple_size(r) > 0 and :erlang.element(1, r) == :cql_result
        def convert(q, client) when is_atom(:cql_schema_changed) and is_tuple(q) and tuple_size(q) > 0 and :erlang.element(1, q) == :cql_schema_changed
        def convert(:void, client)

    (cqex) lib/cqex/result.ex:35: CQEx.Result.convert/2
    (cqex) lib/cqex/query.ex:66: CQEx.Query.call/2

Calling

    {:ok, {:set_keyspace, "foo"}} = :cqerl.run_query(client, "USE foo")

directly works.

Cannot compile

New to elixir and not sure whether this belongs in here but could be linked. I have created a test case where I am trying to connect to a remote cassandra instance however when I try to run it(via mix test test/conn_test:10), the escript.build or just a normal mix deps.compile it says the following:

** (Mix) Could not compile dependency :pooler, "/home/ubuntu/.mix/rebar3 bare compile --paths "/vagrant/test/_build/test/lib/*/ebin"" command failed. You can recompile this dependency with "mix deps.compile pooler", update it with "mix deps.update pooler" or clean it with "mix deps.clean pooler"

Tried doing as suggested a compile, update and clean but to no avail.

Some initial research pointed me to the TEST flag that gets defined and that tries to "export all functions" in pooler. If i manually go to the files mentioned and comment out the export encapsulated in the TEST flag condition, it works fine.

Any idea how to bypass this issue?

Using Maps for results

Hey, maybe a newbie question.
It seems that the CQEx returns a Keyword list as results
Is there any way to get a Map back? (without explicit conversion of each value, which seems to be expensive)

i.e doing something like this:

%{:id => id} = client |> CQEx.Query.call!("SELECT * FROM projects;") |> CQEx.Result.head

Doesn't work

Hi,

I followed your readme. Added those two lines into config, and the just tried to start with client, but everything fall apart.

iex(1)> client = CQEx.Client.new!
** (ArgumentError) argument error
(stdlib) :ets.lookup(:cqerl_clusters, :"$primary_cluster")
src/cqerl_cluster.erl:52: :cqerl_cluster.get_any_client/1
lib/cqex/client.ex:17: CQEx.Client.new!/0
iex(1)>

Any idea?

Batch Queries

I see some API for batch queries, how exactly do I use them? Thanks

Error while starting client

When I start the phoenix server, I get this error:

Error while starting client {{"localhost",9042},
                             [{keyspace,<<"schools">>},
                              {cassandra_clusters,
                                  [{schools_cluster,
                                       {[{<<"localhost">>,9042}],
                                        [{keyspace,<<"schools">>}]}}]},
                              {included_applications,[]}]} for cluster schools_cluster:
shutdown

I can confirm that I can connect successfully to Cassandra using DBeaver client, or via cqlsh on terminal, as I have created the keyspace schools

the error above has no details, I just don't know what's the issue, any idea?

CQEx.Client.close does not work

iex(16)> CQEx.Client.close c
** (UndefinedFunctionError) undefined function: :cqerl.close/1
    (cqerl) :cqerl.close({#PID<0.723.0>, #Reference<0.0.4.492>})

Compilation fails in Elixir 1.8

Environemnt info:

Erlang: 21
Elixir: 1.8.1
OS: macOS High Sierra (10.13.6)
cqex: 0.2.0

I was trying out your library & it won't compile. I did mix clean, mix deps.clean cqex, then mix deps.compile cqex and I get the same error every time. I also tried out your other library cqerl which always times out and never connects to my Cassandra cluster. Not sure if this problem is also causing cqex to break, but it seems that this library needs to be updated to work with Elixir 1.8.

iex -S mix
Erlang/OTP 21 [erts-10.3] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe] [dtrace]

warning: variable "description" does not exist and is being expanded to "description()", please use parentheses to remove the ambiguity or change the variable name
  /Users/mhakeem/Documents/codes/elixir/cassandra/example/deps/cqex/mix.exs:10

warning: variable "package" does not exist and is being expanded to "package()", please use parentheses to remove the ambiguity or change the variable name
  /Users/mhakeem/Documents/codes/elixir/cassandra/example/deps/cqex/mix.exs:11

warning: variable "deps" does not exist and is being expanded to "deps()", please use parentheses to remove the ambiguity or change the variable name
  /Users/mhakeem/Documents/codes/elixir/cassandra/example/deps/cqex/mix.exs:13

==> cqex
Compiling 6 files (.ex)
warning: variable "args" is unused (if the variable is not meant to be used, prefix it with an underscore)
  lib/cqex/helpers.ex:19

warning: unused import CQEx.Helpers
  lib/cqex/cluster.ex:2


== Compilation error in file lib/cqex/client.ex ==
** (ArgumentError) argument error
    :erlang.length(nil)
    expanding macro: CQEx.Helpers.defbang/1
    lib/cqex/client.ex:17: CQEx.Client (module)
    (elixir) lib/kernel/parallel_compiler.ex:208: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6
could not compile dependency :cqex, "mix compile" failed. You can recompile this dependency with "mix deps.compile cqex", update it with "mix deps.update cqex" or clean it with "mix deps.clean cqex"

Cannot compile

When I try to compile source in a cqex directory, I got this. I'm using mix.lock version of dependencies.

$ mix deps.compile
===> Compiling re2
===> Compiling /home/pig/git/cqex/deps/re2/c_src/re2_nif.cpp
===> /home/pig/git/cqex/deps/re2/c_src/re2_nif.cpp:8:21: fatal error: re2/re2.h: No such file or directory
 #include <re2/re2.h>
                     ^
compilation terminated.

** (Mix) Could not compile dependency :re2, "/home/pig/.mix/rebar3 bare compile --paths "/home/pig/git/cqex/_build/dev/lib/*/ebin"" command failed. You can recompile this dependency with "mix deps.compile re2", update it with "mix deps.update re2" or clean it with "mix deps.clean re2"

Argument error

Hi,

I have troubles connecting to my Cassandra cluster.

My configuration files looks like this:

use Mix.Config

config :cqerl, 
 cassandra_nodes: [ "192.168.99.100:9042", "192.168.99.100:9042" ],
 keyspace: "hevis"

After calling client = CQEx.Client.new!, I'm getting the following error message:

** (ArgumentError) argument error
    (stdlib) :ets.lookup(:cqerl_clusters, :"$primary_cluster")
             src/cqerl_cluster.erl:52: :cqerl_cluster.get_any_client/1
             lib/cqex/client.ex:17: CQEx.Client.new!/0

Any hints are very welcome :)
I fairly new to elixir, so hopefully I haven't missed an obvious step.

Thanks,
Seb

cqex in travis ci

I haven't been able to get cqex to work within a travis ci build. Anybody out there that has been able to get it to work willing to offer some guidance? I'm getting this error after confirming cassandra was successfully installed by running cqlsh:

Error while starting client {{{127,0,0,1},9042},
                             [{cassandra_nodes,[<<"127.0.0.1:9042">>]},
                              {included_applications,[]},
                              {keyspace,<<"fountain_test">>}]} for cluster '$primary_cluster':shutdown

This is what my .travis.yml looks like:

sudo: required
dist: trusty
language: elixir
elixir:
  - 1.3.3
otp_release:
  - 19.0
services:
  - redis-server
before_install:
  # install cassandra locally:
  - sudo rm -rf /var/lib/cassandra/*
  - wget https://archive.apache.org/dist/cassandra/3.4/apache-cassandra-3.4-bin.tar.gz
  - tar -xvzf apache-cassandra-3.4-bin.tar.gz
  - sudo mkdir apache-cassandra-3.4/logs
  - sudo touch apache-cassandra-3.4/logs/gc.log
  - sudo sh apache-cassandra-3.4/bin/cassandra -R > /dev/null
  - for i in {1..10}; do cqlsh -e "DESCRIBE keyspaces;" && break || sleep 5; done
# install Neo4j locally:
  - wget dist.neo4j.org/neo4j-community-2.3.2-unix.tar.gz
  - tar -xzf neo4j-community-2.3.2-unix.tar.gz
  - sed -i.bak s/dbms.security.auth_enabled=true/dbms.security.auth_enabled=false/g neo4j-community-2.3.2/conf/neo4j-server.properties
  - neo4j-community-2.3.2/bin/neo4j start

Pool size and configuration

I keep getting the following error often:
pool 'g2gDaARhf2EAYQBhAWIAACNSZAAJdW5kZWZpbmVk' failed to start member: {:error, {:connection_error, :econnrefused}}

Does it have anything to do with pool size? If so, how to configure min and max pool size in cqex?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.