![]() |
VOOZH | about |
Apache Cassandra is a free and open-source, distributed, highly scalable, wide column store, NoSQL database management system. it is designed to handle large volumes of data, providing high availability with no single point of failure.
This article shows you that how to use Cassandra using express js and also shows how to update record in Cassandra using express Cassandra orm framework.
Features: The following features are used in this article.
Example: This example creates Person Table in Cassandra data store with the following columns/attributes.
module.exports={
fields:{
name:"text",
surname:"text",
age:"int",
created:"timestamp"
},
key:["name"]
}
Then it inserts one record using expressjs restful endpoint using express-cassandra
cqlsh> select * from test_ks.person; name | age | created | surname ------+-----+---------------------------------+--------- John | 32 | 2021-04-02 11:05:00.946000+0000 | DoeThen it updates John(name column) record with surname Doe and age 55 with the help of express restful endpoint( please refer below Setting up Environment for updating record in Cassandra section further details).
cqlsh> select * from test_ks.person; name | age | created | surname ------+-----+---------------------------------+--------- John | 55 | 2021-04-02 11:05:00.946000+0000 | Doe (1 rows) cqlsh>
Applications:
Setting up Environment for updating record in Cassandra: Please find the following steps to update record in Cassandra using express js.
Step 1: To setup Cassandra server in docker container.
Please use the following docker commands.
Note: prerequisite is to install docker hub for running cassandra server in docker container.
Please use the following cqlsh command(s) to create keyspace and create a tutorial table in Cassandra to walk through on how to update records in that express.js application for the next steps.
Step 2: To setup express js application.
Please find the following command(s)
Note: prerequisite is to install node for running express js application.
Step 3: To setup cassandra connectivity using express-cassandra.
node index
Note:
root@76561f8b27a2:/# cqlsh Connected to Test Cluster at 127.0.0.1:9042. [cqlsh 5.0.1 | Cassandra 3.11.10 | CQL spec 3.4.4 | Native protocol v4] Use HELP for help. cqlsh> select * from test_ks.person; name | age | created | surname ------+-----+---------+--------- (0 rows) cqlsh>
Note:Actually PersonModel.js maps into Cassandra table and does all the CRUD operation(s) using express-cassandra framework.
Step 4: To update record in Cassandra using express js restful API.
root@76561f8b27a2:/# cqlsh Connected to Test Cluster at 127.0.0.1:9042. [cqlsh 5.0.1 | Cassandra 3.11.10 | CQL spec 3.4.4 | Native protocol v4] Use HELP for help. cqlsh> select * from test_ks.person; name | age | created | surname ------+-----+---------------------------------+--------- John | 32 | 2021-04-02 11:05:00.946000+0000 | Doe (1 rows) cqlsh>
MINGW64 ~ $ curl -v -X PUT http://localhost:3000/person/John/Doe/55 * Trying 127.0.0.1:3000... * TCP_NODELAY set * Connected to localhost (127.0.0.1) port 3000 (#0) > PUT /person/John/Doe/55 HTTP/1.1 > Host: localhost:3000 > User-Agent: curl/7.67.0 > Accept: */* > * Mark bundle as not supporting multiuse < HTTP/1.1 200 OK < X-Powered-By: Express < Content-Type: text/html; charset=utf-8 < Content-Length: 14 < ETag: W/"e-TKKHGLrHAbIKlf0bIe4gdwzz0N0" < Date: Fri, 02 Apr 2021 11:11:37 GMT < Connection: keep-alive < person updated* Connection #0 to host localhost left intact
root@76561f8b27a2:/# cqlsh Connected to Test Cluster at 127.0.0.1:9042. [cqlsh 5.0.1 | Cassandra 3.11.10 | CQL spec 3.4.4 | Native protocol v4] Use HELP for help. cqlsh> select * from test_ks.person; name | age | created | surname ------+-----+---------------------------------+--------- John | 55 | 2021-04-02 11:05:00.946000+0000 | Doe (1 rows) cqlsh>