Post beanstalk queue data to Mssql using python script

322 Views Asked by At

Guys I'm currently developing an offline ALPR solution.

So far I've used OpenAlpr software running on Ubuntu. By using a python script I found on StackOverlFlow I'm able to read the beanstalk queue data (plate number & meta data) of the ALPR but I need to send this data from the beanstalk queue to a mssql database. Does anyone know how to export beanstalk queue data or JSON data to the database? The code below is for local-host, how do i modify it to automatically post data to the mssql database? The data in the beanstalk queue is in JSON format [key=value].

The read & write csv was my addition to see if it can save the json data as csv on localdisk

import beanstalkc
import json
from pprint import pprint

beanstalk = beanstalkc.Connection(host='localhost', port=11300)
TUBE_NAME='alprd'
text_file = open('output.csv', 'w')

# For diagnostics, print out a list of all the tubes available in Beanstalk.
print beanstalk.tubes()

# For diagnostics, print the number of items on the current alprd queue.
try:
    pprint(beanstalk.stats_tube(TUBE_NAME))
except beanstalkc.CommandFailed:
    print "Tube doesn't exist"

# Watch the "alprd" tube; this is where the plate data is.
beanstalk.watch(TUBE_NAME)


while True:

    # Wait for a second to get a job. If there is a job, process it and delete it from the queue.
    # If not, return to sleep.
    job = beanstalk.reserve(timeout=5000)
    if job is None:
        print "No plates yet"
    else:
        plates_info = json.loads(job.body)
    # Do something with this data (e.g., match a list, open a gate, etc.).
    # if 'data_type' not in plates_info:
        # print "This shouldn't be here... all OpenALPR data should have a data_type"
    # if plates_info['data_type'] == 'alpr_results':
        # print "Found an individual plate result!"
    if plates_info['data_type'] == 'alpr_group':
        print "Found a group result!"
        print '\tBest plate: {} ({:.2f}% confidence)'.format(
            plates_info['candidates'][0]['plate'], 
            plates_info['candidates'][0]['confidence'])
        make_model = plates_info['vehicle']['make_model'][0]['name']
        print '\tVehicle information: {} {} {}'.format(
            plates_info['vehicle']['year'][0]['name'],
            plates_info['vehicle']['color'][0]['name'],
            ' '.join([word.capitalize() for word in make_model.split('_')]))
    elif plates_info['data_type'] == 'heartbeat':
        print "Received a heartbeat"
        text_file.write('Best plate')
    # Delete the job from the queue when it is processed.

job.delete()
text_file.close()
1

There are 1 best solutions below

0
user8222982 On

AFAIK there is no way to directly export data from beanstalkd.

  • What you have makes sense, that is streaming data out of a tube into a flat file or performing a insert into the DB directly
  • Given the IOPS beanstalkd can be produced, it might still be a reasonable solution (depends on what performance you are expecting)

Try asking https://groups.google.com/forum/#!forum/beanstalk-talk as well