【问题标题】:Not able to connect to a keyspace using C driver for Cassandra无法使用 Cassandra 的 C 驱动程序连接到键空间
【发布时间】:2020-01-08 08:16:37
【问题描述】:

我有以下代码用于插入 Cassandra 的简单查询。我在这里尝试使用准备好的语句,因为常规语句无法在表time_demo 的时间戳列中插入时间戳。

      CassFuture* connect_future = NULL;
      CassCluster* cluster = cass_cluster_new();
      CassSession* session = cass_session_new();
      char* hosts = "127.0.0.1";

      time_t rt= time(NULL);
      struct tm * timeinfo;

      timeinfo = localtime ( &rt );
      char lt[20];
      strftime(lt, sizeof(lt), "%Y-%m-%d %T", timeinfo);

      /* Add contact points */
      cass_cluster_set_contact_points(cluster, hosts);

      /* Provide the cluster object as configuration to connect the session  with a specified keyspace*/
      connect_future = cass_session_connect_keyspace(session, cluster,"test_keyspace");

      //After this line program exits
      CassFuture* prepare_future
          = cass_session_prepare(session, "INSERT INTO time_demo(id,time) VALUES(now(),?);");

在最后一行之后,我的程序突然结束。我想连接到键空间test_keyspace,同时还使用准备好的语句。我猜该程序因此而被终止,因为我没有为它正确编写代码。

谁能指出我在这里犯的错误?我正在为 C 使用 Cassandra 2.13 驱动程序。

【问题讨论】:

    标签: c cassandra cassandra-driver


    【解决方案1】:

    您需要等到连接建立 - 最简单的方法是使用类似的东西:

    CassError rc = cass_future_error_code(connect_future);
    

    在你得到驱动程序的OK后,你可以开始准备查询,但你还需要等待准备的结果,可能也是cass_future_error_code,或者其他方式。

    否则,您会触发 2 个异步操作而不等待结果,然后您的程序就会完成 - C/C++ 驱动程序在设计上是异步的。正确使用方法请参考Getting Started文档。

    【讨论】:

    • 非常感谢。现在代码正在运行,但还有其他问题。我会问它作为另一个问题。
    • 您也可以在 C++ 驱动程序邮件列表中询问,或在community.datastax.com
    • 感谢您的建议。我会这样做的。
    猜你喜欢
    • 2017-06-23
    • 2016-02-18
    • 2015-08-16
    • 2017-08-07
    • 1970-01-01
    • 2023-03-11
    • 2016-12-22
    • 2018-04-16
    • 2016-07-26
    相关资源
    最近更新 更多