Code Monkey home page Code Monkey logo

flask-cqlalchemy's Introduction

Flask-CQLAlchemy

Latest Version License Python Versions CI Build and Test Code Climate

Flask-CQLAlchemy handles connections to Cassandra clusters and gives a unified easier way to declare models and their columns.

Installation

$ pip install flask-cqlalchemy

Dependencies

As Flask-CQLAlchemy depends only on the cassandra-driver. It is assumed that you already have Flask installed.

Flask-CQLAlchemy has been tested with all versions of the cassandra-driver>=3.22.0 and Cassandra 3.0.25, 3.11.11, 4.x. All previous versions and configurations are deprecated. Used to be reported that plugin worked with cassandra-driver>=2.5, we can not guarantee proper work of older configurations so use on your own. Some versions of cassandra-driver can be incompatible with some versions of Cassandra itself either.

If you have problems using the plugin, try updating to the latest patch version of the minor version you are using.

Example

# example_app.py
import uuid
from flask import Flask
from flask_cqlalchemy import CQLAlchemy

app = Flask(__name__)
app.config['CASSANDRA_HOSTS'] = ['127.0.0.1']
app.config['CASSANDRA_KEYSPACE'] = "cqlengine"
db = CQLAlchemy(app)


class User(db.Model):
    uid = db.columns.UUID(primary_key=True, default=uuid.uuid4)
    username = db.columns.Text(required=False)

User Defined Types

# example_app_udt.py
from flask import Flask

from flask_cqlalchemy import CQLAlchemy

app = Flask(__name__)
app.config['CASSANDRA_HOSTS'] = ['127.0.0.1']
app.config['CASSANDRA_KEYSPACE'] = "cqlengine"
app.config['CASSANDRA_SETUP_KWARGS'] = {'protocol_version': 3}
db = CQLAlchemy(app)


class Address(db.UserType):
    street = db.columns.Text()
    zipcode = db.columns.Integer()


class Users(db.Model):
    __keyspace__ = 'cqlengine'
    name = db.columns.Text(primary_key=True)
    addr = db.columns.UserDefinedType(Address)

Usage

Enter in Python Interpreter:

>>> from example_app import db, User
>>> db.sync_db()
>>> user1 = User.create(username='John Doe')
>>> user1
User(example_id=UUID('f94b6156-2964-4d46-919c-d6e4abcb9ef1'), username='John Doe')

User Defined Types

>>> from example_app_udt import db, Address, Users
>>> db.sync_db()
>>> user_address = Address(street="Easy Street, 12", zipcode=12345)
>>> user = Users(name='John Appleseed', addr=user_address)
>>> user
Users(name='John Appleseed', addr=<example_app_udt.Address object at 0x10fe56070>)
>>> user.addr
<example_app_udt.Address object at 0x10fe56070>
>>> user.addr.street
'Easy Street, 12'
>>> user.addr.zipcode
12345

For a complete list of available methods refer to the cassandra.cqlengine.models documentation.

Configuration Options

CQLAlchemy object provides following the options available for the cqlengine connection.setup():

  • CASSANDRA_HOSTS — A list of hosts
  • CASSANDRA_KEYSPACE — The default keyspace name to use
  • CASSANDRA_CONSISTENCY — The global default ConsistencyLevel, default is the driver's Session.default_consistency_level
  • CASSANDRA_LAZY_CONNECTTrue if should not connect until first use, default is False
  • CASSANDRA_RETRY_CONNECTTrue if we should retry to connect even if there was a connection failure initially, default is False
  • CASSANDRA_SETUP_KWARGS — Pass-through keyword arguments for Cluster()

API

CQLAlchemy object provides some helper methods for Cassandra database management:

  • sync_db() — Creates/Syncs all the tables corresponding to the models declared in the application.
  • set_keyspace() — Sets the keyspace for a session. Keyspaces once set will remain the default keyspace for the duration of the session. If the change is temporary, it must be reverted back to the default keyspace explicitly.

Contributing

Found a bug? Need a feature? Open it in issues, or even better, open a PR. Please include tests in the PR.

flask-cqlalchemy's People

Contributors

onix avatar thegeorgeous avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flask-cqlalchemy's Issues

Batch writes?

Hi there,
Is there a way to perform batch writes?

We currently use the plugin to use an ORM-like interface and we do upserts using the save method. This prevents us to use batch statements for efficient writing.

Thanks!
d

Flask-CQLAlchemy getting issue NoHostAvailable

Hi George Thomas,

I installed your python library for Flask but I'm getting issues. I created a Model like in your example, then in a flask view I have a route to get all datas from a columnfamily. But when I do a GET request on this route, I get this error :

[Thu Aug 11 15:40:58.637843 2016] [wsgi:error] [pid 6021] [client 192.168.20.8:47626]
[Thu Aug 11 15:40:58.637912 2016] [wsgi:error] [pid 6021] ERROR:run:Exception on /operations/ [GET]
[Thu Aug 11 15:40:58.637923 2016] [wsgi:error] [pid 6021] Traceback (most recent call last):
[Thu Aug 11 15:40:58.637927 2016] [wsgi:error] [pid 6021]   File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1988, in wsgi_app
[Thu Aug 11 15:40:58.637931 2016] [wsgi:error] [pid 6021]     response = self.full_dispatch_request()
[Thu Aug 11 15:40:58.637934 2016] [wsgi:error] [pid 6021]   File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1641, in full_dispatch_request
[Thu Aug 11 15:40:58.637938 2016] [wsgi:error] [pid 6021]     rv = self.handle_user_exception(e)
[Thu Aug 11 15:40:58.637941 2016] [wsgi:error] [pid 6021]   File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1544, in handle_user_exception
[Thu Aug 11 15:40:58.637945 2016] [wsgi:error] [pid 6021]     reraise(exc_type, exc_value, tb)
[Thu Aug 11 15:40:58.637949 2016] [wsgi:error] [pid 6021]   File "/usr/local/lib/python3.4/dist-packages/flask/_compat.py", line 33, in reraise
[Thu Aug 11 15:40:58.637952 2016] [wsgi:error] [pid 6021]     raise value
[Thu Aug 11 15:40:58.637956 2016] [wsgi:error] [pid 6021]   File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1639, in full_dispatch_request
[Thu Aug 11 15:40:58.637959 2016] [wsgi:error] [pid 6021]     rv = self.dispatch_request()
[Thu Aug 11 15:40:58.637963 2016] [wsgi:error] [pid 6021]   File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1625, in dispatch_request
[Thu Aug 11 15:40:58.637967 2016] [wsgi:error] [pid 6021]     return self.view_functions[rule.endpoint](**req.view_args)
[Thu Aug 11 15:40:58.637970 2016] [wsgi:error] [pid 6021]   File "/var/www/html/cassandrarest/app/main/views.py", line 20, in getOperations
[Thu Aug 11 15:40:58.637974 2016] [wsgi:error] [pid 6021]     if not operations:
[Thu Aug 11 15:40:58.637977 2016] [wsgi:error] [pid 6021]   File "/usr/local/lib/python3.4/dist-packages/cassandra/cqlengine/query.py", line 351, in __len__
[Thu Aug 11 15:40:58.637981 2016] [wsgi:error] [pid 6021]     self._execute_query()
[Thu Aug 11 15:40:58.637984 2016] [wsgi:error] [pid 6021]   File "/usr/local/lib/python3.4/dist-packages/cassandra/cqlengine/query.py", line 386, in _execute_query
[Thu Aug 11 15:40:58.637988 2016] [wsgi:error] [pid 6021]     self._result_generator = (i for i in self._execute(self._select_query()))
[Thu Aug 11 15:40:58.637992 2016] [wsgi:error] [pid 6021]   File "/usr/local/lib/python3.4/dist-packages/cassandra/cqlengine/query.py", line 318, in _execute
[Thu Aug 11 15:40:58.637995 2016] [wsgi:error] [pid 6021]     result = _execute_statement(self.model, statement, self._consistency, self._timeout)
[Thu Aug 11 15:40:58.638008 2016] [wsgi:error] [pid 6021]   File "/usr/local/lib/python3.4/dist-packages/cassandra/cqlengine/query.py", line 1359, in _execute_statement
[Thu Aug 11 15:40:58.638013 2016] [wsgi:error] [pid 6021]     return connection.execute(s, params, timeout=timeout)
[Thu Aug 11 15:40:58.638017 2016] [wsgi:error] [pid 6021]   File "/usr/local/lib/python3.4/dist-packages/cassandra/cqlengine/connection.py", line 170, in execute
[Thu Aug 11 15:40:58.638020 2016] [wsgi:error] [pid 6021]     result = session.execute(query, params, timeout=timeout)
[Thu Aug 11 15:40:58.638024 2016] [wsgi:error] [pid 6021]   File "cassandra/cluster.py", line 1941, in cassandra.cluster.Session.execute (cassandra/cluster.c:33642)
[Thu Aug 11 15:40:58.638028 2016] [wsgi:error] [pid 6021]   File "cassandra/cluster.py", line 3629, in cassandra.cluster.ResponseFuture.result (cassandra/cluster.c:69369)
[Thu Aug 11 15:40:58.638034 2016] [wsgi:error] [pid 6021] cassandra.cluster.NoHostAvailable: ('Unable to complete the operation against any hosts', {<Host: 192.168.20.6 DC1>: error('required argument is not an integer',), <Host: 192.168.20.9 DC1>: error('required argument is not an integer',), <Host: 192.168.20.5 DC1>: error('required argument is not an integer',), <Host: 192.168.20.4 DC1>: error('required argument is not an integer',)})

My flask app structure looks like below:

run.py
|
app
   |_config.py
   |_db_config.py (Used to instanciate CQLAlchemy to avoid cyclic imports in my project)
   |_models.py (where I have my class model)
   |_main
         |_views.py
#run.py
from flask import Flask
from app.config import Production, Testing
from flask_cqlalchemy import CQLAlchemy

app = Flask(__name__)
app.config.from_object(Testing)
app._static_folder = "./app/static"

from app.main.views import main as main_blueprint
app.register_blueprint(main_blueprint, url_prefix='/operations')

from app.db_config import db


if __name__ == "__main__":
        app.run()

#app/config.py
import os

class Config(object):
        """Default Flask configuration inherited by all environments. Use this for development environments."""
        PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
        DEBUG = True
        TESTING = False
        CSRF_ENABLED = True
        CSRF_SESSION_KEY = "xxx"
        SECRET_KEY = "xxx"
        HOST = "0.0.0.0"
        PORT = 80
        CASSANDRA_HOSTS = ['192.168.20.9', '192.168.20.5']
        CASSANDRA_CONSISTENCY = "QUORUM"
        CASSANDRA_SETUP_KWARGS = {'protocol_version': 3}


class Production(Config):
        DEBUG = False
        CASSANDRA_KEYSPACE = "myKeyspace"

class Testing(Config):
        TESTING = True
        CASSANDRA_KEYSPACE = "myKeyspace_test"

#app/db_config.py
from flask_cqlalchemy import CQLAlchemy
from run import app

db = CQLAlchemy(app)

#app/models.py
from app.db_config import db

class Operations(db.Model):
        client = db.columns.Text(partition_key=True, required=True)
        type = db.columns.Text(primary_key=True, required=True)
        probe_id = db.columns.Text(primary_key=True, required=True)
        op_start = db.columns.DateTime(primary_key=True, required=True)
        op_end = db.columns.DateTime(primary_key=True, required=True)
        parameters = db.columns.Map(db.columns.Text, db.columns.Text)
        docs = db.columns.Map(db.columns.Text, db.columns.Text)
        requester = db.columns.Text()
        request_date = db.columns.DateTime()
        event_id = db.columns.Text()
        status = db.columns.Text()

#app/main/views.py
from __future__ import print_function
from flask import Blueprint, jsonify, request
from app.models import Operations
from app.db_config import db
from datetime import datetime
import sys

main = Blueprint('operations', __name__)

@main.route("/", methods=["GET"])
def getOperations():
        operations = Operations.objects.all()
        if not operations:
                resp = jsonify({'operations': ''})
                resp.status_code = 200
                return resp
        op_json = []
        for operation in operations:
                op_json.append(
                        {
                                'client' : operation.client,
                                'type' : operation.type,
                                'probe_id' : operation.probe_id,
                                'op_start' : operation.op_start,
                                'op_end' : operation.op_end,
                                'parameters' : operation.parameters,
                                'docs' : operation.docs,
                                'requester' : operation.requester,
                                'request_date' : operation.request_date,
                                'event_id' : operation.event_id,
                                'status' : operation.status
                        }
                )
        resp = jsonify({'operations': op_json})
        resp.status_code = 200
        return resp

Any idea why I get this NoHostAvailable? nodetool status tells me that everything is ok, and before using your library I was using the cassandra-driver native and it was working fine. And about db_sync() didn't really understood how I have to use it.

Versions used:

cassandra = 3.0.5
cassandra_driver = 3.5.0
Flask-CQLAlchemy = 1.2.0
Flask = 0.11.1

Thanks in advance and wish you a great day.

Flask-CQLAlchemy DB objects dont have 'query' attribute

I am trying to write a flask application with Cassandra. I am using the Flask-CQLAlchemy library to do this and the basic querying supported by the ORM doesn't seem to work.

My code looks something like this :

from flask_cqlalchemy import CQLAlchemy
from flask import Flask

db = CQLAlchemy(app)

class Person(db.Model):
    id = db.columns.UUID(primary_key=True)
    first_name  = db.columns.Text()
    last_name = db.columns.Text()

When I open up the terminal and go to Python, I create a new person with

person1 = Person(first_name='ABC', last_name='WYZ')
It goes through but when I query the Person database with :

Person.query.limit(1).all()
I get an error saying :

AttributeError: type object 'Person' has no attribute 'query'

Is there something else that needs to be added in the CQLAlchemy program? If this code is write then is there another way to query data?

where to set authentication info ?

I try to set authentication information by set:

CASSANDRA_SETUP_KWARGS = {'protocol_version': 3, "username": "cassandra_user", "password": "passwd"}

but received the following errors:

File "/repos/python3/lib/python3.5/site-packages/flask_cqlalchemy/__init__.py", line 40, in __init__
    self.init_app(app)
  File "/repos/python3/lib/python3.5/site-packages/flask_cqlalchemy/__init__.py", line 64, in init_app
    **setup_kwargs)
  File "/repos/python3/lib/python3.5/site-packages/cassandra/cqlengine/connection.py", line 267, in setup
    retry_connect=retry_connect, cluster_options=kwargs, default=True)
  File "/repos/python3/lib/python3.5/site-packages/cassandra/cqlengine/connection.py", line 149, in register_connection
    conn.setup()
  File "/repos/python3/lib/python3.5/site-packages/cassandra/cqlengine/connection.py", line 89, in setup
    raise CQLEngineException("Username & Password are now handled by using the native driver's auth_provider")
cassandra.cqlengine.CQLEngineException: Username & Password are now handled by using the native driver's auth_provider

what's the correct way to do this?

Add Vector support

The official Python driver has released this PR datastax/python-driver#1161 which introduces a new Vector type.

Am I right in assuming that flask-cqlalchemy requires a patch to support this new type?

If so, I'm more than happy to look at creating a PR. I might need a bit of support though, as Python isn't my first language.

Add automated PyPI deployments via Travis

Travis CI can automatically release a Python package to PyPI after a successful build. For this to be a viable option it must be able to create both wheel and egg distributions since wheel distributions are not supported by setup_tools

Release the new PyPi version and support for Python 3.10

Hi, we are trying to migrate our Flask application from Python 3.9 to 3.10 and pip install fails while compiling the blist dependency with a clang code error:

    blist/_blist.c:5403:23: warning: comparison of integers of different signs: 'Py_ssize_t' (aka 'long') and 'unsigned long' [-Wsign-compare]
            for (j = 0; j < NUM_PASSES; j++) {
                        ~ ^ ~~~~~~~~~~
    blist/_blist.c:6592:27: error: expression is not assignable
            Py_REFCNT(&saved) = 1;
            ~~~~~~~~~~~~~~~~~ ^
    4 warnings and 1 error generated.
    error: command '/usr/bin/clang' failed with exit code 1

Looking through the recent commit history I found you have removed blist dependency and bumped app version to 1.3.0 but no new version in PyPi was released. Do you plan to release a new version to PyPi and possibly add support for the recent Python version?

Python tested version: 3.10.0, 3.10.1
Flask 2.0.2
cassandra-driver 3.25.0
(blist==1.3.6)

Install using setup.py

I am trying to install this package in my app setup.py and it fails to install as it cannot find 0.2.0 version in pypi.python.org/simple. Can you please make it work?

My app setup.py:

install_requires = [
'Flask-CQLAlchemy==0.2.0',
]


Searching for Flask-CQLAlchemy==0.2.0
Reading https://pypi.python.org/simple
Reading https://pypi.python.org/simple/Flask-CQLAlchemy/
No local packages or download links found for Flask-CQLAlchemy==0.2.0
error: Could not find suitable distribution for Requirement.parse('Flask-CQLAlchemy==0.2.0')

Getting more done in GitHub with ZenHub

Hola! @thegeorgeous has created a ZenHub account for the thegeorgeous organization. ZenHub is the only project management tool integrated natively in GitHub – created specifically for fast-moving, software-driven teams.


How do I use ZenHub?

To get set up with ZenHub, all you have to do is download the browser extension and log in with your GitHub account. Once you do, you’ll get access to ZenHub’s complete feature-set immediately.

What can ZenHub do?

ZenHub adds a series of enhancements directly inside the GitHub UI:

  • Real-time, customizable task boards for GitHub issues;
  • Multi-Repository burndown charts, estimates, and velocity tracking based on GitHub Milestones;
  • Personal to-do lists and task prioritization;
  • Time-saving shortcuts – like a quick repo switcher, a “Move issue” button, and much more.

Add ZenHub to GitHub

Still curious? See more ZenHub features or read user reviews. This issue was written by your friendly ZenHub bot, posted by request from @thegeorgeous.

ZenHub Board

I run this install flask-cqlalchemy

Command "/var/www/app_arsip/flask/bin/python2 -u -c "import setuptools, tokenize;file='/tmp/pip-build-zuvskE/blist/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /tmp/pip-qYK0Yo-record/install-record.txt --single-version-externally-managed --compile --install-headers /var/www/app_arsip/flask/include/site/python2.7/blist" failed with error code 1 in /tmp/pip-build-zuvskE/blist/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.