Saturday, 29 August 2015

Backups and Restore data In Cassandra

 Cassandra backs up data by taking a snapshot of all on-disk data files (SSTable files) stored in the data directory. You can take a snapshot of all keyspaces, a single keyspace, or a single table while the system is online.

Using a parallel ssh tool (such as pssh), you can snapshot an entire cluster. This provides an eventually consistent backup. Although no one node is guaranteed to be consistent with its replica nodes at the time a snapshot is taken, a restored snapshot resumes consistency using Cassandra's built-in consistency mechanisms.
 
After a system-wide snapshot is performed, you can enable incremental backups on each node to backup data that has changed since the last snapshot: each time an SSTable is flushed, a hard link is copied into a /backups subdirectory of the data directory (provided JNA is enabled)

1. Taking a snapshot

Run the nodetool snapshot command, specifying the hostname, JMX port, and keyspace.
 
$ nodetool -h hostname -p jmx port snapshot mykeyspace

 For example:

$ nodetool -h localhost -p 7999  snapshot test


The snapshot is created in data_directory_location/keyspace_name/table_name/snapshots/snapshot_name directory. Each snapshot directory contains numerous .db files that contain the data at the time of the snapshot.

For example:

Packaged installs:

/var/lib/cassandra/data/mykeyspace/mytable/snapshots/23939834298/mykeyspace.db

Tarball installs:

install_location/data/data/mykeyspace/mytable/snapshots/23939834298/mykeyspace.db

Deleting snapshot files


To delete all snapshots for a node, run the nodetool clearsnapshot command. For example:
$ nodetool -h localhost -p 7199 clearsnapshot

Enabling incremental backups

Edit the cassandra.yaml configuration file on each node in the cluster and change the value of incremental_backups to true.


 Restoring from a Snapshot

1. Shut down the node.

2. Clear all files in the commitlog directory:
    
 Packaged installs: /var/lib/cassandra/commitlog
 Tarball installs: install_location/data/commitlog


3. Delete all *.db files in data_directory_location/keyspace_name/table_name directory, but DO NOT delete the /snapshots and /backups subdirectories. where data_directory_location is Packaged installs: /var/lib/cassandra/data and Tarball installs: install_location/data/data

4. Locate the most recent snapshot folder in this directory:
data_directory_location/keyspace_name/table_name/snapshots/snapshot_name

5. Copy its contents into this directory: data_directory_location/keyspace_name/table_name directory.

6. If using incremental backups, copy all contents of this directory:
data_directory_location/keyspace_name/table_name/backups

7. Paste it into this directory: data_directory_location/keyspace_name/table_name
8. Restart the node.

Related Posts :

Insert Data Into Cassandra Example

Insert Data Into Cassandra Example Set 2 

 Select Data From Cassandra Using Java



Java Memory Leak Cause

 In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations[1] in such a way that memory which is no longer needed is not released. In object-oriented
programming, a memory leak may happen when an object is stored in memory but cannot be accessed by the running code.[2] A memory leak has symptoms similar to a number of other problems (see below) and generally can only be diagnosed by a programmer with access to the program's source code. Here are the typical cause of  Memory Leak In Java:

1. Do not close DB, file, socket, JMS resources and other external resources properly.

2. Do not close resources properly when an exception is thrown.

3.  Keep adding objects to a cache or a hash map without expiring the old one.

3. Do not implement the hash and equal function correctly for the key to a cache

4. Leak in third party library or the application server

5. Bugs in the JDK.

6. In an infinite application code loop.

7. Leaking memory in the native code

9. Session data is too large.

 

Friday, 28 August 2015

java.io.IOException: Failed to retrieve RMIServer stub: javax.naming.CommunicationException in cassandra

 I had got this exception many times when i had snapshot from cassandra. i try with different ways but there was no solutions. After many try i goto cassandra.yaml file and change rcp_ip address  after that i restart cassandra and it works properly . I  things this problem occur if some ip configure have issue or net connection down.


java.io.IOException: Failed to retrieve RMIServer stub: javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
    java.net.ConnectException: Connection refused: connect]
    at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:338)
    at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:248)
    at my.code.RemoteSimonManagerFactoryImpl.createSimonManager(RemoteSimonManagerFactoryImpl.java:24)
    at my.code.Demo.main(DemoAggregation.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
    java.net.ConnectException: Connection refused: connect]
    at com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:101)
    at com.sun.jndi.toolkit.url.GenericURLContext.lookup(GenericURLContext.java:185)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at javax.management.remote.rmi.RMIConnector.findRMIServerJNDI(RMIConnector.java:1886)
    at javax.management.remote.rmi.RMIConnector.findRMIServer(RMIConnector.java:1856)
    at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:255)
    ... 9 more
Caused by: java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
    java.net.ConnectException: Connection refused: connect
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
    at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:97)


Related Posts

How do I delete all data in a Cassandra column family ?

Wednesday, 26 August 2015

How to delete a non-empty directory in Terminal?

rm -rf myfolder

It deletes all files and folders contained in the myfolder directory.

Add sudo at the beginning of the command :

In case user doesn't have the permission to delete the folder:

sudo rm -rf myfolder

Otherwise, without sudo you will be returned permission denied. And it's a good practice to try not to use -f while deleting a directory:

sudo rm -r myfolder

Note: this is assuming you are already on the same level of the folder you want to delete in terminal, if not:

sudo rm -r /path/to/myfolder
FYI: you can use letters -f, -r, -v:

-f = to ignore non-existent files, never prompt
-r = to remove directories and their contents recursively
-v = to explain what is being done

How do I delete all data in a Cassandra column family ?


We can truncate  all rows from cassandra tables using truncate


Example:

cqlsh: test > TRUNCATE test.users ;



 

How to Convert Java Timestamp to MySQL timestamp vice versa


How to Insert java time in mysql timestamp

java.util.Date date = new Date();


Timestamp timestamp = new Timestamp(date.getTime());

preparedStatement.setTimestamp(1, timestamp);



Thursday, 6 August 2015

How To Install and Use Memcache on Ubuntu

Memcached  is a general-purpose distributed memory caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source  must be read.

Install Memcached

1.     Firstly we install libevent

       sudo apt-get update

       sudo apt-get install libevent-core-2.0-5


2.  Install memcached 

 sudo apt-get install memcached



Configaration


3.  configure iptables  with these entries

 sudo iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 11211 -j ACCEPT

 sudo iptables -A INPUT -m state --state NEW -m udp -p udp --dport 11211 -j ACCEPT


4.  Go to  /usr/share/memcached/memcached.conf.default
     and  change defulat connection port:

   -p 11211 [defualt]

memory

 -m 20


and ip address also
-l 127.0.0.1 [ip address ]

5.  Now also configure    /etc/memcached.conf

  change    port

    -p 11211 [defualt]

    and ip address

  
-l 127.0.0.1 [ip address ]


Using

-> telnet 127.0.0.1 11211

You can check memcached is runing or not.