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

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

Have you tried using Python client here?

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

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"
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.

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

HTH

1 Like

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

© 2015 Copyright Aerospike, Inc. | All rights reserved. Creators of the Aerospike Database.