2010-11-23 62 views
2

嗨,我正在使用Voldemort來存儲我的數據。我的關鍵字是一個單詞,值是單詞和URL的出現次數。例如:如何在voldemort中傳遞sting數組

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

您應該編輯您的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序列類型詳細信息