Ignore:
Timestamp:
May 12, 2011, 8:20:33 AM (14 years ago)
Author:
rick
Message:

Example for bulk importing data.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/django_gheat/gheat/management/commands/import_droidstumbler.py

    r9180 r9181  
    1111from django.core.management import setup_environ
    1212from django.core.management.base import BaseCommand, CommandError
     13from django.db import connection, transaction
    1314from django.db.utils import IntegrityError
    1415from optparse import OptionParser, make_option
     
    2122import sys
    2223
    23 def user_feedback(count, dot_step, summary_report):
     24def user_feedback(output, count, dot_step, summary_report):
    2425  if (count % summary_report) == 0:
    25     sys.stdout.write(str(count))
     26    output.write(str(count))
    2627  elif (count % dot_step) == 0:
    27     sys.stdout.write(".")
    28   sys.stdout.flush()
     28    output.write(".")
     29  output.flush()
    2930  return count + 1
    3031
    3132
    32 def import_droidstumbler(location, meetrondje, gebruiker, email, datum,show_progres=False):
     33def import_droidstumbler(location, meetrondje, gebruiker, email, datum,show_progres=False,output=sys.stdout,bulk_import=True):
    3334  """ Import all points, return tuple with summary"""
    3435  g, created = Gebruiker.objects.get_or_create(naam=gebruiker , email=email)
     
    4142  meting_pool = {}
    4243
    43   # Process file
     44  if show_progres: output.write('#INFO: Pre-Processing file: ')
    4445  if location.endswith('.gz'):
    4546    fh = gzip.open(location,'rb')
     
    4748    fh = open(location,'rb')
    4849  csvfile = csv.reader(fh, delimiter=',')
     50  count = 0
    4951  for row in csvfile:
     52    if show_progres: count = user_feedback(output, count, 100, 1000)
    5053    try:
    5154      epoch, msg_type, lat, lon, accuracy, ssid, bssid, level, frequency, capabilities = row
     
    6568      else:
    6669        meting_pool[key] = signaal
     70  if show_progres: output.write("%s\n" % count)
    6771
    6872
    69   # Import the data into the database
     73  if show_progres: output.write('#INFO: Importing the data into the database: ')
    7074  meting_count = 0
     75  cursor = connection.cursor()
     76  sql_values = []
    7177  for (ap,lat,lon),signal in meting_pool.iteritems():
    7278    try:
    73       m = Meting.objects.create(meetrondje=mr, accespoint=ap, latitude=lat, longitude=lon, signaal=signaal)
     79      if bulk_import:
     80        value_str = str((int(mr.id),int(ap.id),float(lat),float(lon),int(signaal)))
     81        sql_values.append(value_str)
     82      else:
     83        m = Meting.objects.create(meetrondje=mr, accespoint=ap, latitude=lat, longitude=lon, signaal=signaal)
    7484    except IntegrityError, e:
    7585      logging.error("Unable to import - %s" %  e)
     
    7787
    7888    # Give some feedback to the user
    79     if show_progres:
    80       meting_count = user_feedback(meting_count, 100, 1000)
     89    if show_progres: meting_count = user_feedback(output, meting_count, 100, 1000)
    8190
    82   if show_progres:
    83     sys.stdout.write("%s\n" % meting_count)
     91  # Make sure to include closing newline
     92  if show_progres: output.write("%s\n" % meting_count)
     93  if bulk_import:
     94    sql = "INSERT INTO gheat_meting (`meetrondje_id`, `accespoint_id`, `lat`, `lng`, `signaal`) VALUES %s" % ','.join(sql_values)
     95    cursor.execute(sql)
     96    transaction.commit_unless_managed()
    8497
    8598  return (len(ap_cache), new_ap_count, meting_count, len(meting_pool) - meting_count)
Note: See TracChangeset for help on using the changeset viewer.