将json文件插入Cassandra表

将json文件插入Cassandra表,json,ruby,cassandra,datastax-enterprise,cql3,Json,Ruby,Cassandra,Datastax Enterprise,Cql3,我目前正在使用Cassandra Ruby驱动程序将JSON文件中的数据插入数据库中的现有表中 JSON文件如下所示: [ { "id": "123", "destination": "234", "type": "equipment", "support": "type 1", "test": "test1" }, { "id": "234", "destination": "123", "type": "equipm

我目前正在使用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
我在文件中读到如下内容:

[
  {
    "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\u 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}';

即使我使用
.to_JSON
方法将每个对象转换为JSON,我仍然会得到相同的错误,即
keyspace.table
正确的键空间名称和表名称?是的,一切都是正确的,因为我可以从它们中查询。您能提供has.to_JSON的字符串结果吗?@ArthurLandim,它与文件中JSON的字符串格式相同。