我当前正在使用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格式。
使用.to_json
添加逃脱字符。这给了我错误
INSERT INTO organization_metadata JSON '{"id":9150,"destroyed":false,"name":"ABC","timestamp":1510541801000000000}';
和以下作用。
INSERT INTO organization_metadata JSON '{"id":9150,"destroyed":false,"name":"ABC","timestamp":1510541801000000000}';