代码之家  ›  专栏  ›  技术社区  ›  Henrik P. Hessel

在python中为cassandra生成uuid

  •  10
  • Henrik P. Hessel  · 技术社区  · 15 年前

    嗯, 我在用

    cf.insert(uuid.uuid1().bytes_le, {'column1': 'val1'}) (皮卡萨)

    为Cassandra创建TimeUuid,但获取错误

    InvalidRequestException: 
    InvalidRequestException(why='UUIDs must be exactly 16 bytes')
    

    它不适用于

    uuid.uuid1()
    uuid.uuid1().bytes
    str(uuid.uuid1())
    

    要么。

    创建有效的TimeUuid以与compareWith=“TimeUuidType”标志一起使用的最佳方法是什么?

    谢谢,
    亨利克

    2 回复  |  直到 13 年前
        1
  •  4
  •   Carlo Pires    13 年前

    必须确保列族架构接受UUID作为键。您的代码将使用创建为(使用cassandra cli)的列族:

    create column family MyColumnFamily
      with column_type = 'Standard'
      and comparator = 'AsciiType'
      and default_validation_class = 'BytesType'
      and key_validation_class = 'TimeUUIDType';
    

    要向此CF添加值,请执行以下操作:

    import pycassa
    pool = pycassa.ConnectionPool('Keyspace1')
    cf = pycassa.ColumnFamily(pool, 'MyColumnFamily')
    cf.insert(uuid.uuid1(), {'column1': 'val1'})
    
        2
  •  9
  •   Schildmeijer    15 年前

    看起来您正在使用uuid作为行键,而不是列名。

    将_与:timeuuidtype进行比较 'attribute指定将与使用timeUuidType(即它)比较列名 tells Cassandra how to sort the columns for slicing operations

    您是否考虑过使用任何高级python客户机?例如。 Tradedgy , Lazy Boy , Telephus Pycassa

    推荐文章