Deploy Cassandra on a virtual machine and use it as the back end for a simple bl
ID: 3786665 • Letter: D
Question
Deploy Cassandra on a virtual machine and use it as the back end for a simple blogging application.
You must implement a data model that demonstrates a non-relational, column-family approach for your application
Create a simple application (console or GUI, platform/language of your choice) to meet the following requirements:
Support N users (no login or password; just a user name)
Allow N users to store blog posts with a title and content (plain text only, no formatting or HTML)
Allow all users to view all blog post titles from all users (i.e. a list of content that’s available on the blogging platform)
Allow all users to add comments on all blog posts
i.Comments do NOT need to be nested (no commenting on comments)
Allow all users to view all blog posts by a user
i.Viewers will see:
The blog title, time stamp, posting user name, and content
The associated comments, commenter user name, and comment time stamp.
I have cassandra deployed on my vm
package CassandraApp.CassandraApp;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.Session;
import static java.lang.System.out;
public class CassandraConnector {
/** Cassandra Cluster. */
private Cluster cluster;
/** Cassandra Session. */
private Session session;
/**
* Connect to Cassandra Cluster specified by provided node IP
* address and port number.
*
* @param node Cluster node IP address.
* @param port Port of cluster host.
*/
public void connect(final String node, final int port)
{
this.cluster = Cluster.builder().addContactPoint(node).withPort(port).build();
final Metadata metadata = cluster.getMetadata();
out.printf("Connected to cluster: %s ", metadata.getClusterName());
for (final Host host : metadata.getAllHosts())
{
out.printf("Datacenter: %s; Host: %s; Rack: %s ",
host.getDatacenter(), host.getAddress(), host.getRack());
}
session = cluster.connect();
}
/**
* Provide my Session.
*
* @return My session.
*/
public Session getSession()
{
return this.session;
}
/** Close cluster. */
public void close()
{
cluster.close();
}
public static void main(final String[] args)
{
final CassandraConnector client = new CassandraConnector();
final String ipAddress = args.length > 0 ? args[0] : "localhost";
final int port = args.length > 1 ? Integer.parseInt(args[1]) : 9042;
out.println("Connecting to IP Address " + ipAddress + ":" + port + "...");
client.connect(ipAddress, port);
client.close();
}
}
This is what I have to connect java to cassandra, and it works I just dont know how code cql into java.
Explanation / Answer
create table bloguser using Cassandra Query Language (CQL)
BlogUser.cql
CREATE TABLE blog
(
Name varchar,
description varchar,
PRIMARY KEY (Name)
);
The above file can be executed within cqlsh with the command source 'C:cassandracqlBolgUser.cql'
the same thing can also be created using java as
final String bolgUserCql =
"CREATE TABLE blog_keyspace.blog (Name varchar,description varchar, PRIMARY KEY (title, year))";
client.getSession().execute(createblogUserCql);
Shell of blogPersistence.java
package dustin.examples.cassandra;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import java.util.Optional;
import static java.lang.System.out;
/**
* Handles blog persistence access.
*/
public class blogPersistence
{
private final CassandraConnector client = new CassandraConnector();
public blogPersistence(final String newHost, final int newPort)
{
out.println("Connecting to IP Address " + newHost + ":" + newPort + "...");
client.connect(newHost, newPort);
}
/**
* Close my underlying Cassandra connection.
*/
private void close()
{
client.close();
}
}
The next steps are to manipulate data related to this table.
The next code listing shows a method that could be used to write new rows to the blog table.
public void persistblog(
final String Name, final String description)
{
client.getSession().execute(
"INSERT INTO blog_keyspace.blog (Name,description) VALUES (?, ?)",
Name,description);
}
With the data inserted into the blog table, we need to be able to query it.
The next code listing shows one potential implementation for querying a blog by Name.
Querying with Cassandra Java Driver
/**
* Returns blog matching provided Name.
*
* @param Name of desired movie.
*/
public Optional<blog> queryblogByName(final String Name)
{
final ResultSet blogResults = client.getSession().execute(
"SELECT * from blog_keyspace.blog WHERE Name = ? ",Name);
final Row blogRow = blogResults.one();
final Optional<blog> blog =
blogRow != null
? Optional.of(new blog(
blogRow.getString("Name"),
blogRow.getString("description"),
: Optional.empty();
return blog;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.