sahilpaudel

sahilpaudel

I am trying to use Xandra for my Phoenix project for which I am connecting to Cassandra like

{:ok, conn} = Xandra.start_link([
  nodes: ["localhost:9042"]
])

Every time I am suppose to run a query I need to start_link, Is there a way I can provide all connection details in config file and it runs once the app is started?

Thanks

Showing Posts 1 to 9

sahilpaudel

sahilpaudel OP

Anyone with help is appreciated.

Qqwy

Qqwy

TypeCheck Core Team

Not in the config file, but in (one of) the supervisor(s) of your application. See here.

However, how one obtains a conn datatype to pass to all of the other functions when using Xandra (or another DBConnection-implementation for that matter) inside a supervision tree is something I cannot immediately figure out.

Maybe library author @whatyouhide can shed some light on this?

EDIT: It’s probably {Xandra, name: MyXandraConnection}, as now the connection GenServer is registered node-wide with that particular name. Then you can use MyXandraConnection everywhere you passed a conn PID before.

sahilpaudel

sahilpaudel OP

Thanks @Qqwy for the details analysis but I am relatively new to this stuff.

I am not sure where the connection details would go where it will be started the first time.

Qqwy

Qqwy

TypeCheck Core Team

Probably something like {Xandra, name: MyXandraConnection, other: option, here: probably}

whatyouhide

whatyouhide

Elixir Core Team

You can can read the configuration from the application environment manually:

# in config/config.exs (or in an environment-specific config file)
config :my_app, :my_xandra, options

and then:

# in my_app/application.ex
xandra_config = Application.fetch_env!(:my_app, :my_xandra)

children = [
  # ...,
  {Xandra, xandra_config}
]
sahilpaudel

sahilpaudel OP

Thank you @whatyouhide for the solution but I am still getting few errors with this approach.

dev.exs

config :my_app, :xandra,
  nodes: System.get_env("CASSANDRA_HOST"),
  authentication: {Xandra.Authenticator.Password, 
        [
          username: System.get_env("CASSANDRA_USER"),
          password: System.get_env("CASSANDRA_PASSWORD")
        ]
  },
  pool: DBConnection.Poolboy,
  pool_size: 10

application.exs

children = [
      # Start the Ecto repository
      supervisor(MyApp.Repo, []),
      # Start the endpoint when the application starts
      supervisor(MyApp.Web.Endpoint, []),
      worker(Cachex, [:app_cache, []]),
      worker(MyApp.Scheduler, []),
      {Xandra, Application.fetch_env!(:my_app, :xandra)}
    ]

Stacktrace

(Mix) Could not start application my_app: MyApp.Application.start(:normal, []) returned an error: shutdown: failed to start child: Xandra
    ** (EXIT) an exception was raised:
        ** (ArgumentError) multi-node use requires Xandra.Cluster instead of Xandra
            (xandra) lib/xandra.ex:1027: Xandra.parse_nodes_option/1
            (xandra) lib/xandra.ex:1010: Xandra.convert_nodes_options_to_address_and_port/1
            (xandra) lib/xandra.ex:336: Xandra.start_link/1
            (stdlib) supervisor.erl:379: :supervisor.do_start_child_i/3
            (stdlib) supervisor.erl:365: :supervisor.do_start_child/2
            (stdlib) supervisor.erl:349: anonymous fn/3 in :supervisor.start_children/2
            (stdlib) supervisor.erl:1157: :supervisor.children_map/4
            (stdlib) supervisor.erl:315: :supervisor.init_children/2
            (stdlib) gen_server.erl:374: :gen_server.init_it/2
            (stdlib) gen_server.erl:342: :gen_server.init_it/6
            (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
49: :proc_lib.init_p_do_apply/3

Then I changed the info in application.exs to

children = [
      # Start the Ecto repository
      supervisor(MyApp.Repo, []),
      # Start the endpoint when the application starts
      supervisor(MyApp.Web.Endpoint, []),
      worker(Cachex, [:app_cache, []]),
      worker(MyApp.Scheduler, []),
      {Xandra.Cluster, Application.fetch_env!(:my_app, :xandra)}
    ]

I got new exception

Could not start application my_app: MyApp.Application.start(:normal, []) returned an error: shutdown: failed to start child: Xandra.Cluster
    ** (EXIT) an exception was raised:
        ** (Protocol.UndefinedError) protocol Enumerable not implemented for "localhost:9042". This protocol is implemented for: Ecto.Adapters.SQL.Stream, Postgrex.Stream, Xandra.PageStream, Xandra.Page, DBConnection.Stream, DBConnection.PrepareStream, Timex.Interval, HashSet, Range, Map, Function, List, Stream, Date.Range, HashDict, GenEvent.Stream, MapSet, File.Stream, IO.Stream
            (elixir) /Users/sahilpaudel/.kiex/builds/elixir-git/lib/elixir/lib/enum.ex:1: Enumerable.impl_for!/1
            (elixir) /Users/sahilpaudel/.kiex/builds/elixir-git/lib/elixir/lib/enum.ex:141: Enumerable.reduce/3
            (elixir) lib/enum.ex:3015: Enum.map/2
            (xandra) lib/xandra/cluster.ex:204: Xandra.Cluster.start_link/1
            (stdlib) supervisor.erl:379: :supervisor.do_start_child_i/3
            (stdlib) supervisor.erl:365: :supervisor.do_start_child/2
            (stdlib) supervisor.erl:349: anonymous fn/3 in :supervisor.start_children/2
            (stdlib) supervisor.erl:1157: :supervisor.children_map/4
            (stdlib) supervisor.erl:315: :supervisor.init_children/2
            (stdlib) gen_server.erl:374: :gen_server.init_it/2
            (stdlib) gen_server.erl:342: :gen_server.init_it/6
            (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

My env file

export CASSANDRA_HOST=localhost:9042
export CASSANDRA_USER=
export CASSANDRA_PASSWORD=

Thanks.

whatyouhide

whatyouhide

Elixir Core Team

The nodes option requires a list of strings, but you are reading a single string from the system environment. Use nodes: [System.get_env("CASSANDRA_HOST")].

sahilpaudel

sahilpaudel OP

Great, it worked.

Final question how can I execute a query now?

Thanks.

whatyouhide

whatyouhide

Elixir Core Team

You need to pass the name option to give a name to the Xadnra pool (such as name: MyXandra) and then you can use Xandra.execute or similar functions to execute queries. Refer to the docs for more information :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews