Apache cassandra NOSQL Database
Apache Cassandra Is an open-source NOSQL database used to manage large amounts of data.It is well known for high scalability, performance, and availability. The Cassandra ecosystem includes a range of third-party Cassandra projects, tools, products, and services for end users, like cloud computing and Kubernetes.Official Web page Apache Cassandra
In today's world, data visualization plays a vital role in providing more data insights. As we know, Apache Cassandra is a very powerful columnar database that holds vast amounts of data sets. In order to visualize more insights out of large datasets, we can use another Apache tool that is useful for a wide variety of data visualizations.
Download Apache Cassandra
Apache ECharts Library for Data Visualization
Apache ECharts is a powerful interactive charting and data visualization library for web browsers. allows developers/users to render interactive charts/graphs and visualizations with less amount of HTML source code. It is a lightweight canvas library written in Javascript.Apache ECharts Home
Download Apache Echart resources
Now, how do I render the data from the Cassndra database table into Echarts? The solution is to build a web-based application that allows communication and data transfer between the frontend view and the backend. we can choose from wide variety of web development scripting and programming languages or frameworks like PHP, JSP, Python, etc.
In this article, I will show that by using the Python flask framework with the Apache Cassandra datasax connector, we can establish a communication channel between the Python and Cassandra database data.
Python Flask offocial Home Page
The Python flask is a popular micro web development framework for WSGI apps; it is a lightweight web application development framework to build complex applications using a rapid application development process.
Steps to visualize data from Cassandra Database to Apache Echarts using Python Flask:
Install Apache Cassandra
To install apache Cassandra, you can use the following commands on Ubuntu terminal:
Installing Cassandra using the Docker image:
cassandra - Official Image Docker HubPull latest cassandra installer image using following command:
Run the cassandra Docker image using following command:
Start the cassandra from Docker image using following command:
Installing Cassandra using the binary tarball:
Download binary package using following command:Unpack the Installer from zip file using following command:
start cassandra using following command:
connect to database cassndra shell using following command:
Installing the Cassandra using Debian packages:
Check Cassandra sources list using following command:Download cassandra trusted GPG keys using following command:
Update apt sources and install cassandra using following command:
Installing the Cassandra using RPM packages:
Update RPM sources and install cassandra using following command:Start cassandra service using following command:
Navigate to the Cassandra/bin directory and use following command to start the Cassandra cluster:
Start Cassandra shell CQLSH in terminal:
Create Keyspace and Use keyspace to create Table in it:
Create table structure to store data
Insert Data/Records into Table
View/Select inserted data from table
Install a code editor like PyCharm or Vscode.
Download VSCODE editor for Linux
Create python web Application directory with virtual environment
Flask web application directory structure for the project looks like:
Install python packages- Flask, Apache Cassandra-driver
Flask to create web application and Cassandra driver act as connector between web app backend and Cassandra data store.
Source files for flask application
Create config.py file with following parameters:
CASSANDRA_CONTACT_POINTS = ['127.0.0.1'] to specify the local cluster.
CASSANDRA_PORT = 9042 to bind app with the cluster port.
CASSANDRA_KEYSPACE = 'chartdata' to bind app with the keyspace from cluster.
Create __init__.py file for flask web Application with following parameters:
Required Packages/libraries for flask Cassandra web application.
Flask app initialization : To create the flask application object
Cassandra initialization : To configure Cassandra connector with cluster and keyspace.
Application url routes : To access application in browser using URL/route
Application run method : To start/run application and host it on url 127.0.0.1:5000
In the source code shown above, we have included a method with the name fetch_data to perform the following operations: Select data from table test_data of configured keyspace chartdata.
Create a list of the category and amount objects from table data to pass as arguments to the Jinja template for graph/chart.
return html template view with chart arguments to display bar chart.
Create Jinja HTML template file "charts.html" and include EChart Java script CDN source
In this web application, our aim is to access data from Cassandra keyspace and display it in the form of a chart using the Echart library on the web page. to achieve this objective, we need to create Jinja template structure with the CDN source from Echart.
In this source, we have used the "echarts.min.js" CDN script source to create chart objects on the HTML web page. I have passed arguments from url route fetch_data to this template.
jsdelivr official Apache Echarts library CDN
jinja placeholders to display the chart data are:
{{ catagory | tojson}}: As an X-axis data element.
{{ chart_type | tojson}} : To specify chart type, ex-bar, line, scatter, and many more
{{ amount | tojson}}: As Y axis data element.
Note: Here, if you want to render the various types of charts from the Echart library, you need to follow the required chart options, components, and data elements to achieve this. You can refer to official chart examples from the Apache Echarts library official website.
Apache Echarts official Examples
Render chart data on the web page
So with the use of Apache Echarts Library and Python Flak application, we can visualize data from the Cassandra database on the web page in an interactive and dynamic way to improve the user experience and to represent more data insights.
Some useful resources
Cassandra installation official documentation
Download latest Python Here
Download Ubuntu Linux Here
Comments
Post a Comment
Thanks for your valuable input