如何使用python3在cql中准备select查询?

mqkwyuun  于 2021-06-14  发布在  Cassandra
关注(0)|答案(2)|浏览(419)

就像我可以准备的那样 INSERT 请问,我该怎么准备 SELECT 在cql python中查询?插入语句:

insert_sql = self.session.prepare(
            (
                "INSERT INTO  {} ({}, {}, {}) VALUES (?,?,?)"
            ).format(
                self.table_name, "id", "version", "row"
            )
        )

并以以下方式执行:

params = (key, "version_1", row, )
                futures.append(
                    (
                        insert_sql,
                        params
                    )
                )
            # Checking whether the formed keys already exists
            results = execute_concurrent(
                self.session, futures, concurrency=1000, raise_on_first_error=False
            )

当我对你做同样的事 SELECT :

select_sql = self.session.prepare(
            (
                "SELECT * FROM {} WHERE {}=%s AND {}=%s AND {}!=%s"
            ).format(
                self.table_name, "id", "version", "row"
            )
        )

并以以下方式执行:

params = (key, "version_1", row, )
                futures.append(
                    (
                        select_sql,
                        params
                    )
                )
            # Checking whether the formed keys already exists
            results = execute_concurrent(
                self.session, futures, concurrency=1000, raise_on_first_error=False
            )

我面临一个错误:

cassandra.InvalidRequest: Error from server: code=2200 [Invalid query] message="Unsupported "!=" relation: row != ?"
Traceback (most recent call last):
  File "getting_started.py", line 145, in <module>
    example1.insert_data()
  File "getting_started.py", line 97, in insert_data
    self.table_name, "id", "version", "row"
  File "cassandra/cluster.py", line 2405, in cassandra.cluster.Session.prepare
  File "cassandra/cluster.py", line 2402, in cassandra.cluster.Session.prepare
  File "cassandra/cluster.py", line 4062, in cassandra.cluster.ResponseFuture.result
cassandra.InvalidRequest: Error from server: code=2200 [Invalid query] message="Unsupported "!=" relation: row != ?"

换了衣服之后呢 !=NOT 如代码所示:

select_query = (
                "SELECT * FROM {} WHERE {}=%s AND {}=%s AND NOT {}=%s"
            ).format(
                self.table_name, "id", "version", "row"
            )
        print(select_query)
        select_sql = self.session.prepare(
            select_query
        )
        for df in chunks:
            futures = []
            df = df.to_dict(orient='records')
            chunk_counter += 1
            for row in df:
                key = str(row["0"])
                row = json.dumps(row, default=str)
                params = (key, "version_1", row, )
                futures.append(
                    (
                        select_sql,
                        params
                    )
                )
            # Checking whether the formed keys already exists
            results = execute_concurrent(
                self.session, futures, concurrency=1000, raise_on_first_error=False
            )
            for (success, result) in results:
                if not success:
                    self.handle_error(result)
                print(result)
                sys.exit()
            results = execute_concurrent(
                self.session, futures, concurrency=1000, raise_on_first_error=False)
            for (success, result) in results:
                if not success:
                    self.handle_error(result)  # result will be an Exception
            if (chunk_counter % 1000 == 0):
                self.log.info(
                    "{} 's Batch Insert Completed".format(chunk_counter))
            # print(
            #     str(chunk_counter*chunksize) + " : " +
            #     str(datetime.utcnow() - start_time)
            # )
        print("Complete task's duration is: {}".format(
            datetime.utcnow() - start_time))

我明白了:

etting_started.py:8: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from cassandra.cluster import BatchStatement, Cluster
2019-01-19 19:49:16,582 [INFO] root: setting keyspace...
SELECT * FROM TenMillion WHERE id=%s AND version=%s AND NOT row=%s
2019-01-19 19:49:16,590 [ERROR] cassandra.cluster: Error preparing query:
Traceback (most recent call last):
  File "cassandra/cluster.py", line 2402, in cassandra.cluster.Session.prepare
  File "cassandra/cluster.py", line 4062, in cassandra.cluster.ResponseFuture.result
cassandra.protocol.SyntaxException: <Error from server: code=2000 [Syntax error in CQL query] message="line 1:34 no viable alternative at character '%'">
Traceback (most recent call last):
  File "getting_started.py", line 147, in <module>
    example1.insert_data()
  File "getting_started.py", line 100, in insert_data
    select_query
  File "cassandra/cluster.py", line 2405, in cassandra.cluster.Session.prepare
  File "cassandra/cluster.py", line 2402, in cassandra.cluster.Session.prepare
  File "cassandra/cluster.py", line 4062, in cassandra.cluster.ResponseFuture.result
cassandra.protocol.SyntaxException: <Error from server: code=2000 [Syntax error in CQL query] message="line 1:34 no viable alternative at character '%'">
lyr7nygr

lyr7nygr1#

Cassandra似乎不支持 !=SELECT 点击此处:https://docs.datastax.com/en/cql/3.3/cql/cql_reference/cqlselect.html
你必须写一个没有 != 管理代码中的逻辑。

ncecgwcz

ncecgwcz2#

该错误返回消息=“unsupported”!=“.”“!=”不支持。尝试将选择代码更改为:

select_sql = self.session.prepare(
            (
                "SELECT * FROM {} WHERE {}=%s AND {}=%s AND NOT {}=%s"
            ).format(
                self.table_name, "id", "version", "row"
            )
        )

相关问题