クエリー結果をJSON形式に設定する
SELECTコマンドを使用して、JSONデータを返します。
CQL SELECTキーワードは、JSON形式でテーブルからデータを取得する際に使用できます。詳細については、「Cassandra 2.2の新機能:JSONサポート」を参照してください。
すべての結果をJSON形式で取得する
この結果を取得するには、JSONキーワードをSELECTコマンドとデータの指定の間に挿入します。例を次に示します。
cqlsh:cycling> select json name, checkin_id, timestamp from checkin;
[json]<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE map PUBLIC "-//OASIS//DTD DITA Map//EN" "map.dtd">
<map>
<title>meta</title>
<topicref href="dse-wip.ditamap" format="ditamap"/>
<topicref href="install.ditamap" format="ditamap"/>
<topicref href="security.ditamap" format="ditamap"/>
<topicref href="cql.ditamap" format="ditamap"/>
<topicref href="opscuserguide.ditamap" format="ditamap"/>
<topicref href="studio.ditamap" format="ditamap"/>
<topicref href="variables.ditamap" format="ditamap"/>
<topicref href="universalCommons.ditamap" format="ditamap"/>
</map>
------------------------------------------------------------------------------------------------------------------
{"name": "BRAND", "checkin_id": "50554d6e-29bb-11e5-b345-feff8194dc9f", "timestamp": "2016-08-28 21:45:10.406Z"}
{"name": "VOSS", "checkin_id": "50554d6e-29bb-11e5-b345-feff819cdc9f", "timestamp": "2016-08-28 21:44:04.113Z"}
(2 rows)
選択したカラムをJSON形式で取得する
選択したカラムに対してJSON形式を指定するには、
toJson()
にその名前を含めます。例を次に示します。cqlsh:cycling> select name, checkin_id, toJson(timestamp) from checkin;
name | checkin_id | system.tojson(timestamp)
-------+--------------------------------------+----------------------------
BRAND | 50554d6e-29bb-11e5-b345-feff8194dc9f | "2016-08-28 21:45:10.406Z"
VOSS | 50554d6e-29bb-11e5-b345-feff819cdc9f | "2016-08-28 21:44:04.113Z"
注: DataStax Enterpriseでは、JSON形式の
timestamp
と完全なタイム・ゾーン情報が返されます。