我使用cassaforte 1.3.0-beta9,Cassandra 1.2(DataStax)和com.datastax.cassandra / cassandra-driver-core 1.0.5(因为2.0不适用于Cassandra 1.2)。我有一个包含以下架构的表:
CREATE TABLE entities (
id text,
address_country text,
address_county text,
address_region text,
address_street_two text,
csv_uploads list<text>,
documents list<text>,
name text,
notes list<text>,
photos list<text>,
project_id text,
property_code text,
property_data text,
retrofit_completion_date text,
metering_point_ids text,
devices map<text,text>,
user_id text,
PRIMARY KEY (id)
)
这是一个存储在一行中的例子:
7b237da126d39ccf98f80d78b6145a10828970f8 |
{'b9d88015c84312bf24cef0e7f1a0baf98dbc8d8d':
'{"metadata":{"passivrole":"zone 1 temperature,zone 1 temperature"},
"readings":
[{"type":"temperature","resolution":"120","accuracy":"0.1","period":"INSTANT"},
{"type":"interpolatedTemperatureAir","resolution":100,"accuracy":"0.1","period":"INSTANT"}],
"description":"External air temperature sensor",
"entity-id":"3e0730d4672538307056a30615aaaa333107d94e"}',
'd2fe3dca47b760dd67759a56ee0ebbc2cb3395f4':
'{"metadata":{"passivrole":"zone 1 humidity"},
"readings":[{"type":"blah","resolution":"120","accuracy":"0.1","period":"INSTANT"}],
"description":" air humidity sensor",
"entity-id":"3e0730d4672538307056a30615aaaa333107d94e"}'} |
alice
当我尝试阅读devices
列时,我收到以下错误:
com.datastax.driver.core.exceptions.InvalidTypeException: Invalid serialized value for type map<text, text> (String didn't validate.)
at com.datastax.driver.core.DataType.deserialize(DataType.java:503)
我可以使用cqlsh读取设备列没有问题(如上所示)。存储在地图中的值是字符串化的json对象。
以下是示例工作流程:
(require '[clojurewerkz.cassaforte.client :as cassaclient]
' [clojurewerkz.cassaforte.query :as cassaquery]
' [clojurewerkz.cassaforte.cql :as cql])
;; SELECT on empty devices:
(binding [cassaclient/*default-session* session]
(let [entity (cql/select :entities (cassaquery/where :id "3e0730d4672538307056a30615aaaa333107d94e"))]
(prn "retrieved entity: " entity)))
;; Returns vector of results, no errors:
"retrieved entity: " [{:retrofit_completion_date nil, :notes nil, :property_code "3e49de0-12af-012e-4f3a-12313b0348f8", :devices nil, :name nil, :user_id "alice", :csv_uploads nil, :project_id "0b4f512fb7834a0878252ab144a525d424445ba2", :photos nil, :property_data nil, :metering_point_ids "[\"1\" \"2\"]", :address_street_two nil, :documents nil, :address_region nil, :address_country nil, :address_county nil, :id "3e0730d4672538307056a30615aaaa333107d94e"}]
;; UPDATE using cassaforte:
(binding [cassaclient/*default-session* session]
(cql/update :entities {:devices {"0edf9e09bce895caf0342ac6ae12511555930362" "{:metadata {\"passivrole\" \"zone 1 humidity\"}, :readings [{\"type\" \"tba\", \"resolution\" \"90\", \"accuracy\" \"0.2\", \"period\" \"INSTANT\"}], :description \"lalalala\", :entity-id \"3e0730d4672538307056a30615aaaa333107d94e\"}"}} (cassaquery/where :id "3e0730d4672538307056a30615aaaa333107d94e")))
;; update worked ok, cqlsh (select devices from entities where id = '3e0730d4672538307056a30615aaaa333107d94e';)
;; returns correct map:
;; {'0edf9e09bce895caf0342ac6ae12511555930362': '{:metadata {"passivrole" "zone 1 humidity"}, :readings [{"type" "tba", "resolution" "90", "accuracy" "0.2", "period" "INSTANT"}], :description "lalalala", :entity-id "3e0730d4672538307056a30615aaaa333107d94e"}'}
;; SELECT using cassaforte
(binding [cassaclient/*default-session* session]
(let [entity (cql/select :entities (cassaquery/where :id "3e0730d4672538307056a30615aaaa333107d94e"))]
(prn "retrieved entity: " entity)))
;; Returns this error:
;; InvalidTypeException Invalid serialized value for type map<text, text> (String didn't validate.) com.datastax.driver.core.DataType.deserialize (DataType.java:503)
我使用原始CQL和Cassaforte尝试了相同的工作流程,结果完全相同:
(require '[clojurewerkz.cassaforte.client :as cassaclient])
;; SELECT on empty devices:
(cassaclient/execute session "SELECT * FROM entities WHERE id = '3e0730d4672538307056a30615aaaa333107d94e';")
;; Returns vector of results, no errors:
[{:retrofit_completion_date nil, :notes nil, :property_code "3e49de0-12af-012e-4f3a-12313b0348f8", :devices nil, :name nil, :user_id "alice", :csv_uploads nil, :project_id "0b4f512fb7834a0878252ab144a525d424445ba2", :photos nil, :property_data nil, :metering_point_ids "[\"1\" \"2\"]", :address_street_two nil, :documents nil, :address_region nil, :address_country nil, :address_county nil, :id "3e0730d4672538307056a30615aaaa333107d94e"}]
;; UPDATE using cassaforte:
(cassaclient/execute session "UPDATE entities SET devices = {'0edf9e09bce895caf0342ac6ae12511555930362' : '{:metadata {\"passivrole\" \"zone 1 humidity\"}, :readings [{\"type\" \"tba\", \"resolution\" \"90\", \"accuracy\" \"0.2\", \"period\" \"INSTANT\"}], :description \"lalalala\", :entity-id \"3e0730d4672538307056a30615aaaa333107d94e\"}'} WHERE id = '3e0730d4672538307056a30615aaaa333107d94e';")
;; update worked ok, cqlsh (select devices from entities where id = '3e0730d4672538307056a30615aaaa333107d94e';)
;; returns correct map:
;; {'0edf9e09bce895caf0342ac6ae12511555930362': '{:metadata {"passivrole" "zone 1 humidity"}, :readings [{"type" "tba", "resolution" "90", "accuracy" "0.2", "period" "INSTANT"}], :description "lalalala", :entity-id "3e0730d4672538307056a30615aaaa333107d94e"}'}
;; SELECT using cassaforte
(cassaclient/execute session "SELECT * FROM entities WHERE id = '3e0730d4672538307056a30615aaaa333107d94e';")
;; Returns this error:
;; InvalidTypeException Invalid serialized value for type map<text, text> (String didn't validate.) com.datastax.driver.core.DataType.deserialize (DataType.java:503)
非常感谢任何关于如何在Cassandra地图集中存储json对象的帮助和/或建议。
答案 0 :(得分:1)
(Cassaforte Maintainer)
直到现在还没有看到这个问题,
这是1.0.5版DataStax驱动程序的问题。 Cassaforte目前处于2.0.0,任何版本都超过beta12,因此问题已关闭。 更多信息:https://github.com/clojurewerkz/cassaforte/issues/41
使用最新版本的Cassaforte,并尝试坚持与Cassaforte捆绑在一起的Java Driver版本,这一切都很棒。