Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java aerospike JUNIT记录构造函数不工作_Java_Junit_Classcastexception_Aerospike - Fatal编程技术网

Java aerospike JUNIT记录构造函数不工作

Java aerospike JUNIT记录构造函数不工作,java,junit,classcastexception,aerospike,Java,Junit,Classcastexception,Aerospike,我正在为我的测试用例编写aeropsike junit测试用例。对于存储aerospike垃圾箱,我使用ConcurrentHashMap // InMemory Map for storing AeroSpike Related Data... ConcurrentMap<String, Bin[]> aerospike_keyBins = new ConcurrentHashMap<String, Bin[]>(); // InMemory Map for stor

我正在为我的测试用例编写aeropsike junit测试用例。对于存储aerospike垃圾箱,我使用ConcurrentHashMap

// InMemory Map for storing AeroSpike Related Data...
ConcurrentMap<String, Bin[]> aerospike_keyBins = new ConcurrentHashMap<String, Bin[]>();

// InMemory Map for storing AeroSpike Related Data...
ConcurrentMap<String, Integer> aerospike_keyGen = new ConcurrentHashMap<String, Integer>();
在该函数中调用
newrecord
时,我收到以下错误消息

java.lang.ClassCastException: com.aerospike.client.Value$StringValue cannot be cast to java.lang.String
    at com.aerospike.client.Record.getString(Record.java:66)
在调试这个问题时,我发现在Aerospike的
Record.class
中, 下面的函数给出错误

/**
 * Get bin value as String.
 */
public String getString(String name) {
    return (String) getValue(name);
}
getValue
正在返回正确的值,但
(字符串)getValue(名称)
抛出错误

/**
 * Get bin value as String.
 */
public String getString(String name) {
    return (String) getValue(name);
}

有关于我为什么会出现此错误的线索吗?

您的BinStoreRecord方法看起来不错

/**
 * Get bin value as String.
 */
public String getString(String name) {
    return (String) getValue(name);
}
在Bin中,binValue是一个StringValue对象,所以不能直接将其转换为String。 试一试


实际上,问题在于将值放入本地hashmap时。 我在本地hashmap中推送StringValues类型的值来解决这个问题

下面是新的BinStoreRecord函数

/*
 * Convert Bin Set to Record Object.
 */
private Record binsToRecord(String stringKey, Bin... bins) {
    Map<String, Object> mapOfBins = new HashMap<String, Object>();
    for (Bin bin : bins) {
        if (bin.value instanceof Value.StringValue)
            mapOfBins.put(bin.name, bin.value.toString());
        else if (bin.value instanceof Value.LongValue)
            mapOfBins.put(bin.name, bin.value.toLong());
        else if (bin.value instanceof Value.DoubleValue)
            mapOfBins.put(bin.name, bin.value.toLong());

    }
    return (new Record(mapOfBins, aerospike_keyGen.get(stringKey), 1));
}
/*
*将仓位集转换为记录对象。
*/
私人记录箱存储记录(字符串串键、箱…箱){
Map mapOfBins=新HashMap();
用于(垃圾箱:垃圾箱){
if(bin.value实例of value.StringValue)
mapOfBins.put(bin.name、bin.value.toString());
else if(bin.value instanceof value.LongValue)
mapOfBins.put(bin.name,bin.value.toLong());
else if(bin.value instanceof value.DoubleValue)
mapOfBins.put(bin.name,bin.value.toLong());
}
返回(新记录(mapOfBins,aerospike_keyGen.get(stringKey),1));
}

谢谢苏丹舒。我得到了代码中的实际问题。
/*
 * Convert Bin Set to Record Object.
 */
private Record binsToRecord(String stringKey, Bin... bins) {
    Map<String, Object> mapOfBins = new HashMap<String, Object>();
    for (Bin bin : bins) {
        if (bin.value instanceof Value.StringValue)
            mapOfBins.put(bin.name, bin.value.toString());
        else if (bin.value instanceof Value.LongValue)
            mapOfBins.put(bin.name, bin.value.toLong());
        else if (bin.value instanceof Value.DoubleValue)
            mapOfBins.put(bin.name, bin.value.toLong());

    }
    return (new Record(mapOfBins, aerospike_keyGen.get(stringKey), 1));
}