# Step by Step : Load a CSV File, then query the CSV File

**URL:** <https://discuss.aerospike.com/t/step-by-step-load-a-csv-file-then-query-the-csv-file/6877>\
**Category:** CSV Loader\
**Tags:** query\
**Created:** [December 4, 2019, 6:28am UTC](https://discuss.aerospike.com/t/step-by-step-load-a-csv-file-then-query-the-csv-file/6877 "2019-12-04T06:28:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benjamin\_Cabalona\_Jr](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/benjamin_cabalona_jr/32/1113_2.png) [@Benjamin\_Cabalona\_Jr](https://discuss.aerospike.com/u/Benjamin_Cabalona_Jr)\
**Post date:** [December 4, 2019, 6:28am UTC](https://discuss.aerospike.com/t/step-by-step-load-a-csv-file-then-query-the-csv-file/6877/1 "2019-12-04T06:28:54Z")

</div>

Can someone show me a step by step example in loading, then querying back a CSV file? The Documentation lacks some practical example in my opinion. I’ve tried the aerospike-loader, followed the examples, but it does not seem to work.

Thanks

---

<div class="post-metadata">

**Author:** ![Mich\_Talebzadeh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/mich_talebzadeh/32/1141_2.png) [@Mich\_Talebzadeh](https://discuss.aerospike.com/u/Mich_Talebzadeh)\
**Post date:** [December 5, 2019, 9:03am UTC](https://discuss.aerospike.com/t/step-by-step-load-a-csv-file-then-query-the-csv-file/6877/2 "2019-12-05T09:03:27Z")

</div>

Have you tried using Python client here?

```auto
  def write_aerospike_set(self):
    # Check aerospike is accessible
    try:
      c.client
    except ex.ClientError as e:
      print("Error: {0} [{1}]".format(e.msg, e.code))
      sys.exit(1)
    else:
      print("Connection to aerospike successful")
      rec = {}
      # read from csv file
      csv_file = open(v.dump_dir+v.filename, mode='r')
      reader = csv.reader(csv_file, delimiter=',')
      rownum = 0
      for row in reader:
        if rownum == 0:
          header = row
        else:
          column = 0
          for col in row:
            # print (rownum,header[colnum],col)
            rec[header[column]] = col
            column += 1
        rownum += 1
        #print(rownum, rec)
        if rec:
          c.client.put((v.namespace, v.dbSet, str(rownum)), rec)
        rec = {}
      print("writing to aerospike set complete")
      csv_file.close()
      c.client.close()
      sys.exit(0)

```

```auto
  def read_aerospike_set(self):
    # Check aerospike is accessible
    try:
      c.client
    except ex.ClientError as e:
      print("Error: {0} [{1}]".format(e.msg, e.code))
      sys.exit(1)
    else:
      print("Connection successful")
      keys = []
      for k in range(1,10000):
         key = (v.namespace, v.dbSet, str(k))
         keys.append(key)

      records = c.client.get_many(keys)
      pprint.PrettyPrinter(depth=4).pprint (records)
      print("\nget everyting for one record with pk = '9'")
      (key, meta, bins)= c.client.get((v.namespace, v.dbSet, '9'))
      print (key)
      print (meta)
      print (bins)
      c.client.close()
      sys.exit(0)

```

And you can create these two static modules

```auto
cat variables.py
#! /usr/bin/env python
dump_dir = "/backup/hduser/"
filename = "DUMMY.csv"

# aerospike variables
dbHost = "dpcluster-m"
dbPort = 3000
dbConnection = "mich"
namespace = "test"
dbPassword = "xxxx"
dbSet = "oracletoaerospike2"
dbKey = "ID"

```

```auto
cat configs.py
#! /usr/bin/env python
import variables as v
import aerospike
from aerospike import exception as ex
# aerospike
write_policy = {'key': aerospike.POLICY_KEY_SEND}
policies = {'write': write_policy, 'total_timeout': 1000}
config = {
  'hosts': [(v.dbHost, v.dbPort)],
  'policies': policies
}
client = aerospike.client(config).connect(v.dbConnection, v.dbPassword)

```

Don’t forget to add aerospike python client to your Python environment.

```auto
from __future__ import print_function
import variables as v
import configs as c
import sys
import pprint
import csv

```

HTH

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/system/32/2274_2.png) [@system](https://discuss.aerospike.com/u/system)\
**Post date:** [December 11, 2019, 9:17am UTC](https://discuss.aerospike.com/t/step-by-step-load-a-csv-file-then-query-the-csv-file/6877/3 "2019-12-11T09:17:56Z")

</div>

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.
