How can i list Cassandra cql result? Do i have to parse JSON?
如何从 Cassandra 解析我的 json 样式结果?
我正在使用 cassandra-cql gem for rails,我想解析查询结果以列出所有消息。我的查询看起来像:
1 2 3 | def self.get_messages uid @@db.execute("SELECT * FROM messages WHERE uid=?", uid) end |
如何列出所有消息?我目前的看法是:
1 2 3 4 | <% json_dec = ActiveSupport::JSON.decode(u.row.to_json) %> <% json_dec.each do |f| %> <%= f[1] %><br/> <%end%> |
它返回:
1 2 | 1 [{"name"=>"uid","value"=>"1","timestamp"=>-1}, {"name"=>"1326751801","value"=>"test content","timestamp"=>1326751801970000}, {"name"=>"1326754147","value"=>"some content to test","timestamp"=>1326754147612000}] |
有没有更好的查询方法?其他人如何解决这个问题?
编码成json再解码没有多大意义。相反,您可以在行对象上调用
1 2 3 | <% row.to_hash.each do |key, value| %> <p>key: <%= key %>, value: <%= value %></p> <%end%> |
看起来您还需要更改 get_message 的实现以调用
1 2 3 | def self.get_messages uid @@db.execute("SELECT * FROM messages WHERE uid=?", uid).fetch end |