如何在gleam中使用xandra(elixir包)?

问题描述 投票:0回答:1

我想尝试使用 Gleam 编程语言(我是初学者)来尝试 ScyllaDB,但在与 gleam 建立连接方面遇到了困难。我无法在 gleam 中找到任何 cassandra 驱动程序,因此在这种情况下使用 @external 属性是我唯一的选择。

我使用docker轻松初始化2个scyladb节点:

services:
  gleam:
    build:
      context: .
      dockerfile: Dockerfile-dev
    # command: 
    container_name: server
    ports:
      - 9163:8080
    volumes:
      - ./wnw_backend/:/app/wnw_backend
    networks:
      - web

  scylladb-node1:
    image: scylladb/scylla:latest
    container_name: scylladb-node1
    ports:
      - '9042:9042'
      - '9160:9160'
      - '7004:7000'
      - '7005:7001'
      - '10000:10000'
    volumes:
      - type: volume
        source: target
        target: /var/lib/scylla
        volume:
          nocopy: true
    environment:
      - SCYLLA_ARGS=--developer-mode 1
    healthcheck:
      test: ["CMD-SHELL", "cqlsh -e 'SELECT now() FROM system.local' || exit 1"]
      start_period: 30s
      interval: 10s
      timeout: 10s
      retries: 10
    restart: always
    networks:
      - web

  scylladb-node2:
    image: scylladb/scylla:latest
    container_name: scylladb-node2
    depends_on:
      scylladb-node1:
        condition: service_healthy
    ports:
      - '9043:9042'
      - '9161:9160'
      - '7006:7000'
      - '7007:7001'
      - '10001:10000'
    volumes:
      - type: volume
        source: target
        target: /var/lib/scylla
        volume:
          nocopy: true
    environment:
      - SCYLLA_ARGS=--developer-mode 1
    command: --seeds=scylladb-node1
    healthcheck:
      test: ["CMD-SHELL", "cqlsh -e 'SELECT now() FROM system.local' || exit 1"]
      start_period: 30s
      interval: 10s
      timeout: 10s
      retries: 10
    restart: always
    networks:
      - web

volumes:
  target:

networks:
  web:
    driver: bridge
    # external: true

我当前编写的代码如下所示:

import app/router
import dot_env as dot
import dot_env/env
import gleam/dynamic.{type Dynamic}
import gleam/erlang/process
import gleam/int
import gleam/io
import mist
import wisp

pub fn main() {
  wisp.configure_logger()

  dot.new()
  |> dot.set_path("/app/.env")
  |> dot.set_debug(False)
  |> dot.load

  let assert Ok(secret_key) = env.get("SECRET_KEY_BASE")

  let assert Ok(conn) =
    start_link(["scylladb-node1:9042", "scylladb-node2:9042"])

  io.println("xandra" <> int.to_string(conn) <> ".")
  io.println(secret_key)

  let assert Ok(_) =
    router.handle_request(_)
    |> wisp.mist_handler(secret_key)
    |> mist.new()
    |> mist.port(8080)
    |> mist.start_http()

  process.sleep_forever()
}

@external(erlang, "Xandra", "start_link")
pub fn start_link(nodes: List(String)) -> Result(Int, Dynamic)

编译日志:

/app/wnw_backend # gleam run
  Compiling wnw_backend
   Compiled in 1.45s
    Running wnw_backend.main
exception error: undefined function 'Xandra':start_link/1
  in function  wnw_backend:main/0 (/app/wnw_backend/build/dev/erlang/wnw_backend/_gleam_artefacts/wnw_backend.erl, line 29)/app/wnw_backend #

现在我觉得我可能误解了 @external 属性的目的。如果是这样,我错过了什么?

docker-compose elixir scylla gleam cassandra-driver-xandra
1个回答
0
投票

Elixir 模块的前缀为 Elixir - Rawhat(来自 Discord)

@external(erlang, "Elixir.Xandra", "start_link")
pub fn start_link(nodes: List(String)) -> Result(Atom, a)
© www.soinside.com 2019 - 2024. All rights reserved.