2

ヴォルデモートを使用してデータを保存しています。私のキーは単語で、値は単語と URL の出現回数です。例えば:

key :question
value: 10, www.stackoverflow.com

string[]値を渡すために使用しています。しかし、使用しようとしているときに、 java.lang.ClassCastException: [Ljava.lang.String;client.put ("xxxx", valuePair);を取得して います。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");
  } 
} 
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 に答える