我正在使用Voldemort来存储我的数据。我的关键是一个单词,值是单词和URL的出现次数。例如:
key :question
value: 10, www.stackoverflow.com
我正在使用string[]
传递值。但是当我尝试使用client.put ("xxxx", valuePair);
时,我得到 java.lang.ClassCastException:[Ljava.lang.String;无法转换为java.lang.String。
我的代码看起来像这样
public class ClientExample {
public static void main (String [] args) {
String bootstrapUrl = "tcp://localhost:6666";
ClientConfig cc = new ClientConfig ();
cc.setBootstrapUrls (bootstrapUrl);
String[] valuePair = new String[2];
int val = 1;
String value = new Integer(val).toString();
valuePair[0]=value;
valuePair[1] = "www.cnn.com";
System.out.println("Executed one");
StoreClientFactory factory = new SocketStoreClientFactory (cc);
StoreClient <String, String[]> client = factory.getStoreClient ("test");
System.out.println("Executed two");
client.put ("xxxx", valuePair);
System.out.println("Executed three");
String[] ans = client.getValue("key");
System.out.println("Executed four");
System.out.println ("value " +ans[0] +ans[1]);
System.out.println("Executed 5");
}
}
答案 0 :(得分:0)
您应该编辑store.xml
以更改值序列化程序的设置。现在应该看起来像这样:
<stores>
<store>
<name>test</name>
<persistence>bdb</persistence>
<routing>client</routing>
<replication-factor>1</replication-factor>
<required-reads>1</required-reads>
<required-writes>1</required-writes>
<key-serializer>
<type>string</type>
</key-serializer>
<value-serializer>
<type>string</type>
</value-serializer>
</store>
</stores>
现在,您需要将value-serializer
更改为:
<value-serializer>
<type>json</type>
<schema-info>["string"]</schema-info>
</value-serializer>
请注意,这不会映射到Java数组,而是映射到Java列表。如果这就是你真的那么就是我所知道的那么接近它。
但是,可能想要这样的内容:
<value-serializer>
<type>json</type>
<schema-info>{"occurences":"int32", "site":"string"}</schema-info>
</value-serializer>
然后,你可以(摘录):
Map<String, Object> pair = new HashMap<String,Object>();
pair.put("occurences", 10);
pair.put("site", "www.stackoverflow.com");
client.put("question",pair);
System.out.println(client.get("question"));
希望这有用! 您可以在以下位置查看相关文档:
http://project-voldemort.com/design.php
JSON序列化类型详细信息