How to pass string array in voldemort
I am using Voldemort to store my data. My key is a word and values are number of occurrence of the word and the URL. For example:
key :question
value: 10, www.stackoverflow.com
I am using string[]
to pass the values. But while I was trying to use client.put ("xxxx", valuePair);
, I am getting java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String.
My code looks like this
public class ClientExample {
public static void main (String [] args) {
String bootstrapUrl = "tcp://localhost:6666";
ClientConfig cc 开发者_Python百科= 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");
}
}
You should edit your store.xml
to change the setup of value serializer. It should look something like this right now:
<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>
Now, what you need to is to change the value-serializer
to:
<value-serializer>
<type>json</type>
<schema-info>["string"]</schema-info>
</value-serializer>
Note, that this wouldn't map to Java array, but to Java list. If that is what you really wan't to to this is as close to it as I know of.
However, you might want something like this:
<value-serializer>
<type>json</type>
<schema-info>{"occurences":"int32", "site":"string"}</schema-info>
</value-serializer>
Then, you could (snippet):
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"));
Hope this was helpful! You can see some documentation of this at:
http://project-voldemort.com/design.php
JSON Serialization Type Details
精彩评论