dblookup_example.py :  » Development » SnapLogic » snaplogic » example » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » SnapLogic 
SnapLogic » snaplogic » example » dblookup_example.py
# $SnapHashLicense:
# 
# SnapLogic - Open source data services
# 
# Copyright (C) 2008 - 2009, SnapLogic, Inc.  All rights reserved.
# 
# See http://www.snaplogic.org for more information about
# the SnapLogic project. 
# 
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2. See the LEGAL file
# at the top of the source tree.
# 
# "SnapLogic" is a trademark of SnapLogic, Inc.
# 
# 
# $

# $Id: dblookup_example.py 10183 2009-12-16 23:58:55Z grisha $
"""
DBLookup example 1.

Create a DBLookup component resource which subsitutes some data in from a previous DBRead query and writes it to
a file.

"""
import sys

from snaplogic import snapi

SERVER_URL = 'http://localhost:8088'
RESOURCE_ROOT = SERVER_URL + '/examples/dblookup'

def print_resource_validate_info(error):
    """Print out useful information about resource validation errors and exit."""
    print "Resource validation failed with the following error"
    # This should be a call to some super snapi helper routine which unpacks errors nicely
    print error
    sys.exit(1)

def delete_resource(uri):
    try:
        snapi.delete_resource(uri)
    except Exception:
        pass

########################################
# Delete existing resources if exist
########################################
for uri in ['/dbconn', '/lead_reader', '/dblookup', '/writer', '/pipe']:
    delete_resource(RESOURCE_ROOT + uri)

########################################
# Create the database connection
########################################

dbconn = snapi.create_resource_object(SERVER_URL, 'snaplogic.components.Connection')
dbconn.set_property_value('ConnectString', 'MySQLdb:host=mysql,db=qa,user=qa,passwd=qa')
error = dbconn.validate()
if error is not None:
    print_resource_validate_info(error)
dbconn.save(RESOURCE_ROOT + '/dbconn')

########################################
# Create the lead reader
########################################

lead_reader = snapi.create_resource_object(SERVER_URL, 'snaplogic.components.DBRead')
lead_reader.set_resource_ref('DBConnect', RESOURCE_ROOT + '/dbconn')
# The subquery is used to prevent the DBLookup from failing for zipcodes that don't exist in the census table.
lead_reader.set_property_value('SQLStmt',
                               'SELECT last, first, zip FROM leads ' \
                               'WHERE state = "CA" AND zip IN (SELECT zip FROM census)')
lead_reader.add_record_output_view('output1',
                                   [('last',    'string', 'Last Name'),
                                    ('first',   'string', 'First Name'),
                                    ('zip',     'number', 'Zip Code')],
                                   '')

error = lead_reader.validate()
if error is not None:
    print_resource_validate_info(error)
lead_reader.save(RESOURCE_ROOT + '/lead_reader')

########################################
# Create DBLookup that finds the MoreThan50K value for a given zip code
########################################

lookup = snapi.create_resource_object(SERVER_URL, 'snaplogic.components.DBLookup')
lookup.set_resource_ref('DBConnect', RESOURCE_ROOT + '/dbconn')
lookup.set_property_value('SQLStmt',
                          'SELECT "${first} ${last}" AS fullname, morethan50k, zip ' \
                          'FROM census WHERE zip = ${zip}')
lookup.add_record_input_view('input1',
                             lead_reader.get_output_view('output1')['fields'],
                             '')
lookup.add_record_output_view('output1',
                              [('fullname',         'string',       'Full name'),
                               ('morethan50k',      'string',       'MoreThan50K'),
                               ('zip',              'number',       'Zip Code')],
                              '')

error = lookup.validate()
if error is not None:
    print_resource_validate_info(error)
lookup.save(RESOURCE_ROOT + '/dblookup')

########################################
# File writer
########################################

writer = snapi.create_resource_object(SERVER_URL, 'snaplogic.components.CsvWrite')
writer.set_property_value('filename', 'file://tutorial/data/dblookup_example_out.csv')
writer.add_record_input_view('input1',
                             lookup.get_output_view('output1')['fields'],
                             '')
error = writer.validate()
if error is not None:
    print_resource_validate_info(error)
writer.save(RESOURCE_ROOT + '/writer')

########################################
# Pipeline
########################################

p = snapi.create_resource_object(SERVER_URL, snapi.PIPELINE)
p.add(lead_reader, "LeadReader")
p.add(lookup, "Lookup")
p.add(writer, "Writer")

p.link_views('LeadReader', 'output1', 'Lookup', 'input1', [('zip', 'zip'),
                                                           ('first', 'first'),
                                                           ('last', 'last')])
p.link_views('Lookup', 'output1', 'Writer', 'input1', [('fullname', 'fullname'),
                                                       ('morethan50k', 'morethan50k'),
                                                       ('zip', 'zip')])
error = p.validate()
if error is not None:
    print_resource_validate_info(p)
p.save(RESOURCE_ROOT + '/pipe')

h = snapi.exec_resource(RESOURCE_ROOT + '/pipe')
h.wait()

print "Done"
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.