Illustration Image

Cassandra.Link

The best knowledge base on Apache Cassandra®

Helping platform leaders, architects, engineers, and operators build scalable real time data platforms.

10/31/2017

Reading time:1 min

HAPI Cassandra

by John Doe

#REST API on top of Cassandra database with hapi Nodejs frameworkThe goal of this project is to create a very simple REST API on top of Cassandra tables.In this example, services are created and used by users at a defined date.In coming versions there will be more Cassandra and DataStax Enterprise features like TTL and Spark usage. An interface is also planned to play with the API.##InstallationYou need a Cassandra cluster running, for download and installation instructions http://planetcassandra.org/cassandra/Clone all js files in a local directoryInstall Nodejs : depending of your OS see https://github.com/joyent/node/wiki/installation for installation instructionsCreate your nodejs project$npm initInstall hapi a server framework for builing application and services (rest api) for nodejs http://hapijs.com/$npm install hapi -saveInstall joi hapi plugin for object schema validation https://github.com/hapijs/joi$npm install --save joiInstall Cassandra Nodejs driver from https://github.com/datastax/nodejs-driver$npm install --save cassandra-driverIn your Cassandra cluster, create the keyspace and the table with CQL found in services_ks.cql file##UsingRun HTTP Nodejs server$node server.jsTest the REST APIIn command line with curlCreate a service$curl -X POST -H "Content-Type: application/json" -d '{"title":"The best service"}' http://localhost:3000/service/create -iAdd a user using this service$curl -X POST -H "Content-Type: application/json" -d '{"id":"d0f60aa8-54a9-4840-b70c-fe562b68842b","user":"victor"}' http://localhost:3000/service/user -iGet all users for a service$curl -X GET http://localhost:3000/service/d0f60aa8-54a9-4840-b70c-fe562b68842b -iGet all services$curl -X GET http://localhost:3000/services -iDelete a service$curl -X DELETE http://localhost:3000/service/delete/c391b62c-5e72-4e5d-ae74-6a5acd0a3ac3 -i

Illustration Image

#REST API on top of Cassandra database with hapi Nodejs framework

The goal of this project is to create a very simple REST API on top of Cassandra tables. In this example, services are created and used by users at a defined date. In coming versions there will be more Cassandra and DataStax Enterprise features like TTL and Spark usage. An interface is also planned to play with the API.

##Installation

##Using

  • Run HTTP Nodejs server

    $node server.js
    
  • Test the REST API

    • In command line with curl

    Create a service

    $curl -X POST -H "Content-Type: application/json" -d '{"title":"The best service"}' http://localhost:3000/service/create -i
    

    Add a user using this service

    $curl -X POST -H "Content-Type: application/json" -d '{"id":"d0f60aa8-54a9-4840-b70c-fe562b68842b","user":"victor"}' http://localhost:3000/service/user -i
    

    Get all users for a service

    $curl -X GET http://localhost:3000/service/d0f60aa8-54a9-4840-b70c-fe562b68842b -i
    

    Get all services

    $curl -X GET http://localhost:3000/services -i
    

    Delete a service

    $curl -X DELETE http://localhost:3000/service/delete/c391b62c-5e72-4e5d-ae74-6a5acd0a3ac3 -i
    

Related Articles

cassandra
slides
java

Seattle Cassandra Users: An OSS Java Abstraction Layer for Cassandra

Josh Turner

9/23/2020

cassandra
slides

Checkout Planet Cassandra

Claim Your Free Planet Cassandra Contributor T-shirt!

Make your contribution and score a FREE Planet Cassandra Contributor T-Shirt! 
We value our incredible Cassandra community, and we want to express our gratitude by sending an exclusive Planet Cassandra Contributor T-Shirt you can wear with pride.

Join Our Newsletter!

Sign up below to receive email updates and see what's going on with our company

Explore Related Topics

AllKafkaSparkScyllaSStableKubernetesApiGithubGraphQl

Explore Further

resources