Coder Social home page Coder Social logo

quickstart-python's Introduction

DataStax Python Driver for Apache Cassandra Quickstart

A basic Python demo CRUD application using the DataStax Python Driver for Apache Cassandra. The intent is to help users get up and running quickly with the driver. If you are having trouble, the complete code solution for quickstart.py can be found here.

Prerequisites

  • A running instance of Apache Cassandra® 2.1+
  • Python 2.7, 3.4, 3.5, or 3.6.
  • Use Pip to install the driver: pip install cassandra-driver
  • We highly recommend to use a virtualenv

Create the keyspace and table

The users.cql file provides the schema used for this project:

CREATE KEYSPACE demo
    WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'};

CREATE TABLE demo.users (
    lastname text PRIMARY KEY,
    age int,
    city text,
    email text,
    firstname text);

Connect to your cluster

All of our code is contained in the quickstart.py file. The create_connection() function connects to our cluster. By default, Cluster() will try to connect to 127.0.0.1 (localhost). Replace with your own contact point(s) if necessary.

def create_connection():
    # TO DO: Fill in your own contact point
    cluster = Cluster(['127.0.0.1'])
    return cluster.connect('demo')

CRUD Operations

Fill the code in the functions that will add a user, get a user, update a user and delete a user from the table with the driver.

INSERT a user

def set_user(session, lastname, age, city, email, firstname):
    # TO DO: execute SimpleStatement that inserts one user into the table
    session.execute("INSERT INTO users (lastname, age, city, email, firstname) VALUES (%s,%s,%s,%s,%s)", [lastname, age, city, email, firstname])

SELECT a user

def get_user(session, lastname):
    # TO DO: execute SimpleStatement that retrieves one user from the table
    # TO DO: print firstname and age of user
    result = session.execute("SELECT * FROM users WHERE lastname = %s", [lastname]).one()
    print result.firstname, result.age

UPDATE a user's age

def update_user(session, new_age, lastname):
    # TO DO: execute SimpleStatement that updates the age of one user
    session.execute("UPDATE users SET age =%s WHERE lastname = %s", [new_age, lastname])

DELETE a user

def delete_user(session, lastname):
    # TO DO: execute SimpleStatement that deletes one user from the table
    session.execute("DELETE FROM users WHERE lastname = %s", [lastname])

License

Copyright 2019 Rebecca Mills

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

quickstart-python's People

Contributors

beccam avatar

Watchers

 avatar  avatar

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.