2015-11-18 43 views
4

如何從aerospike命名空間獲取所有密鑰,例如我可以使用map.keyset從地圖獲取密鑰。我通過這個鏈接Aerospike: how do I get record key?。我做了答案中所講的內容。我用writePolicy.sendKey = true編寫了代碼,但當我在scanCallback函數中顯示密鑰時,我仍然收到NULL如何獲取命名空間中的所有密鑰?

這裏是我的代碼寫一個記錄

def writeToAerospike(): Boolean = { 

val host:Host = new Host("xx.xxx.xxx.xx", 3000) 
val client = new AerospikeClient(new ClientPolicy,host) 

val policy = new WritePolicy(); 
policy.sendKey = true; 
policy.timeout = 50000; 

if(client.isConnected()){ 
    println("connection to aerospike client sucessful") 
    client.put(policy,new Key("test", "testSet", "1"),new Bin("name", "john")) 

    //verify if the record is entered correctly 
    println(client.get(policy,new Key("test", "testing", "1")).getValue("name")) 

    client.close() 
    return true 
} 
return false 
} 

我使用從這個例子的代碼掃描記錄(任何幫助,將不勝感激):http://www.aerospike.com/docs/client/java/usage/scan/scan.html

class ScanParallelTest extends ScanCallback { 

    private var recordCount: Int = _ 

    def runTest() { 
    val client = new AerospikeClient("xxx.xx.xxx.xx", 3000) 
    try { 
     val policy = new ScanPolicy() 
     policy.concurrentNodes = true 
     policy.priority = Priority.LOW 
     policy.includeBinData = true 
     client.scanAll(policy, "test", "testSet", this) 
     println("Records " + recordCount) 
    } finally { 
     client.close() 
    } 
} 

def scanCallback(key: Key, record: Record) { 
    recordCount += 1 

    println("Records " + recordCount) 
    println ("key=" + " "+key.userKey.toString() +" and "+" record: "+" "+ record.bins) 
    } 
} 

這裏是錯誤我得到:

com.aerospike.client.AerospikeException: java.lang.NullPointerException 
    at com.aerospike.client.command.ScanExecutor.scanParallel(ScanExecutor.java:66) 
    at com.aerospike.client.AerospikeClient.scanAll(AerospikeClient.java:551) 
    at aerospike.asd.ScanParallelTest.runTest(ScanParallelTest.scala:23) 
    at aerospike.asd.test$.main(test.scala:10) 
    at aerospike.asd.test.main(test.scala) 

Caused by: java.lang.NullPointerException 
    at aerospike.asd.ScanParallelTest.scanCallback(ScanParallelTest.scala:35) 
    at com.aerospike.client.command.ScanCommand.parseRecordResults(ScanCommand.java:122) 
    at com.aerospike.client.command.MultiCommand.parseResult(MultiCommand.java:58) 
    at com.aerospike.client.command.SyncCommand.execute(SyncCommand.java:56) 
    at com.aerospike.client.command.ScanExecutor$ScanThread.run(ScanExecutor.java:134) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
+3

你能交叉檢查,關鍵確實是把你的數據備份到服務器的。您的備份文件應該有密鑰。試圖孤立罪魁禍首。寫或掃描。 – sunil

+2

是的,鑰匙使它到服務器,但沒有讓它回到我身邊。 – Hammad

回答

5

我試過使用client.readPolicyDefault.sendKey = true;client.scanPolicyDefault.sendKey = true;client.writePolicyDefault.sendKey = true;和代碼開始返回原始鍵而不是NULL。

下面是helipilot50到工作代碼的鏈接:https://github.com/helipilot50/store-primary-key

它確實在我的案件有幫助。謝謝!

2

添加到法赫德的回答是:

請記住,存儲密鑰(而不僅僅是摘要)佔用空間。如果您的密鑰是100個字符的字符串,並且您的記錄是兩個8字節整數,則密鑰存儲將使用比這兩個值更多的空間。

利用這些知識爲好,而不是邪惡的:)

相關問題