Thursday, October 18, 2012

JAVA relative path issue in CRONTAB

It can be occurred issues relative to files access when you have used relative path in JAVA application which are used by crontab in linux. crontab cannot be used in relative paths. One solution is to change JAVA application file access to FULL paths. Another solution is to create shell script as follows. Then, this script can be used by crontab without issues.

#!/bin/bash
cd /home/sujith/app_location
/usr/jdk1.7.0_07/bin/java -jar -Xms512m -Xmx1024m app_name.jar

Tuesday, October 9, 2012

mysqldump Command Usage

It is used mysqldump command to get a backup of existing tables.

Simply, run the following command to get the backup.

mysqldump -usujith -psujith database_name table_name  > /home/sujith/backup.sql

NOTE :- This dump contains all the table structure and data in table.

Thursday, October 4, 2012

Random String (ID) generate in JAVA

java.util.UUID package contains random String generation feature.

Refer below code.

UUID uuid = UUID.randomUUID();
uuid.toString();

Generated Strings

a5b74c68-41de-4605-affb-882e97e33779
8d615a58-8899-4d94-9bf0-b79e411d06b6

Wednesday, October 3, 2012

MySQL outfile write permission

When unloading data from MySQL database, it is needed to given special WRITE permission for user to write files.

Refer below command.

SQL command for unload data

  • select * into outfile '/tmp/testfile.txt' fields terminated by '|' from recharge where req_date<'2012-10-03';

SQL command to grant write permission

  • GRANT FILE ON *.* TO 'sujith'@'localhost';
  • flush privileges;


Tuesday, October 2, 2012

MySQL grant permission

It is needed to grant permission in MySQL server for users of the database. Below MySQL commands can be used to grant permissions.

  1. grant all privileges on database.* to 'sujith'@'localhost';
  2. flush privileges;
Refer MySQL documentation for more details.

Monday, October 1, 2012

export EDITOR=vi

In some cases, it is not shown data in Linux terminal window. In that case, you can use "vi editor" to view hidden data.

Following command can be used to export data to vi editor.

export EDITOR=vi   

This is most commonly used for crontab edition.

Tuesday, September 25, 2012

byte[] to Object in java

If data must be sent over network, it is needed to send  data as byte stream. In these cases, it can be used object serialize method as follows. Below method can be used to convert Object to byte stream. 

public static byte[] serialize(Object obj) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(out);
    os.writeObject(obj);
    return out.toByteArray();
}


Below deserialize method can be used to converted stream back to Object. If Object is specific use Object casting methods further.

public static Object deserialize(byte[] data) {
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    ObjectInputStream is = new ObjectInputStream(in);
    return is.readObject();
}


NOTE :- This can be avoided by using Java RMI technology.