jsonファイルをCassandraテーブルに挿入する
概要
現在、Cassandra-Ruby ドライバーを使用して、JSON ファイルからデータベース内の既存のテーブルにデータを挿入しています。
JSON ファイルは次のようになります。
[
{
"id": "123",
"destination": "234",
"type": "equipment",
"support": "type 1",
"test": "test1"
},
{
"id": "234",
"destination": "123",
"type": "equipment",
"support": "type 1",
"test": "test1"
}
]
私は次のようにファイルを読んでいます:
file = File.read('itemType.json')
data_hash = JSON.parse(file) #return an array of hashes
配列を反復処理して各ハッシュを取得します 各ハッシュをテーブルに挿入します
data_hash.each do |has|
#check the type of each object
#puts has.class #return hash
insert_statement = session.prepare('INSERT INTO keyspace.table JSON ?')
session.execute(insert_statement, [has]) #error occurs here
end
このコードを実行すると、次のエラー メッセージが表示されます
in `assert_instance_of': options must be a Hash
テーブルに挿入されている各オブジェクトがハッシュであることを確認しましたが、なぜこの問題が発生するのかわかりません。
解決策
JSON を挿入していると言っていますが、そうではなく、オブジェクトを挿入しようとしています。ドキュメントのこの例を参照してください。
INSERT INTO cycling.cyclist_category JSON '{
"category" : "Sprint",
"points" : 700,
"id" : "829aa84a-4bba-411f-a4fb-38167a987cda"
}';
そのようにする場合は、json形式を指定する必要があります。