将Python字典序列化为Cassandra 1.2列 [英] Serialize a Python dict into a Cassandra 1.2 column

查看:68
本文介绍了将Python字典序列化为Cassandra 1.2列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题



我正在尝试将序列化对象(使用cPickle)保存到Cassandra 1.2列中, python cql库。我已经尝试将列定义为文本(utf8字符串)和blob,在两种情况下,我都收到相同的错误:



该对象是Python字典:

  obj = {'id':'sometextid',
'time_created':05/12/2013,# < ---- datetime
'其他字符串属性':'一些其他值'
}

错误是这样的:

  raise cql.ProgrammingError( Bad请求:%s%ire.why)
cql.apivalues.ProgrammingError:错误请求:第31:36行字符'no'


然后查看执行的CQL语句,可以在腌制对象后看到一些'\'字符,例如:



腌制对象的一部分

  cdatetime 
datetime
p4
(S'\x07\xdd\x03\x1c\x000\x13\x05\xd0<'
tRp5

我的问题



使用cql库序列化python字典(包括日期时间)以将其保存到cassandra 1.2的常用方法是什么?有更好或更直接的方法吗?



预先感谢!

解决方案

此问题的完整解决方案是以这种方式将列定义为blob并包含十六进制编码(如cassandra文档中对blob类型的定义):

  obj_to_store = cPickle.dumps(input_obj).encode( hex)

这样,您可以序列化常规python字典。使用常规,我的意思是它可以包含python dict可以包含的任何内容,包括日期时间或您想要的任何内容,并且可以正确地序列化并存储在cassandra中。



也许有更好的选择解决方案,但到目前为止,这是我发现的唯一可与任意python dict一起实际使用的解决方案。



希望它对某人有帮助!


The problem

I'm trying to save a serialized object (using cPickle) into a Cassandra 1.2 column, I'm using the python cql library. I've already tried defining the column as text (utf8 string) and blob, in both cases I'm receiving the same error:

The object is a Python dict:

obj = {'id':'sometextid',
       'time_created':05/12/2013, #<---- datetime
       'some other string property': 'some other value'
}

The error is this:

raise cql.ProgrammingError("Bad Request: %s" % ire.why)
cql.apivalues.ProgrammingError: Bad Request: line 31:36 no viable alternative at character '\'

And looking at the executed CQL statement I can see some '\' characters after pickling the object, for instance:

Part of the pickled object

cdatetime
datetime
p4
(S'\x07\xdd\x03\x1c\x000\x13\x05\xd0<'
tRp5

My questions

What is the usual way of serializing a python dict (including datetimes) to save it into cassandra 1.2 using the cql library? Is there a better or more straightforward way of doing this?

Thanks in advance!

解决方案

The complete solution to this problem is to define the column as a blob and include an encode to hex (as defined in the cassandra docs for the blob type) in this way:

obj_to_store = cPickle.dumps(input_obj).encode("hex")

In this way you can serialize a regular python dict. With regular I mean it can contain anything a python dict can, including datetimes or whatever you want and it will be properly serialized and stored in cassandra.

Maybe there is a better solution out there but so far this is the only one i've found that actually works with an arbitrary python dict.

Hope it helps somebody!

这篇关于将Python字典序列化为Cassandra 1.2列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆