如何使用Node.js驱动程序将REQ.Body的数据发布到CQL UDT列中?

发布于 2025-02-06 12:12:51 字数 1067 浏览 2 评论 0原文

我是Cassandra的新手,我需要您的帮助。

使用CQL控制台创建收集表后,我能够创建新的记录并读取它们,但是使用nodejs中的Cassandra-Driver进行后操作不起作用,仅在使用CQL控制台时才有效。

我创建了表:

CREATE TYPE event_info (
    type text,
    pagePath text,
    ts text,
    actionName text
);
CREATE TABLE journey_info_5 (
    id uuid PRIMARY KEY,
    user_id text,
    session_start_ts timestamp,
    event FROZEN<event_info>
);

邮政操作的代码:

export const pushEvent = async(req,res)=>{
    const pushEventQuery = 'INSERT INTO user_journey.userjourney (id, user_id, session_start_ts,events)
    VALUES ( ${types.TimeUuid.now()}, ${req.body.user_id},${types.TimeUuid.now()},
     { ${req.body.type},${req.body.pagePath},${req.body.ts},${req.body.actionName}} } );'

    try {
        
        await client.execute(pushEventQuery)
       res.status(201).json("new record added successfully");

    } catch (error) {
        res.status(404).send({ message: error });
        console.log(error);
    }
}

它给出错误,如何从用户中获取数据并在此集合中发布? 如果有的话,请帮助我

I am new to cassandra I need your help.

After creating a collection table using cql console, I am able to create new records and read them, but Post operation using cassandra-driver in nodejs is not working, it only works when I use cql console.

I created table:

CREATE TYPE event_info (
    type text,
    pagePath text,
    ts text,
    actionName text
);
CREATE TABLE journey_info_5 (
    id uuid PRIMARY KEY,
    user_id text,
    session_start_ts timestamp,
    event FROZEN<event_info>
);

codes for post operation:

export const pushEvent = async(req,res)=>{
    const pushEventQuery = 'INSERT INTO user_journey.userjourney (id, user_id, session_start_ts,events)
    VALUES ( ${types.TimeUuid.now()}, ${req.body.user_id},${types.TimeUuid.now()},
     { ${req.body.type},${req.body.pagePath},${req.body.ts},${req.body.actionName}} } );'

    try {
        
        await client.execute(pushEventQuery)
       res.status(201).json("new record added successfully");

    } catch (error) {
        res.status(404).send({ message: error });
        console.log(error);
    }
}

it is giving errors, How can I get data from user and post in this collection?
please help me, if any idea

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹉夏雨初晴づ 2025-02-13 12:12:51

问题是您的CQL语句无效。用于在用户定义类型(UDT)列中插入值的格式是:

    { fieldname1: 'value1', fieldname2: 'value2', ... }

请注意,模式中的列名与您的代码中的CQL语句不匹配,因此我在此处重新启动架构,以确保

CREATE TYPE community.event_info (
    type text,
    pagepath text,
    ts text,
    actionname text
)
CREATE TABLE community.journey_info_5 (
    id uuid PRIMARY KEY,
    event frozen<event_info>,
    session_start_ts timestamp,
    user_id text
)

: CQL语句我用来将UDT插入表中(格式化以可读性):

INSERT INTO journey_info_5 (id, user_id, session_start_ts, event)
  VALUES (
    now(),
    'thierry',
    totimestamp(now()),
    {
      type: 'type1',
      pagePath: 'pagePath1',
      ts: 'ts1',
      actionName: 'actionName1'
    }
  );

有关参考,请参见将数据插入或更新到UDT列。干杯!

The issue is that your CQL statement is invalid. The format for inserting values in a user-defined type (UDT) column is:

    { fieldname1: 'value1', fieldname2: 'value2', ... }

Note that the column names in your schema don't match up with the CQL statement in your code so I'm reposting the schema here for clarity:

CREATE TYPE community.event_info (
    type text,
    pagepath text,
    ts text,
    actionname text
)
CREATE TABLE community.journey_info_5 (
    id uuid PRIMARY KEY,
    event frozen<event_info>,
    session_start_ts timestamp,
    user_id text
)

Here's the CQL statement I used to insert a UDT into the table (formatted for readability):

INSERT INTO journey_info_5 (id, user_id, session_start_ts, event)
  VALUES (
    now(),
    'thierry',
    totimestamp(now()),
    {
      type: 'type1',
      pagePath: 'pagePath1',
      ts: 'ts1',
      actionName: 'actionName1'
    }
  );

For reference, see Inserting or updating data into a UDT column. Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文