4

How can I get all the keys from aerospike namespace like I can get keys from a map using map.keyset. I went through this link Aerospike: how do I get record key?. I did what was being told in the answers. I wrote the code with writePolicy.sendKey = true but i am still getting NULL when I display the key in scanCallback funtion.

Here is my code to write a record (any help would be appreciated)

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 
}

I am using the code from this example to scan the records: 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)
  }
}

Here is the error I am getting:

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)
Community
  • 1
  • 1
Hammad
  • 177
  • 1
  • 10
  • 3
    Can you crosscheck that the key indeed made to the server by taking backup of your data. Your backup file should have the key. Just trying to isolate the culprit. Write or scan. – sunil Nov 19 '15 at 04:47
  • 2
    yes, the keys making it to the server but weren't making it back to me. – Hammad Nov 19 '15 at 08:58

2 Answers2

5

I tried using client.readPolicyDefault.sendKey = true; and client.scanPolicyDefault.sendKey = true; with client.writePolicyDefault.sendKey = true; and the code started to return the original keys instead of NULL.

Here is a link to a working code by helipilot50: https://github.com/helipilot50/store-primary-key

It really helped in my case. Thank you!

Fahad Siddiqui
  • 1,829
  • 1
  • 19
  • 41
2

Adding to Fahad's answer:

Keep in mind that storing the key (rather than just the Digest) takes up space. If you key is a 100 character string and your record is two 8 byte integers, the key storage will use more space than the the two values.

Use this knowledge for good and not for evil :)

Helipilot50
  • 227
  • 1
  • 1