[英]Ruby: how to insert Hash value into Cassandra map
Am attempting to store values provided as int, time, hash
into Cassandra using Datastax driver. 我试图使用Datastax驱动程序将以int, time, hash
提供的值存储到Cassandra中。
Hash appears as { "Q17.1_4"=>"val", "Q17.2"=>"other", ...
} 哈希显示为{ "Q17.1_4"=>"val", "Q17.2"=>"other", ...
}
Have defined table as: 将表定义为:
I can get the PK inserted ok but am having trouble coercing the hash values into something that Cassandra can cope with. 我可以正常插入PK,但是在将哈希值强制转换为Cassandra可以应付的值时遇到了麻烦。
Have created a prepared statement and using it while (trying to) looping through values: 创建了一个准备好的语句,并在(尝试)循环遍历值时使用它:
stmt = session.prepare( "insert into forms( id, stamp, questions ) values ( ?,?,? )" )
...
json_val.each{ |key,val|
result = session.execute( stmt, val[ 'ID' ].to_i, Time.parse( val[ 'tDate' ]).to_i, val )
}
If I insert 'val' as a hash I get this error: 如果我将“ val”作为哈希插入,则会出现此错误:
/lib/cassandra/statements/prepared.rb:53:in `bind': expecting exactly 3 bind parameters, 2 given (ArgumentError) /lib/cassandra/statements/prepared.rb:53:在`bind'中:恰好期望3个绑定参数,给定2个(ArgumentError)
This says (to me) that there isn't a method to convert the hash to what Cassandra wants. (对我来说)这表明没有一种将哈希转换为Cassandra想要的方法。
If I insert the val as a string (using hash.to_s
) I get this error: 如果我将val作为字符串插入(使用hash.to_s
), hash.to_s
此错误:
/lib/cassandra/protocol/type_converter.rb:331:in
varchar_to_bytes': undefined method
encode' for 1:Fixnum (NoMethodError) /lib/cassandra/protocol/type_converter.rb:331:invarchar_to_bytes': undefined method
encoding'for 1:Fixnum(NoMethodError)
... same goes for converting it to a json string, changing double quotes to single and so on. ...将其转换为json字符串,将双引号更改为单引号等等同样适用。
I can insert the values using the cqlsh (command line) ( EG insert into forms (id, stamp, questions) values ( 123, 12345678, { 'one':'two','three':'four'});
) 我可以使用cqlsh(命令行)插入值(将EG insert into forms (id, stamp, questions) values ( 123, 12345678, { 'one':'two','three':'four'});
)
So the question is - how do I get this Ruby hash into a format that the Cassandra driver will accept? 所以问题是-如何将这个Ruby哈希转换为Cassandra驱动程序将接受的格式?
Using: 使用:
EDIT: 编辑:
JSON value of 'questions' hash is { "Q17.1_4":"val", "Q17.2":"other", ...} “ questions”哈希的JSON值为{“ Q17.1_4”:“ val”,“ Q17.2”:“ other”,...}
Also - I can insert the first two columns just fine. 另外-我可以插入前两列。 Omitting the third value from the insert statement works so I know those two values aren't the culprit here. 从insert语句中省略第三个值是可行的,因此我知道这两个值不是这里的罪魁祸首。
Found two problems: 发现了两个问题:
Last argument will be treated as options if it is a Hash. 如果最后一个参数是哈希,则将其视为选项。 Therefore, make sure to pass empty options when executing a statement with the last parameter required to be a map datatype. 因此,确保在执行带有最后一个参数的语句时必须传递空选项,而最后一个参数必须是map数据类型。
type_converter.rb:331:in
varchar_to_bytes': undefined method
encode' for 1:Fixnum (NoMethodError) type_converter.rb:331:在varchar_to_bytes': undefined method
编码为1:Fixnum(NoMethodError)
This lead me to believe that there was an invalid value in there somewhere. 这使我相信某个地方存在无效值。 To fix this I created a temp hash and checked every value to make sure it was a string before adding it to this temp hash. 为了解决这个问题,我创建了一个临时哈希,并在将其添加到该临时哈希之前检查了每个值以确保它是字符串。 Then using this temp value I was able to add to the DB using the stored procedure. 然后使用这个临时值,我可以使用存储过程将其添加到数据库中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.