示例#1
0
def rockset_querymaker(query):
    # connect to Rockset

    rs = Client(api_key=api_key)
    print("query is", query)

    if query not in queries:
        print("Err!")

        return

    time = timeit.timeit(str(rs.sql(Q(queries[query]))))

    print(query, 1000 * time)

    return query, time * 1000
示例#2
0
def run(args):
    if args.profile:
        rs = Client(profile=args.profile)
    else:
        rs = Client()

    queries = []
    for f in os.listdir(args.query_dir):
        if not f.endswith('.sql'):
            continue
        query_id = os.path.splitext(f)[0]
        with open(os.path.join(args.query_dir, f), 'r') as f:
            query_str = f.read()
        queries.append((query_id, query_str))
    queries = sorted(queries)
    print('Found {} queries to run. Will run each {} times and take the median'
          ' runtime.'.format(len(queries), args.runs))
    print('=' * 70)

    for query_id, query_str in queries:
        times = []
        rows = None
        error = False
        for _ in range(args.runs):
            start = time.time()
            try:
                resp = rs.sql(Q(query_str))
                if rows is None:
                    rows = len(resp.results())
                else:
                    assert rows == len(resp.results())
                times.append(1000 * (time.time() - start))
            except Exception as e:
                print('Query {} produced an error: {}'.format(
                    query_id, str(e)))
                error = True
                break
        if not error:
            print('Query {} produced {:>3d} rows in {:>5.0f} ms'.format(
                query_id, rows, statistics.median(times)))
import flask
from flask_geomapper import flask_geomapper
from apscheduler.schedulers.background import BackgroundScheduler
from rockset import Client, Q
from os import getenv

app = flask.Flask(__name__)
fg = flask_geomapper(app, debug=True)

token = getenv("RS2_TOKEN") # or set token to a string with your API key

rs = Client(token, "https://api.rs2.usw2.rockset.com") # configure server based off your location (this one is us west)
collection_name = "flask-locations" # configure based off your collection name and workspace (if not in "commons")
collection = rs.Collection.retrieve(collection_name)

previous_locations = list(rs.sql(Q(f"select * from \"{collection_name}\""))) # retrieve previous locations from database

if previous_locations != []: fg.add_locations(previous_locations, ip_key="_id") # if there are any items in the database, add them to flask-geomapper

def add_docs():
    collection.add_docs(fg.shape_to_docs())

scheduler = BackgroundScheduler(daemon=True) # init scheduler
scheduler.add_job(func=collection.add_docs, args=(fg.shape_to_docs(ip_key="_id"), ), trigger="interval", seconds=10)
"""
^^^
Add documents to collection every ten seconds.
Only locations with an unrecorded IP are added, by setting the `ip_key` parameter of 
`fg.shape_to_docs` (`flask_geomapper.flask_geomapper().shape_to_docs`) to `_id`, the
unique document identifier for Rockset.
"""