在 Python 中使用 Cassandra 时,<CassandraResult>.all() 会擦除使用中的数据吗?

Does <CassandraResult>.all() wipe data on use, while using Cassandra in Python?

Table 内容,

# Insert stuff into the table
query = 'insert into music_lib (year, artist_name, album_name) values (%s, %s, %s)'
try:
    session.execute(query, (2010, 'Linkin Park', 'A Thousand Suns'))
    session.execute(query, (2012, 'Linkin Park', 'Living Things'))
except Exception as e:
    print(e)

我的查询是这样的,

try:
    rows = session.execute('SELECT * FROM MUSIC_LIB')
except Exception as e:
    print(e)

当我第一次执行rows.all()时,我得到了我的结果,

[Row(year=2010, artist_name='Linkin Park', album_name='A Thousand Suns'),
 Row(year=2012, artist_name='Linkin Park', album_name='Living Things')]

但是当我再次执行rows.all()时,我得到一个空白输出, []

调用 rows.all() 时数据是否被擦除?

当你在做 session.execute 时,它 return 就是 ResultSet object, that is iterator over data that is received from Cassandra, and these results are paged。但是数据并没有存储在内存中进行随机访问,所以当你迭代时,你会跳到下一条记录,并且可能不会 return 到上一条。

当您调用 all(这对大数据集很危险!)时,它基本上是在调用 list(result_set),在内部对所有行执行迭代。但是当你第二次调用它时,迭代器已经用完了,你收到的是空列表。为防止这种情况,您需要将第一次调用 all 的结果保存在某处。

我建议阅读 Getting Started 文档,并阅读最佳实践,例如使用准备好的语句等。另外,请注意 select * from table 是 Cassandra 的反模式,并且当您获得足够大的数据集时,很可能会对您造成沉重打击。