Thursday, September 8, 2016

Problem with Ubuntu 16.04 and cqlsh in Cassandra 3.7

When I was installing Cassandra 3.7 on Ubuntu 16.04 I faced issue :

Connection error: ('Unable to connect to any servers', {'127.0.0.1': TypeError('ref() does not take keyword arguments',)})

To Solve this I have done the following and it worked :
     sudo apt update
     sudo apt upgrade                  <upgrade the old packages>
     sudo apt install python-pip   <install pip for python if not installed>
     pip install cassandra-driver   <install Cassandra driver>
      pip install --upgrade pip       <upgrade pip. This step is not required>
     export CQLSH_NO_BUNDLED=true 


After above changes cqlsh started working for me.
It might be due to a change in the behavior of the weakref.ref() function in Python 2.7.12.

Monday, July 7, 2014

Maven Tip: Get sources and Javadocs of jars

When we are using Maven in an IDE we often find/feel the need of IDE to resolve source code and Javadocs for the library dependencies. There’s an easy way to accomplish that goal which maven provides us.

# mvn dependency:sources
# mvn dependency:resolve -Dclassifier=javadoc

The first command will attempt to download source code for each of the dependencies in your pom file.

The second command will attempt to download the Javadocs.
Only problem here is that some of them won’t have source code packaged and so they won’t have Javadocs.

Saturday, January 5, 2013

Use of String's intern() Method in Java

Someone ask me "Do you Know the use of Strings intern() in java ?"

I was confused because I heard of it but never had the opportunity to use it. So I googled it and found something interesting.

As per meaning/method summary given in JAVA API Docs :
intern() Returns a canonical representation for the string object.

So actually what it do??


A pool of String initially empty, is maintained privately by String  class.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned.
 Otherwise, this String object is added to the pool and a reference to this String object is returned.

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

An example is given below :

public class InternExample {
 public static void main(String[] args)
   {
       // Create three strings in three different ways.
       String s1 = "Intern Example";
       String s2 = new StringBuffer("Intern").append(" Example").toString();
       String s3 = s2.intern();

       //Compare  strings using == & equals
       System.out.println("s1 == s2? " + (s1 == s2)+" --"+s1.equals(s2));
       System.out.println("s1 == s3? " + (s1 == s3)+" --"+s1.equals(s3));
   }
}


In the above example the object references are compared as well as the value of String objects.
Output is :

s1 == s2? false --true
s1 == s3? true --true



Saturday, November 24, 2012

Cassandra JDBC to perform CRUD operations in Cassandra NoSql DB

Hope everybody is doing good. I was trying and using different NOSQL databases. Last week I used Cassandra. It was a good experience , as a developer I developed and started using Cassandra with JAVA.
There are many Java client available for Cassandra. Hector, Kundera, Easy Cassandra are few of them.  As  I'm just migrating from MySql to Cassandra ,I preferred   Cassandra JDBC Driver because it is quite simple and easy to use. Its just like using a jdbc driver and the queries are quite simple too.

Cassandra JDBC is a  JDBC-compliant driver for Cassandra using CQL.
For somebody new to Cassandra download Cassandra from  link http://cassandra.apache.org/download/.I used Cassandra 1.1.6 latest stable release available.You can download the latest stable build of Cassandra JDBC from the link http://code.google.com/a/apache-extras.org/p/cassandra-jdbc/downloads/list .

If you are new to cassandra and want to know how to install and use you can view link http://wiki.apache.org/cassandra/GettingStarted. This is one of the best link  if you want to start and explore Cassandra.
 Start the Cassandra  server and open the Cassandra-cli a command line interface to create and view keyspaces. We can perform other task as well like creating column family but we'll be using JAVA and cassandra  JDBC to perform CRUD operation in cassandra.


Create Keyspace keyspace1 .
Syntax is  CREATE KEYSPACE keyspace1.


Include the Cassandra JDBC driver in your project and import the files :
 
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
 
Load the driver and make JDBC connection :

Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
java.sql.Connection con=null;
con=DriverManager.getConnection("jdbc:cassandra://localhost:9160/Keyspace1");
 

To create a column family created as 
String query="CREATE columnfamily news"+
               "(key int primary key, category text , linkcounts int ,url text)";
Statement st = con.createStatement();

same way we can insert values into column family newLinkCount as :
String data=
         "BEGIN BATCH \n"+   
         "insert into news (key, category, linkcounts,url) values ('user5','class',71,'news.com') \n"+
         "insert into news (key, category, linkcounts,url) values ('user6','education',15,'tech.com') \n"+
         "insert into news (key, category, linkcounts,url) values ('user7','technology',415,'ba.com') \n"+
         "insert into news (key, category, linkcounts,url) values ('user8','travelling',45,'google.com/teravel') \n"+
         "APPLY BATCH;"

Statement st = con.createStatement();
st.executeUpdate(data);

To drop a column family :
       String data="drop columnfamily "+ name +";";
      Statement st = con.createStatement();
       st.execute(data);

   
 
 The original code look likes below :

package com.classteacher.common.db;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;

public class CassandraCql {
    private static java.sql.Connection con = null;
   
    public static void main(String[] a){
        try {
            Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
            con=DriverManager.getConnection("jdbc:cassandra://localhost:9160/niranjan");
            CassandraCql sample = new CassandraCql();
            String Columnname="subject";
          
            /* -- Functions to perform on Keyspace --*/
            createColumnFamily();
            pouplateData();
            deleteData();
            updateData();
            listData();
            dropColumnFamily("news");
          
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
   
   
     public static void createColumnFamily() throws SQLException {
         String data="CREATE columnfamily news (key int primary key, category text , linkcounts int ,url text)";
         Statement st = con.createStatement();
         st.execute(data);
         }

         public static void dropColumnFamily(String name) throws SQLException {
         String data="drop columnfamily "+ name +";";
         Statement st = con.createStatement();
         st.execute(data);
         }
        
         public static void pouplateData() throws SQLException {
         String data=
         "BEGIN BATCH \n"+  
         "insert into news (key, category, linkcounts,url) values ('user5','class',71,'news.com') \n"+
         "insert into news (key, category, linkcounts,url) values ('user6','education',15,'tech.com') \n"+
         "insert into news (key, category, linkcounts,url) values ('user7','technology',415,'ba.com') \n"+
         "insert into news (key, category, linkcounts,url) values ('user8','travelling',45,'google.com/teravel') \n"+
         "APPLY BATCH;";  
         Statement st = con.createStatement();
        st.executeUpdate(data);
         }
         public static void deleteData() throws SQLException {
         String data=
         "BEGIN BATCH \n"+
         "delete from  news where key='user5' \n"+
         "delete  category from  news where key='user2' \n"+
         "APPLY BATCH;";  
         Statement st = con.createStatement();
         st.executeUpdate(data);
         }
         public static void updateData() throws SQLException {
         String t = "update news set category='sports', linkcounts=1 where key='user5'";         
         Statement st = con.createStatement();
         st.executeUpdate(t);
         }
         public static void listData() throws SQLException {
         String t = "SELECT * FROM news";         
         Statement st = con.createStatement();
         ResultSet rs = st.executeQuery(t);
         while(rs.next())
         {
         System.out.println(rs.getString("KEY"));
         for(int j=1;j<rs.getMetaData().getColumnCount()+1;j++)
         {
         System.out.println(rs.getMetaData().getColumnName(j) +" : "+rs.getString(rs.getMetaData().getColumnName(j)));
         }
         }
         }
   
}

 

 That's it of now. Please share your feedback. Will share more codes and changes with thrift and other Cassandra clients soon.

Monday, October 1, 2012

Database solution for web-scale databases

Database solution for  web-scale databases :

I was looking for a solution to optimize the performance of one of application am working. The main task was to store all the activities on DB. But with MySQL this is not possible as with time the data may increase to thousands of line for a single activity. So I came to know about the NoSql solutions available.
NoSQL database systems are often highly optimized for retrieve and append operations and often offer little functionality beyond record storage (e.g. key-value stores).
The reduced run time flexibility compared to full SQL systems is compensated by significant gains in scalability and performance for certain data models.

NoSQL database management systems are useful when working with a huge quantity of data and the data's nature does not require a relational model for the data structure. The data could be structured, but it is of minimal importance and what really matters is the ability to store and retrieve great quantities of data, and not the relationships between the elements.

Being Java my primary language my main objective was 'How easily I can structure the data to insert onto DB', 'ease to retrieve from DB'  and support for JAVA.
 Hadoop/Hbase, Cassandra, MongoDB, Bigdata are few of them.  I started using Hadoop/Hbase.
Will update on blog with a sample program on usage of Hadoop/Hbase in Java with basic crud operations.