2

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 = 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");
  } 
} 
4

1 回答 1

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 序列化类型详细信息

于 2010-12-05T12:35:44.383 回答