# How to get all the keys from aerospike?

**URL:** <https://discuss.aerospike.com/t/how-to-get-all-the-keys-from-aerospike/2142>\
**Category:** Java Client\
**Tags:** key, primarykey\
**Created:** [November 18, 2015, 1:32pm UTC](https://discuss.aerospike.com/t/how-to-get-all-the-keys-from-aerospike/2142 "2015-11-18T13:32:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hammad](https://avatars.discourse-cdn.com/v4/letter/h/f08c70/32.png) [@Hammad](https://discuss.aerospike.com/u/Hammad)\
**Post date:** [November 18, 2015, 1:32pm UTC](https://discuss.aerospike.com/t/how-to-get-all-the-keys-from-aerospike/2142/1 "2015-11-18T13:32:44Z")

</div>

Sir,

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 [GitHub - helipilot50/store-primary-key](https://github.com/helipilot50/store-primary-key) .I tried writing a similar code to this one.I wrote the code with `client.xpolicy=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 appriciated):

```
def writeToAerospike():Boolean = {

val host:Host = new Host("xx.xxx.xxx.xx", 3000)
val client = new AerospikeClient(new ClientPolicy,host)
client.writePolicyDefault.sendKey = true;
client.readPolicyDefault.sendKey = true;
client.scanPolicyDefault.sendKey = true;
val policy = new WritePolicy();
policy.sendKey = true;
policy.timeout = 500;

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](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)

```

I am using `Aerospike 3.1.7 library`. If i only write key.toString() instead of key.userKey.toString() i get an output like this: `key= test:testSet:null:[B@7a7683b1 and record: {name=john}`

---

<div class="post-metadata">

**Author:** ![Hammad](https://avatars.discourse-cdn.com/v4/letter/h/f08c70/32.png) [@Hammad](https://discuss.aerospike.com/u/Hammad)\
**Post date:** [November 18, 2015, 2:03pm UTC](https://discuss.aerospike.com/t/how-to-get-all-the-keys-from-aerospike/2142/2 "2015-11-18T14:03:17Z")

</div>

@helipilot50 Sir,

I am sorry to tag you in the post like this but I read your answers on similar question. I executed your code using `key.userKey.toString()` it works fine. But when i tried my scanall function on the same namespace and same set. It gave me the same error as before. without the ‘userKey’ part it gives me record like this: `Records 493 key= test:heliSet:null:[B@4ab63056 and record: {age=26}`

---

<div class="post-metadata">

**Author:** ![naoum](https://avatars.discourse-cdn.com/v4/letter/n/4da419/32.png) [@naoum](https://discuss.aerospike.com/u/naoum)\
**Post date:** [November 18, 2015, 3:26pm UTC](https://discuss.aerospike.com/t/how-to-get-all-the-keys-from-aerospike/2142/3 "2015-11-18T15:26:03Z")

</div>

Did you set the sendKey property when you pushed the data into aerospike? If you didn’t, it never saved it. The key should look something like: namespace1:setname1:username1:hashdigest so it looks like your key is null

---

<div class="post-metadata">

**Author:** ![Hammad](https://avatars.discourse-cdn.com/v4/letter/h/f08c70/32.png) [@Hammad](https://discuss.aerospike.com/u/Hammad)\
**Post date:** [November 19, 2015, 8:23am UTC](https://discuss.aerospike.com/t/how-to-get-all-the-keys-from-aerospike/2142/4 "2015-11-19T08:23:28Z")

</div>

> [@Hammad](#):
>
> val policy = new WritePolicy(); policy.sendKey = true;

Sir i did set the sendKey = true. But i am still getting the error.

---

<div class="post-metadata">

**Author:** ![Hammad](https://avatars.discourse-cdn.com/v4/letter/h/f08c70/32.png) [@Hammad](https://discuss.aerospike.com/u/Hammad)\
**Post date:** [November 19, 2015, 9:17am UTC](https://discuss.aerospike.com/t/how-to-get-all-the-keys-from-aerospike/2142/5 "2015-11-19T09:17:44Z")

</div>

I tried using

```
   client.writePolicyDefault.sendKey = true;
   client.readPolicyDefault.sendKey = true;
   client.scanPolicyDefault.sendKey = true;

```

The code started working for me. Thank you 😄

---

<div class="post-metadata">

**Author:** ![myth](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@myth](https://discuss.aerospike.com/u/myth)\
**Post date:** [May 23, 2017, 6:42am UTC](https://discuss.aerospike.com/t/how-to-get-all-the-keys-from-aerospike/2142/6 "2017-05-23T06:42:47Z")

</div>

this same code available in c language… if anyone have plz share with me
