Using Redis (NoSQL database) In Java

Aside

Hello All,

As we have discussed lot about noSQL in my last post “ Using NoSQL database In java (memcached)“.
This is my second post about using noSQL in java. In this post i am going to use redis. Redis is very widely used by many organisation these day’s.
Following are the reason, Why many organisation prefer to use redis database server.

In redis entire data set, like memcached, is stored in-memory so it is extremely fast (like memcached)… often even faster than memcached. Redis had virtual memory, where rarely used values would be swapped out to disk, so only the keys had to fit into memory, but this has been deprecated. Going forward the use cases for Redis are those where its possible (and desirable) for the entire data set to fit into memory.

Redis is a fantastic choice if you want a highly scalable data store shared by multiple processes, multiple applications, or multiple servers. As just an inter-process communication mechanism it is tough to beat. The fact that you can communicate cross-platform, cross-server, or cross-application just as easily makes it a pretty great choice for many many use cases. Its speed also makes it great as a caching layer.

You can use below link to learn the basic of Redis. This is superb link

I am going to complete this post in two phase. In first we see the redis installation and configuration and in second we work on using redis with java and spring.
Redis Installation and Configuration
You can either download the latest Redis tar ball from the redis.io web site, or you can alternatively use this special URL that always points to the latest stable Redis version, that is, http://download.redis.io/redis-stable.tar.gz.
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make

if got exception “make[3]: *** [net.o] Error 127” during make. Use below command

make distclean
apt-get install gcc

then again run make commond

After the compilation the src directory inside the Redis distribution is populated with the different executables that are part of Redis

  •   redis-server is a redis server itself.
  •   redis-cli is the command line interface utility to talk with Redis.
  •   redis-benchmark is used to check redis performance
  •   redis-check-aof and redis-check-dump are useful in rare event of corrupted data files.

Copy both Redis server than the command line interface in proper places using the following commands:

sudo cp redis-server /usr/local/bin/
sudo cp redis-cli /usr/local/bin/
sudo cp redis-benchmark /usr/local/bin/

Install Redis in your Linux box in a proper way using an init script, so that after a restart everything will start again properly.

Create a directory where to store your Redis config files and your data:

sudo mkdir /etc/redis
sudo mkdir /var/redis

Copy the init script that you’ll find in the Redis distribution under the utils directory into /etc/init.d. We suggest calling it with the name of the port where you are running this instance of Redis. For example:

sudo cp utils/redis_init_script /etc/init.d/redis_6379

Edit the init script.

sudo vi /etc/init.d/redis_6379

Make sure to modify REDIS_PORT accordingly to the port you are using. Both the pid file path and the configuration file name depend on the port number. Copy the template configuration file you’ll find in the root directory of the Redis distribution into /etc/redis/ using the port number as name, for instance:

sudo cp redis.conf /etc/redis/6379.conf

Create a directory inside /var/redis that will work as data and working directory for this Redis instance:

sudo mkdir /var/redis/6379

Edit the configuration file, making sure to perform the following changes: Set daemonize to yes (by default it is set to no).

Set the pidfile to /var/run/redis_6379.pid (modify the port if needed). Change the port accordingly. In our example it is not needed as the default port is already 6379. Set your preferred loglevel. Set the logfile to /var/log/redis_6379.log

Set the dir to /var/redis/6379 (very important step!). Finally add the new Redis init script to all the default runlevels using the following command:

sudo update-rc.d redis_6379 defaults

You are done! Now you can try running your instance with:

/etc/init.d/redis_6379 start

Make sure that everything is working as expected:

Try pinging your instance with redis-cli. Do a test save with redis-cli save and check that the dump file is correctly stored into /var/redis/6379/ (you should find a file called dump.rdb).

Check that your Redis instance is correctly logging in the log file. If it’s a new machine where you can try it without problems make sure that after a reboot everything is still working.

We can also do a quick check by running the Redis Benchmark. It will not only stress test Redis itself but also verify that there’s nothing wrong with our installation. The following will issue 100.000 requests from 50 clients sending 12 commands at the same time.

$ redis-benchmark -q -n 100000 -c 50 -P 12

For advanced configuration you can visit below link

http://blog.sensible.io/2013/08/20/setting-up-redis-for-production-environment.html

Working with java

There are many redis library available for java. Jedis, JDBC-Redis and JRedis .  I did some reading and found out that jedis is a better Java client for Redis. I tried it out and it works perfectly and it is also very simple to use.

I am going to create a example for jedis. Now create a java project in eclipse ide and put the name JavaRedisExample. 

Step 1 : Create a fresh java project JavaRedisExample

Step2 : Create a package com.redis.manager

Step3 : Create a java class RedisManager

package com.redis.manager;

/*
 * Author: radhey
 * Date: 21-Dec-2013
 * Last Date of Modification: 21-Dec-2013 1:36:37 PM
 * Comments:
 * Version: 1.0
 *
 */
 import redis.clients.jedis.Jedis;
 import redis.clients.jedis.JedisPool;
 import redis.clients.jedis.JedisPoolConfig;
public class RedisManager {
 private static final RedisManager instance = new RedisManager();
 private static JedisPool pool;
 private RedisManager() {}
 public final static RedisManager getInstance() {
 return instance;
 }
 public void connect() {
 // Create and set a JedisPoolConfig
 JedisPoolConfig poolConfig = new JedisPoolConfig();
 // Maximum active connections to Redis instance
 poolConfig.setMaxActive(20);
 // Tests whether connection is dead when connection
 // retrieval method is called
 poolConfig.setTestOnBorrow(true);
 /* Some extra configuration */
 // Tests whether connection is dead when returning a
 // connection to the pool
 poolConfig.setTestOnReturn(true);
 // Number of connections to Redis that just sit there
 // and do nothing
 poolConfig.setMaxIdle(5);
 // Minimum number of idle connections to Redis
 // These can be seen as always open and ready to serve
 poolConfig.setMinIdle(1);
 // Tests whether connections are dead during idle periods
 poolConfig.setTestWhileIdle(true);
 // Maximum number of connections to test in each idle check
 poolConfig.setNumTestsPerEvictionRun(10);
 // Idle connection checking period
 poolConfig.setTimeBetweenEvictionRunsMillis(60000);
 // Create the jedisPool
 pool = new JedisPool(poolConfig, "localhost", 6379);
 }
 public void release() {
 pool.destroy();
 }
 public Jedis getJedis() {
 return pool.getResource();
 }
 public void returnJedis(Jedis jedis) {
 pool.returnResource(jedis);
 }
 }

Step 4:  Create a test package com.redis.test and then create a java class RedisTest

/*
 * Author: radhey
 * Date: 21-Dec-2013
 * Last Date of Modification: 21-Dec-2013 1:36:37 PM
 * Comments: 
 * Version: 1.0
 * 
 */
package com.redis.test;

import redis.clients.jedis.Jedis;
import com.redis.manager.RedisManager;

public class RedisTest {

 public static void main(String[] args) {
  RedisManager.getInstance().connect();
  Jedis jedis = RedisManager.getInstance().getJedis();
  jedis.set("name", "radhey");
  String val = jedis.get("name");
  System.out.println(val);
  RedisManager.getInstance().release();
}
}

 Step 5 : You need jedis library for this test project. You can download the same from         below git repository.

https://github.com/itthought/noSQL-Test/tree/master/JavaRedisExample

Redis with Spring

Step1 : create a maven project named “SpringRedisExample”

Step2 : add dependency in pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>SpringRedisExample</groupId>
 <artifactId>SpringRedisExample</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <properties>
 <project.build.sourceencoding>UTF-8</project.build.sourceencoding>
 <spring.version>3.1.0.RELEASE</spring.version>
 </properties>
 <build>
 <sourceDirectory>src</sourceDirectory>
 <resources>
 <resource>
 <directory>config</directory>
 <excludes>
 <exclude>**/*.java</exclude>
 </excludes>
 </resource>
 </resources>
 <plugins>
 <plugin>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>2.3.2</version>
 <configuration>
 <source>1.7</source>
 <target>1.7</target>
 </configuration>
 </plugin>
 </plugins>
 </build>
 <dependencies>
 <dependency>
 <groupId>org.springframework.data</groupId>
 <artifactId>spring-data-redis</artifactId>
 <version>1.0.0.RELEASE</version>
 </dependency>
<dependency>
 <groupId>cglib</groupId>
 <artifactId>cglib-nodep</artifactId>
 <version>2.2</version>
 </dependency>
<dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.16</version>
 </dependency>
<dependency>
 <groupId>redis.clients</groupId>
 <artifactId>jedis</artifactId>
 <version>2.0.0</version>
 <type>jar</type>
 </dependency>
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>${spring.version}</version>
 </dependency>
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>${spring.version}</version>
 </dependency>
 </dependencies>
</project>

Step 3 : Create a resource folder and named it config. Then create a property file redis.properties

redis.serverHost=localhost
redis.serverPort=6379
redis.password=

Then create a redisConfig.xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

 <context:property-placeholder location="classpath:redis.properties"/>
 <bean id="connectionFactory"
 class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
 p:host-name="${redis.serverHost}" p:port="${redis.serverPort}" p:password="${redis.password}" />

 <!-- redis template -->
 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
 p:connection-factory-ref="connectionFactory" />
 <context:annotation-config />

 <bean id="example" class="com.test.redis.Example">
 <property name="redisTemplate" ref="redisTemplate"/>
 </bean>
</beans>

Step 4: Create a test class Example.java

/*
 * Author: radhey
 * Date: 21-Dec-2013
 * Last Date of Modification: 21-Dec-2013 4:03:28 PM
 * Comments: 
 * Version: 1.0
 * 
 */
package com.test.redis;
import java.net.MalformedURLException;
import java.net.URL;
import org.springframework.data.redis.core.RedisTemplate;
public class Example {
private RedisTemplate<String, String> redisTemplate;
public RedisTemplate<String, String> getRedisTemplate() {
 return redisTemplate;
 }
public void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
 this.redisTemplate = redisTemplate;
 }
public void addLink(String userId, URL url) {
 redisTemplate.boundListOps(userId).leftPush(url.toExternalForm());
 }
public URL getLink(String userId) {
 try {
 URL url = new URL(redisTemplate.boundListOps(userId).rightPop());
 return url;
 } catch (MalformedURLException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 return null;
 }
}

Then create a test class HelloWorld.java

package com.test.redis;
import java.net.MalformedURLException;
import java.net.URL;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloWorld {
public static void main(String[] args) {
 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
 "redisConfig.xml");
 Example eg = (Example) applicationContext.getBean("example");
 try {
 URL url = new URL("http://localhost:8888");
 eg.addLink("local", url);
 URL url1 = eg.getLink("local");
 System.out.println(url1.toString());
} catch (MalformedURLException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
}
}

Step 5: Run the class HelloWorld. You will get below result

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
http://localhost:8888

you can download this example from the https://github.com/itthought/noSQL-Test/tree/master/RedisExample

Using NoSQL database In Java (memcached)

Aside

NoSQL

NoSQL database, also called Not Only SQL, is an approach to data management and database design that’s useful for very large sets of distributed data. 

NoSQL, which encompasses a wide range of technologies and architectures, seeks to solve the scalability and big data performance issues that relational databases weren’t designed to address. NoSQL is especially useful when an enterprise needs to access and analyze massive amounts of unstructured data or data that’s stored remotely on multiple virtual servers in the cloud.

Motivations for this approach include simplicity of design, horizontal scaling and finer control over availability. NoSQL databases are often highly optimized key–value stores intended for simple retrieval and appending operations, with the goal being significant performance benefits in terms of latency and throughput.

There is around 150 NoSql database available in the market. Major of them are as below

Wide Column Store / Column Families:  Hadoop/Hbase,Cassandra,hipertable,accumulo,Amazon SimpleDB,cloudata etc

Document Store:  MongoDB , Elasticsearch,couchbase Server,couchDB etc

Key Value / Tuple Store :  DynamoDB, Redis , Raik (API:json,Portocol: REST), AeroSpike, memcacheDB etc

Graph Databases:  Neo4j,Infinite Graph,infoGrid,HyperGraphDB etc

Multimodel Databases:

  ArangoDB, OrientDb, Datomic etc

Memcached Installation and Configuration

Memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Traditional Java caches use a replication scheme which makes them very difficult to configure and manage, resulting in number of synchronization and memory leak issues. With Memcached data can be distributed individually across multiple servers without replication. Additionally, deployment requires little or no configuration allowing for maximum scalability and performance with the least effort from your part

How to Install

For Debian-based distributions like Ubuntu, simply type the following command on the terminal:   

sudo apt-get install memcached

On RedHat, CentOS:  

 yum install memcached

If you got error during installation use below steps

1. Install Libevent

Memcached uses the Libevent library for network IO.

$ cd libevent-1.4.11-stable
$ autoconf
$ ./configure --prefix=/usr/local
$ make
$ sudo make install

2. Install Memcached:
Download the latest version of Memcached from 
Danga.com who developed Memcached originally for Livejournal.

$ cd memcached-1.4.0
$ autoconf
$ ./configure --prefix=/usr/local
$ make
$ sudo make install

Once the Memcached server has been installed you may start it by typing memcached at the command line. When you execute memcached without any command-line options, the Memcached server will start up on port 11211 with 64 MB of memory by default. You can achieve greater control over how to start Memcached such as changing the port or running as a daemon. Use the help option to view all the available command line options: 

memcached –help

Testing the installation.

For debugging purposes, it is useful to use telnet to fire commands against Memcached. Start telnet on your localhost connecting to port 11211 as fallows:

To start memcached on localhost and on port 1121 with maximum 32 MB of main memory as the root user:

memcached -u root -m 32 127.0.0.1 -p 1121
telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
set my_key 0 60 1 
1
STORED
get my_key
VALUE my_key 0 1
1
END

If at any time you wish to terminate the Telnet session, simply type “quit” and hit return:

quit
Connection closed by foreign host.

For more commands follow below links  click here

To access the cache from a Java application, first we need to open a connection to a memcached process:

try {
   client = new MemcachedClient(new InetSocketAddress(hostname, port));
} catch (IOException e) {
   e.printStackTrace();
   System.err.println("connection problem");
}

After creating a MemcachedClient, we can now perform operations on the items in the cache. Each item in the cache is associated with a key and an expiration time value. Setting an item in the cache is straightforward, all we need to specify is a key,the corresponding value and an expiration time after which the item will be evicted from the cache. The set operation is asynchronous in the memcached library:

client.set(key, expiration, val);

To get an item, we specify its key. It is possible to perform both synchronous and asynchronous gets with the memcached library. For example

try {
   retrieved = f.get(GET_TIMEOUT, TimeUnit.SECONDS);
  // async get,  timeout after  GET_TIMEOUT  seconds
  // throws InterruptedException, ExecutionException or TimeoutException
} catch (Exception e) {
   // process timeout
   f.cancel(true);// cancel the task
}

Sample Java code to test memcached

import java.io.IOException;
import java.net.InetSocketAddress;
import net.spy.memcached.MemcachedClient;

public class MemcachedTestClient {

 public static void main(String[] args) {
 //initialize Memcached Server Connection
 InetSocketAddress address=new InetSocketAddress("localhost", 11211);

 try {
 MemcachedClient mcc = new MemcachedClient(address);
 mcc.add("someKey", 3600, "Radhey");
 // Retrieve a value (synchronously).
 System.out.println(mcc.get("someKey"));
 //To change value
 mcc.set("someKey", 3600, "Shyam"); 
 System.out.println(mcc.get("someKey"));

 //To delete
 mcc.delete("someKey"); 
 System.out.println(mcc.get("someKey"));

 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } 
 }
}

Add memcached-2.6.jar file in lib folder if you are getting error. Download

Using memcache with Spring and maven

memcahce

Follow the below steps

Step 1 : Create a maven java project and put the project name what ever you want. In my case i am using MemcacheExample.

Step 2: In The pom.xml file add the below dependency

<dependencies>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring</artifactId>
 <version>2.5.6.SEC03</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-asm</artifactId>
 <version>3.1.4.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-beans</artifactId>
 <version>3.2.3.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>3.2.3.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>3.2.3.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-tx</artifactId>
 <version>3.2.3.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-expression</artifactId>
 <version>3.2.3.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>com.danga</groupId>
 <artifactId>memcached</artifactId>
 <version>2.0.1</version>
 </dependency>
 <dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-log4j12</artifactId>
 <version>1.5.6</version>
 <type>jar</type>
 <scope>compile</scope>
 </dependency>
 </dependencies>

Step 3: Create a resource folder and named it config

Step 4: Create a memcache.properties file in config folder and paste below contents into the file.

memcache.poolname=POOL
memcache.server.ip=localhost
memcache.server.port=11211
memcache.initConn=5
memcache.minConn=5
memcache.maxConn=250
memcache.maxIdle=21600000
memcache.socketTO=3000
memcache.socketConnectTO=0
memcache.nagle=false
memcache.maintSleep=30

Step 5: Create xml file having name of memcacheConfig.xml for spring and memcache configuration. And paste below contents into  this.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
 <context:property-placeholder location="classpath:memcache.properties"/>
 <bean id="memcachedSockIOPool" class="com.danga.MemCached.SockIOPool" init-method="initialize" factory-method="getInstance" autowire-candidate="true">
 <constructor-arg type="java.lang.String" value="${memcache.poolname}"/>
 <property name="servers">
 <list>
 <value type="java.lang.String">${memcache.server.ip}:${memcache.server.port}</value>
 </list>
 </property>
 <property name="initConn" value="${memcache.initConn}"/>
 <property name="minConn" value="${memcache.minConn}"/>
 <property name="maxConn" value="${memcache.maxConn}"/>
 <property name="maxIdle" value="${memcache.maxIdle}"/>
 <property name="socketTO" value="${memcache.socketTO}"/>
 <property name="socketConnectTO" value="${memcache.socketConnectTO}"/>
 <property name="nagle" value="${memcache.nagle}"/>
 <property name="maintSleep" value="${memcache.maintSleep}"/>
 </bean> 
 <bean id="memcache-initialize" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
 <property name="targetObject"><ref local="memcachedSockIOPool"/></property>
 <property name="targetMethod"><value>initialize</value></property>
 </bean>
 <context:annotation-config />
</beans>

Step 6: Create a package com.radhey.memcache.manager for creating class for memcache manager.

Step 7: create a class MemcachedManager.java for handing memcache service

package com.radhey.memcache.manager;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.danga.MemCached.MemCachedClient;
@Component
@Scope("prototype")
public class MemcachedManager {
 private MemCachedClient memCachedClient;

 public MemCachedClient getMemCachedClient() {
 return memCachedClient;
 }

 public void setMemCachedClient(MemCachedClient memCachedClient) {
 this.memCachedClient = memCachedClient;
 }

 protected Object get(String key){
 if ((key == null) || (key.trim().equals(""))){
 System.out.println("Get attempt against null or empty key.Returning null value.");
 return null;
 }
 System.out.println("Memcache key = " + key);
 return memCachedClient.get(key);
 }

 protected boolean add(String key, Object value){
 boolean status = false;
 if ((key == null) || (key.trim().equals(""))){
 System.out.println("Add attempt against null or empty key.");
 return status;
 }

 if (value == null){
 System.out.println("Add attempt of null value against key = " + key);
 return status;
 }

 System.out.println("Memcache key = " + key);

 if (get(key) != null){
 System.out.println("Add attempt of value which is already present in memcache against key = " + key);
 status = replace(key, value);
 return status;
 }else 
 status = memCachedClient.add(key, value);

 if (status){
 System.out.println("Successfully added value against key = " + key);
 //addMemcacheKeyToMaster(key);
 }else {
 System.out.println("Failed to add value against key = " + key);
 }
 return status;
 }

 protected boolean add(String key, Object value, Date date){
 boolean status = false;
 if ((key == null) || (key.trim().equals(""))){
 System.out.println("Add attempt against null or empty key.");
 return status;
 }

 if (value == null){
 System.out.println("Add attempt of null value against key = " + key);
 return status;
 }

 System.out.println("Memcache key = " + key);

 if (get(key) != null){
 System.out.println("Add attempt of value which is already present in memcache against key = " + key);
 System.out.println("Call delete or replace");
 return status;
 }

 status = memCachedClient.add(key, value, date);

 if (status){
 System.out.println("Successfully added value against key = " + key);
 //addMemcacheKeyToMaster(key);
 }else {
 System.out.println("Failed to add value against key = " + key);
 }
 return status;
 }

 protected boolean delete(String key){
 boolean status = false;
 if ((key == null) || (key.trim().equals(""))){
 System.out.println("Delete attempt against null or empty key.");
 return status;
 }

 if (get(key) == null){
 System.out.println("Delete attempt of value which is not present in memcache against key = " + key);
 return status;
 }

 System.out.println("Memcache key = " + key);

 status = memCachedClient.delete(key);
 if (status){
 System.out.println("Successfully deleted value against key = " + key);
 //deleteMemcacheKeyFromMaster(key);
 }else {
 System.out.println("Failed to delete value against key = " + key);
 }
 return status;
 }

 protected boolean replace(String key, Object value){
 boolean status = false;
 if ((key == null) || (key.trim().equals(""))){
 System.out.println("Replace attempt against null or empty key.");
 return status;
 }

 System.out.println("Memcache key = " + key);

 status = memCachedClient.replace(key, value);
 if (status){
 System.out.println("Successfully replaced value against key = " + key);
 }else {
 System.out.println("Failed to replace value against key = " + key);
 }
 return status;
 }

 protected boolean replace(String key, Object value, Date date){
 boolean status = false;
 if ((key == null) || (key.trim().equals(""))){
 System.out.println("Replace attempt against null or empty key.");
 return status;
 }

 System.out.println("Memcache key = " + key);

 status = memCachedClient.replace(key, value, date);
 if (status){
 System.out.println("Successfully replaced value against key = " + key);
 }else {
 System.out.println("Failed to replace value against key = " + key);
 }
 return status;
 }

 public boolean flushAll(){
 boolean status = false;
 status = memCachedClient.flushAll();
 if (status){
 System.out.println("Successfully flushed all memcached data");
 }else {
 System.out.println("Failed to flush all memcached data");
 }
 return status;
 }

 /**
 * Returns the running statistics for MemCache.
 * @return Map
 */
 public Map showStats(){
 Map statsMap = memCachedClient.stats();
 Map stats = null;
 if(statsMap != null && statsMap.keySet().size() > 0) {
 Set keys = statsMap.keySet();
 Iterator ite = keys.iterator();
 while(ite.hasNext()){
 String key = (String)ite.next();
 stats = (Map)statsMap.get(key);
 }
 return stats;
 }else
 return null;
 }

 protected Object getMulti(String[] keyList){
 if ((keyList == null) || keyList.length==0){
 System.out.println("Get attempt against null or empty key.Returning null value.");
 return null;
 }
 System.out.println("Memcache key = " + keyList);
 return (Object)memCachedClient.getMulti(keyList);
 }
}

Step 8:  Create an implementation class that instantiate memcache client. TestMemcachedManager.java

package com.radhey.memcache.manager;

import org.springframework.stereotype.Component;
import com.danga.MemCached.MemCachedClient;

@Component
public class TestMemcachedManager extends MemcachedManager {
public TestMemcachedManager(){
super(); 
MemCachedClient memCachedClient = new MemCachedClient("POOL");
super.setMemCachedClient(memCachedClient);
 }
}

Step 9:  Create a package  com.radhey.memcache.service, then create a java file CacheService.java. This class entends TestMemcachedManager

package com.radhey.memcache.service;

import org.springframework.stereotype.Component;
import com.radhey.memcache.manager.TestMemcachedManager;
@Component
public class CacheService extends TestMemcachedManager {

public void loadCache() {
loadDataInCatch();
 fetchValue();
 }

public void loadDataInCatch() {
 boolean isAdded = add("Key1", "radhey");
 if (isAdded)
 System.out.println("Added success fully");
if (!isAdded)
 System.out.println("Error in addition fully");
 }

 public void fetchValue() {
 String value = (String) get("Key1");
 if(value!=null && !value.equalsIgnoreCase(""))
 {
 //value fetch from catched
 System.out.println("catched value:" +value);
 }
 else
 {
 // something wrong with catch. Fetch data from db
 System.out.println("something wrong with catch. Fetch data from db");
 }
}

public void updateCatchValue(String key,String value) 
 {
 boolean isAdded = replace(key, "Shyam");
 if (isAdded)
 System.out.println("replaced success fully");
 if (!isAdded)
 System.out.println("Error in replacing fully");
 }
}

Step 10:  Create a test package com.radhey.test . Then create a MemcacheTest.java class

package com.radhey.test;

import java.util.logging.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.radhey.memcache.service.CacheService;
public class MemcacheTest {
 static Logger log = Logger.getLogger(MemcacheTest.class.getName());
 public static void main(String[] args) {
 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("memcacheConfig.xml");
 CacheService cacheService=new CacheService();
 cacheService.loadCache();
 cacheService.updateCatchValue("Key1", "Shyam");
 cacheService.fetchValue();
 }
}

Step 11 : run the MemcacheTest class if every thing work fine then you will get the below output.

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Memcache key = Key1
Memcache key = Key1
Add attempt of value which is already present in memcache against key = Key1
Memcache key = Key1
Successfully replaced value against key = Key1
Added success fully
Memcache key = Key1
catched value:radhey
Memcache key = Key1
Successfully replaced value against key = Key1
replaced success fully
Memcache key = Key1
catched value:Shyam
you can checkout above example from github
https://github.com/itthought/noSQL-Test/tree/master/MemcacheExample

Merit and Demerit of using NVARCHAR2 in oracle

In my current project. We are using oracle in back-end. We have a table having field NVARCHAR2  datatype and that table are most frequently used table in code base. We are doing operation like insert, update and select on this table in parallel. We have applied index key on this fields(nvarchar2). We are facing serious issue in the performance. Its working fine but some time behave unexpectedly.  After doing lot of RND and raising SR ticket to oracle and doing lot of googling. We found out that NVARCHAR2 is culprit. After changing datatype form NVARCHAR2 to VARCHAR2. Our problem solved.

Here is nice blog written by Joze  “CBO oddities in determining selectivity on NVARCHAR2 data type“.

Please go through this block if you are going to use nvarchar2 in future in your table schema.

CBO oddities in determining selectivity on NVARCHAR2 data type

Working with PostgreSQL (installation, using with java)

PostgreSQL is a powerful open source object-relational database system. PostgreSQL (pronounced as post-gress-Q-L) is an open source relational database management system ( DBMS ) developed by a worldwide team of volunteers. PostgreSQL is not controlled by any corporation or other private entity and the source code is available free of charge.

Key features of PostgreSQL

PostgreSQL runs on all major operating systems, including Linux, UNIX (AIX, BSD, HP-UX, SGI IRIX, Mac OS X, Solaris, Tru64), and Windows. It supports text, images, sounds, and video, and includes programming interfaces for C / C++ , Java , Perl , Python , Ruby, Tcl and Open Database Connectivity (ODBC).

PostgreSQL supports a large part of the SQL standard and offers many modern features including (Complex SQL queries,SQL Sub-selects, Foreign keys,Trigger,Views,Transactions, Multiversion concurrency control (MVCC),
Streaming Replication and Hot Standby

Installation on linux system (ubuntu) . Please refer below link for this

HOW TO INSTALL POSTGRESQL 9.1 ON UBUNTU 13.04 LINUX

you can also get help in installation from this link Installation help

Working with java and postgreSQL

if you are working with maven then you need below dependency for postgre jdbc driver

<dependencies>
 <dependency>
 <groupId>postgresql</groupId>
 <artifactId>postgresql</artifactId>
 <version>9.1-901.jdbc4</version>
 </dependency>
 </dependencies>

For JDBC connectivity use the below steps

Step 1)        Registering PostgreSQL JDBC Driver

try {
Class.forName("org.postgresql.Driver");
 } catch (ClassNotFoundException e) {
 System.out.println("Where is your PostgreSQL JDBC Driver? " + "Include in your library path!");
e.printStackTrace();
}
If you are not using maven project then need to put postgresql api file in lib or classpath. You can download postgre driver from download driver

Step 2)     After registering driver, next step is creating connection with database. This is                    very easy and similar with creating connection with mysql/oracle.

Connection connection = null;
try {
 connection = DriverManager.getConnection("jdbc:postgresql://<host>:<port>/<database>", <user>,<password>);
 } catch (SQLException e) {
 System.out.println("Connection Failed! Check output console");
 e.printStackTrace();
}

         

Below are some example of using postgreSQL with java;

import java.sql.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class PostgreSQLJDBC {
   public static void main( String args[] )
     {
       Connection c = null;
       Statement stmt = null;
       try {
         Class.forName("org.postgresql.Driver");
         c = DriverManager
            .getConnection("jdbc:postgresql://localhost:5432/test",
            "root", "pass");
         System.out.println("Opened database successfully");

         stmt = c.createStatement();
         String sql = "CREATE TABLE COMPANY " +
                      "(ID INT PRIMARY KEY     NOT NULL," +
                      " NAME           TEXT    NOT NULL, " +
                      " AGE            INT     NOT NULL, " +
                      " ADDRESS        CHAR(50), " +
                      " SALARY         REAL)";
         stmt.executeUpdate(sql);
         stmt.close();
         c.close();
       } catch ( Exception e ) {
         System.err.println( e.getClass().getName()+": "+ e.getMessage() );
         System.exit(0);
       }
       System.out.println("Table created successfully");
     }
}
——————————————————————————
Using Insert Query
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class PostgreSQLJDBC {
   public static void main(String args[]) {
      Connection c = null;
      Statement stmt = null;
      try {
         Class.forName("org.postgresql.Driver");
         c = DriverManager
            .getConnection("jdbc:postgresql://localhost:5432/test",
            "root", "pass");
         c.setAutoCommit(false);
         System.out.println("Opened database successfully");

         stmt = c.createStatement();
         String sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "
               + "VALUES (1, 'radhey', 30, 'noida', 20000.00 );";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "
               + "VALUES (2, 'shyam', 30, 'delhi', 15000.00 );";
         stmt.executeUpdate(sql);

         stmt.close();
         c.commit();
         c.close();
      } catch (Exception e) {
         System.err.println( e.getClass().getName()+": "+ e.getMessage() );
         System.exit(0);
      }
      System.out.println("Records created successfully");
   }
}
Select Operation
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class PostgreSQLJDBC {
   public static void main( String args[] )
     {
       Connection c = null;
       Statement stmt = null;
       try {
       Class.forName("org.postgresql.Driver");
         c = DriverManager
            .getConnection("jdbc:postgresql://localhost:5432/test",
            "root", "pass");
         c.setAutoCommit(false);
         System.out.println("Opened database successfully");         stmt = c.createStatement();
         ResultSet rs = stmt.executeQuery( "SELECT * FROM COMPANY;" );
         while ( rs.next() ) {
            int id = rs.getInt("id");
            String  name = rs.getString("name");
            int age  = rs.getInt("age");
            String  address = rs.getString("address");
            float salary = rs.getFloat("salary");
            System.out.println( "ID = " + id );
            System.out.println( "NAME = " + name );
            System.out.println( "AGE = " + age );
            System.out.println( "ADDRESS = " + address );
            System.out.println( "SALARY = " + salary );
            System.out.println();
         }
         rs.close();
         stmt.close();
         c.close();
       } catch ( Exception e ) {
         System.err.println( e.getClass().getName()+": "+ e.getMessage() );
         System.exit(0);
       }
       System.out.println("Operation done successfully");
     }
}

Using postgreSql with JPA and ORM (hibernate, eclipseLink etc). Below are the some light on ORM api.

Hibernate is an object-relational mapping (ORM) library for the Java language, providing a framework for mapping an object-orienteddomain model to a traditional relational database. Hibernate solves object-relational impedance mismatch problems by replacing directpersistence-related database accesses with high-level object handling functions.

EclipseLink is the open source Eclipse Persistence Services Project from the Eclipse Foundation. The software provides an extensible framework that allows Java developers to interact with various data services, including databases, web services, Object XML mapping (OXM), and Enterprise Information Systems (EIS). EclipseLink supports a number of persistence standards including:

EclipseLink is based on the TopLink product from which Oracle contributed the source code to create the EclipseLink project

If you want to know and in a position, not able to decide which one is best for your project eclipseLink or hibernate, go through the below link

Refrence link1      Refrence link2

Oh sorry i am out of context. We are going to see how to use postgreSQL with eclipseLink and hibernate. Below are the some easy steps for this. First we see how to use eclipseLink

Step 1)        Create a maven java project in eclipse

maven

Step 2)     Create a META-INF folder in src/main/resource directory of your project.

after creating folder META-INF, create a persistence.xml file in this folder for configuration of eclipse link.

  <persistence-unit name=”test” transaction-type=”RESOURCE_LOCAL”>
    <class>com.test.jpa.Student</class>
    <properties>
      <property name=”javax.persistence.jdbc.driver” value=”org.postgresql.Driver” />
      <property name=”javax.persistence.jdbc.url”    value=”jdbc:postgresql://localhost:5432/test” />
      <property name=”javax.persistence.jdbc.user” value=”root” />
      <property name=”javax.persistence.jdbc.password” value=”pass” />
      <property name=”eclipselink.ddl-generation” value=”create-tables” />
      <property name=”eclipselink.ddl-generation.output-mode” value=”database” />
    </properties>
  </persistence-unit>
</persistence>

Step 3)      Now create a entity manger class

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class EntityManagerUtil {
private static final EntityManagerFactory entityManagerFactory;
  static {
    try {
      entityManagerFactory = Persistence.createEntityManagerFactory("test");
    } catch (Throwable ex) {
      System.err.println("Initial SessionFactory creation failed." + ex);
      throw new ExceptionInInitializerError(ex);
    }
 }
  public static EntityManager getEntityManager() {
    return entityManagerFactory.createEntityManager();
  }
}
Step 4)     Now you notice that you got many error in EntityManagerUtil class. Dont worry                  you need to add dependency for eclipseLink and postgreSQL. Add following                        dependency in your pom.xml
<dependencies>
 <dependency>
  <groupId>postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>9.1-901.jdbc4</version>
 </dependency>
<dependency>
 <groupId>org.eclipse.persistence</groupId>
 <artifactId>eclipselink</artifactId>
 <version>2.4.1</version>
</dependency>
<dependency>
 <groupId>org.eclipse.persistence</groupId>
 <artifactId>javax.persistence</artifactId>
<version>2.0.3</version>
</dependency>
</dependencies>
you also need to add repository in pom. Eclipselink lib not available on maven site. So add repository
<repositories>
  <repository>
    <id>EclipseLink Repo</id>
     <url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
     <name>EclipseLink Repo</name>
  </repository>
<repository>
   <id>java.net</id>
   <name>java.net</name>
   <url>http://download.java.net/maven/2/</url>
   </repository>
   <repository>
   <id>eaio.com</id>
    <url>http://eaio.com/maven2</url>
 </repository>
</repositories>


Collectively your pom look like
  <modelVersion>4.0.0</modelVersion>
  <groupId>PostgreTest</groupId>
  <artifactId>com.postgre</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <repositories>
<repository>
<id>EclipseLink Repo</id>
<name>EclipseLink Repo</name>
</repository>
<repository>
<id>java.net</id>
<name>java.net</name>
</repository>
<repository>
<id>eaio.com</id>
</repository>
</repositories>
  <name>PostgreTest</name>
  <dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.0.3</version>
</dependency>
</dependencies>
</project>
Step 5)       Create entity class Student
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = “STUDENT”)
public class Student implements java.io.Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = “STUDENTID”)
  private long studentId;
  @Column(name = “STUDENTNAME”)
  private String studentName;
  public void setStudentId(long studentId) {
    this.studentId = studentId;
  }
  public long getStudentId() {
    return studentId;
  }
  public void setStudentName(String studentName) {
    this.studentName = studentName;
  }
  public String getStudentName() {
    return studentName;
  }
}
Step 6)   Create a class for testing your code. Create a class EclipseLinkExample
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
public class EclipseLinkExample {
 private EntityManager entityManager = EntityManagerUtil.getEntityManager();
 public static void main(String[] args) {
   EclipseLinkExample example = new EclipseLinkExample();
   System.out.println(“After Sucessfully insertion “);
   Student student1 = example.saveStudent(“Sumith”);
   Student student2 = example.saveStudent(“Anoop”);
   example.listStudent();
   System.out.println(“After Sucessfully modification “);
   example.updateStudent(student1.getStudentId(), “Sumith Honai”);
   example.updateStudent(student2.getStudentId(), “Anoop Pavanai”);
   example.listStudent();
   System.out.println(“After Sucessfully deletion “);
   example.deleteStudent(student2.getStudentId());
   example.listStudent();
 }
 public Student saveStudent(String studentName) {
   Student student = new Student();
   try {
     entityManager.getTransaction().begin();
     student.setStudentName(studentName);
     student = entityManager.merge(student);
     entityManager.getTransaction().commit();
   } catch (Exception e) {
     entityManager.getTransaction().rollback();
   }
   return student;
 }
 public void listStudent() {
   try {
     entityManager.getTransaction().begin();
     @SuppressWarnings(“unchecked”)
     List<Student> Students = entityManager.createQuery(“from Student”).getResultList();
     for (Iterator<Student> iterator = Students.iterator(); iterator.hasNext();) {
       Student student = (Student) iterator.next();
       System.out.println(student.getStudentName());
     }
     entityManager.getTransaction().commit();
   } catch (Exception e) {
     entityManager.getTransaction().rollback();
   }
 }
 public void updateStudent(Long studentId, String studentName) {
   try {
     entityManager.getTransaction().begin();
     Student student = (Student) entityManager.find(Student.class, studentId);
     student.setStudentName(studentName);
     entityManager.getTransaction().commit();
   } catch (Exception e) {
     entityManager.getTransaction().rollback();
   }
 }
 public void deleteStudent(Long studentId) {
   try {
     entityManager.getTransaction().begin();
     Student student = (Student) entityManager.find(Student.class, studentId);
     entityManager.remove(student);
     entityManager.getTransaction().commit();
   } catch (Exception e) {
     entityManager.getTransaction().rollback();
   }
 }
}

Step 7)           Run the class (EclipseLinkExample) . Hurre you have created first                                      eclipseLink project with postgreSQL. After running your code you will got                            below response.

EL Info]: 2013-09-11 11:44:57.03–ServerSession(1373297288)–EclipseLink, version: Eclipse Persistence Services – 2.4.1.v20121003-ad44345
[EL Info]: connection: 2013-09-11 11:44:57.173–ServerSession(1373297288)–file:/home/radhey/WalletWorkspaceNew/com.postgre/target/classes/_test login successful
After Sucessfully insertion
After Sucessfully modification
After Sucessfully deletion

——————————————————————————————————————–

Now for connecting with hibernate please go through the below link. This in nice blog having easy steps for hibernate and postgreSQL configuration.

 Using hibernate with postgreSQL

Thanks you all. I feel very happy if this post going to help others.

JAVA/Maven Configuration on Linux

Install Java JDK in Linux 

Hello all, This is my first post, this might be going to help to those who is new for linux. Few month ago, i just started using linux and faced some difficulty in jdk and maven configuration. After doing googling i found following easy steps to do jdk/maven configuration

Step1: open terminal and type cd /tmp

 Step2:  download jdk

wget http://download.oracle.com/otn-pub/java/jdk/7u25-b15/jdk-7u25-linux-x64.tar.gz

OR download from below link
 http://www.oracle.com/technetwork/java/javase/downloads/index.html
Step3: Unzip and move it to the location where you want to install. In my case i am using  /usr/lib/jvm/jdk1.7.0
tar -xvf jdk-7u25-linux-x64.tar.gz
sudo mkdir -p /usr/lib/jvm/jdk1.7.0
sudo mv /tmp/jdk1.7.0 /usr/lib/jvm/jdk1.7.0

Step4: Now copy and paste one at a time in terminal to enable java

      For java

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7.0/bin/java" 1

     For javac

sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0/bin/javac" 1

      For javaw

sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.7.0/bin/javaws" 1

     And for appletviewer

sudo update-alternatives --install "/usr/bin/appletviewer" "appletviewer" "/usr/lib/jvm/jdk1.7.0/bin/appletviewer" 1

Step5: Use below command for verification

java -version
javac -version

Installing and configuring maven

Step1: Download maven for apache site http://maven.apache.org/download.cgi

Or use below command.

cd /tmp/

wget http://apache.claz.org/maven/maven-3/3.0.5/binaries/apache-maven-3.0.5-bin.tar.gz

Step2:  unzip tar file then move it to /usr/local/apache-maven-3.0.5

tar -xvf  apache-maven-3.0.5-bin.tar.gz
sudo mv apache-maven-3.0.5 /usr/local/apache-maven-3.0.5

Step3: For configuring maven path use below steps

sudo gedit ~/.bashrc

add below line at the end of file.

export M2_HOME=/usr/local/apache-maven-3.0.5
export M2=$M2_HOME/bin
export PATH=$M2:$PATH

Step4: Save and close bash file

Step5: reload bash file using below command

source ~/.bashrc

Step6: check the done configuration

mvn -version