python - 使用 Python 和 datetime 模块根据 TimeUUIDType 从 Cassandra 获取列范围

标签 python nosql cassandra pycassa

我已经设置了一张 table ,如下所示:

{“字符串”:{uuid1:“字符串”,uuid1:“字符串”},“字符串”:{uuid:“字符串”}}

或者...

Row_validation_class = UTF8Type
Default_validation_class = UTF8Type
Comparator = UUID

(它基本上将网站作为行标签,并根据 datetime.datetime.now() 动态生成列,并使用 Cassandra 中的 TimeUUIDType 和字符串作为值)

我希望使用 Pycassa 根据行和列检索数据切片。然而,在其他(较小的)表上,我已经这样做了,但是通过下载整个数据集(或至少过滤到一行),然后拥有一个可以与日期时间对象进行比较的有序字典。

我希望能够使用 Pycassa multiget 或 get_indexed_slice 函数来提取某些列和行。是否存在类似的允许对日期时间进行过滤的东西。我当前的所有尝试都会导致以下错误消息:

类型错误:无法将 datetime.datetime 与 UUID 进行比较

到目前为止我能想到的最好的办法是......

def get_number_of_visitors(site, start_date, end_date=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S:%f")):
    pool = ConnectionPool('Logs', timeout = 2)
    col_fam = ColumnFamily(pool, 'sessions')
    result = col_fam.get(site)
    number_of_views = [(k,v) for k,v in col_fam.get(site).items() if get_posixtime(k) > datetime.datetime.strptime(str(start_date), "%Y-%m-%d %H:%M:%S:%f") and get_posixtime(k) < datetime.datetime.strptime(str(end_date), "%Y-%m-%d %H:%M:%S:%f")]
    total_unique_sessions = len(number_of_views)
    return total_unique_sessions

get_posixtime 定义为:

def get_posixtime(uuid1):
    assert uuid1.version == 1, ValueError('only applies to type 1')
    t = uuid1.time
    t = (t - 0x01b21dd213814000L)
    t = t / 1e7
    return datetime.datetime.fromtimestamp(t)

这似乎不起作用(没有返回我期望的数据)并且也感觉没有必要。我正在使用以下方法创建列时间戳:

时间戳 = datetime.datetime.now()

有人有什么想法吗?感觉这是 Pycassa(或另一个 python 库)支持的事情,但我不知道如何做到这一点。

附: cqlsh 描述的表架构:

CREATE COLUMNFAMILY sessions (
  KEY text PRIMARY KEY
) WITH
  comment='' AND
  comparator='TimeUUIDType' AND
  row_cache_provider='ConcurrentLinkedHashCacheProvider' AND
  key_cache_size=200000.000000 AND
  row_cache_size=0.000000 AND
  read_repair_chance=1.000000 AND
  gc_grace_seconds=864000 AND
  default_validation=text AND
  min_compaction_threshold=4 AND
  max_compaction_threshold=32 AND
  row_cache_save_period_in_seconds=0 AND
  key_cache_save_period_in_seconds=14400 AND
  replicate_on_write=True;

附注

我知道您可以在 Pycassa 中指定列范围,但我无法保证该范围的起始值和结束值将包含每行的条目,因此该列可能不存在。

最佳答案

您确实希望使用 get()multiget 的 column_startcolumn_finish 参数请求列的“切片” ()get_count()get_range() 等。对于 TimeUUIDType 比较器,pycassa 实际上接受 datetime 实例或时间戳这两个参数;它会在内部将它们转换为类似 TimeUUID 的形式,并具有匹配的时间戳组件。文档中有一部分专门介绍 working with TimeUUIDs这提供了更多详细信息。

例如,我会像这样实现你的函数:

def get_number_of_visitors(site, start_date, end_date=None):
    """
    start_date and end_date should be datetime.datetime instances or
    timestamps like those returned from time.time().
    """
    if end_date is None:
        end_date = datetime.datetime.now()
    pool = ConnectionPool('Logs', timeout = 2)
    col_fam = ColumnFamily(pool, 'sessions')
    return col_fam.get_count(site, column_start=start_date, column_finish=end_date)

您可以使用与 col_fam.get()col_fam.xget() 相同的表单来获取实际的访问者列表。

附注尽量不要为每个请求创建一个新的ConnectionPool()。如果必须,请设置较低的池大小。

关于python - 使用 Python 和 datetime 模块根据 TimeUUIDType 从 Cassandra 获取列范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18021561/

相关文章:

xquery - MarkLogic 的 "xdmp:collection-delete"是如何工作的?

c - 如何以编程方式确定 Cassandra 集群中的节点数量?

hadoop - 热点使用hive插入Cassandra

python - Asyncio - create_task 阻塞线程

python - Kivy中根据窗口大小更改小部件的大小和位置

mongodb - MongoDB 中的多对多

cassandra - 配置多个目录后,cassandra如何拆分键空间数据?

python - 遍历字典列表和按键分组

python - 对于任意前缀符号,我如何在 python 中用 k 等来格式化数字?

javascript - ElasticSearch - 按 : sorting and pagging 聚合/分组