Thursday, December 15, 2011

MySQL - Convert MySQL date, datetime, timestamp to unix time and Vice versa

To convert Unix time to regular MySQL DATETIME
select from_unixtime('1213907115');
It prints '2008-06-19 16:25:15'

To Convert DATETIME to unixtime
select unix_timestamp('2008-06-19 16:25:15');
It prints 1213907115

To Convert current date & time to unix time.
select unix_timestamp();

To Convert a column of DATETIME type to unixtime.
select unix_timestamp(c8013_date_created) from RealTimeStatus.T8013_PROGRAM;

Wednesday, December 14, 2011

JavaMail using SSL and Gmail


Make sure you have mail.jar. Refer example with TLS (Transport Layer Service, SSL - Secure Sockets Layer - http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/)

/**
 *
 */
package sendEmail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

/**
 * @author glakshmanan
 * Download mail.jar for this program
 */
public class SendMailUsingGmailSSL {

private static final Logger logger = Logger
.getLogger(SendMailUsingGmailSSL.class);

/**
*
*/
public SendMailUsingGmailSSL() {
// TODO Auto-generated constructor stub
}

/**
* @param args
*/
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username",
"pwd");
}
});

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("gubs4u@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("mailaddress@hostname.com"));
message.setSubject("Testing Again");
message.setText("SSL Worked Atleast");

Transport.send(message);

logger.info("Mail Successfully Sent");

} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}

Tuesday, December 13, 2011

File Operations Example in JAVA


File Operations
package fileOperations;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.log4j.Logger;

/**
 * @author glakshmanan Nov 21st 2011
 */
public class fileOperations {

static Logger log = Logger.getLogger(fileOperations.class);
static final String fileName = "/home/glakshmanan/fileOperations.csv";

/**
*
*/
public fileOperations() {
// TODO Auto-generated constructor stub
}

/**
* @param args
*/
public static void main(String[] args) {
createFileIfNoExists();
writeDataInFile();
readDataFromFile();
deleteFileIfExist();

}

private static void deleteFileIfExist() {
new File(fileName).delete();
log.info("File Deleted Successfully");
}

private static void readDataFromFile() {
if (new File(fileName).exists()) {
FileReader fileReader = null;
try {
fileReader = new FileReader(fileName);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedReader br = new BufferedReader(fileReader);
try {
String text;
while ((text = br.readLine()) != null) {
log.info("Data from File : " + text);
}
} catch (IOException e) {
log.info("File Reader error" + e.getMessage());
}
}
}

private static void writeDataInFile() {
try {
if (new File(fileName).exists()) {
FileWriter fw = new FileWriter(fileName);
BufferedWriter bw = new BufferedWriter(fw);
String[] testArr = { "Gubs", "Kavitha", "Sai" };
for (String test : testArr) {
bw.write("Testing FileWritter : " + test + "\n");
}
bw.flush();
bw.close();
log.info("Written data into the file");
} else {
createFileIfNoExists();
}

} catch (IOException e) {
log.error("Failed to write content " + e.getMessage());

}
}

private static void createFileIfNoExists() {
File fileCreate = new File(fileName);
try {
if (fileCreate.exists()) {
boolean isFileExecutable = fileCreate.canRead()
&& fileCreate.canWrite();
if (isFileExecutable) {
log.info("File Exist and you can write in file "
+ fileCreate.getAbsolutePath());
}
} else {
fileCreate.createNewFile();
// fileCreate.setWritable(false);
log.info("File Creation Completed, Absolute path name "
+ fileCreate.getAbsolutePath());
}

} catch (IOException e) {
log.error("File Creation Operation Failed : " + e.getMessage());
}
}

}

Sunday, December 11, 2011

Apache Lucene POC with code and explanation


Lucene is an open source, highly scalable text search-engine library available from the Apache Software Foundation. You can use Lucene in commercial and open source applications. Lucene's powerful APIs focus mainly on text indexing and searching. It can be used to build search capabilities for applications such as e-mail clients, mailing lists, Web searches, database search, etc. Web sites like Wikipedia, TheServerSide, jGuru, and LinkedIn have been powered by Lucene.
Lucene has many features. It:
  • Has powerful, accurate, and efficient search algorithms.
  • Calculates a score for each document that matches a given query and returns the most relevant documents ranked by the scores.
  • Supports many powerful query types, such as PhraseQuery, WildcardQuery, RangeQuery, FuzzyQuery, BooleanQuery, and more.
  • Supports parsing of human-entered rich query expressions.
  • Allows users to extend the searching behavior using custom sorting, filtering, and query expression parsing.
  • Uses a file-based locking mechanism to prevent concurrent index modifications.
  • Allows searching and indexing simultaneously.

LuceneIndexWriter Class
package pocUsingLucene;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.lucene.analysis.LimitTokenCountAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LogMergePolicy;
import org.apache.lucene.index.SegmentInfo;
import org.apache.lucene.index.SerialMergeScheduler;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.NIOFSDirectory;
import org.apache.lucene.util.Version;

/**
 * @author glakshmanan
 *
 */
public class LuceneIndexWriter {

// Logger
public static final Logger log = Logger.getLogger(LuceneIndexWriter.class);

// IndexWriting Directory
private static final String INDEX_DIR = "/home/glakshmanan/index";

// Constructor
public LuceneIndexWriter() {
}

/**
* @param args
* @throws IOException
* @throws LockObtainFailedException
* @throws CorruptIndexException
* @throws ParseException
*/
public static void main(String[] args) throws CorruptIndexException,
LockObtainFailedException, IOException, ParseException {

LuceneIndexWriter luceneIndexWriter = new LuceneIndexWriter();

// Create IndexWriter object using config
IndexWriter iw = luceneIndexWriter.createIndexWriter();

// DeleteAll Documents
luceneIndexWriter.deleteAllDocuments(iw);

// Create Documents and index it
luceneIndexWriter.createDocument(iw);

// close the indexWriter
luceneIndexWriter.closeIndexWriter(iw);

// Delete the Document
// luceneIndexWriter.deleteDocument();

log.info("Index Writter written the data Sucessfully");
}

private void deleteAllDocuments(IndexWriter iw) throws IOException {
// Delete existing docs, segments and terms before we add new data
iw.deleteAll();
iw.commit();
}

/**
* This method will delete the Document from the indexDirectory using the
* Term
*
* @throws IOException
*/
@SuppressWarnings("unused")
private void deleteDocument() throws IOException {
Directory dir = NIOFSDirectory.open(new File(INDEX_DIR));
IndexReader ir = IndexReader.open(dir);
ir.deleteDocuments(new Term("name"));
// This will flush the commit and close
ir.close();
}

/**
* This method will commit changes and close the IndexWriter object
*
* @param iw
* @throws IOException
*/
private void closeIndexWriter(IndexWriter iw) throws IOException {
// Optimize, commit and close the IndexWriter
iw.optimize();
iw.commit();
iw.close();
}

/**
* This method will create document with fields and write into FD using
* indexWriter
*
* @param iw
* @throws CorruptIndexException
* @throws IOException
*/
private void createDocument(IndexWriter iw) throws CorruptIndexException,
IOException {
// Creating document to add using indexWriter
Document doc = new Document();

// Value is just stored not indexed
Field idField = new Field("id", "1", Field.Store.YES, Field.Index.NO);
// Value is stored with Indexed, Analyzed and Tokenized
Field nameField = new Field("name", "Gubendran", Field.Store.YES,
Field.Index.ANALYZED);
// Field is not stored and not analyzed
Field addressField = new Field("address", "Jersey city",
Field.Store.NO, Field.Index.NOT_ANALYZED);

// Combined the searchFullText and store in 1 index to search easy when
// we need to show full content
String searchFullText = "Gubendran" + "Jersey City";
Field searchFullTextField = new Field("content", searchFullText,
Field.Store.YES, Field.Index.ANALYZED);

// Add fields into document
doc.add(idField);
doc.add(nameField);
doc.add(addressField);
doc.add(searchFullTextField);

// Adding NumericField Example
/*
* NumericField numericField = new NumericField("title",
* Field.Store.YES, true);
* numericField.setIntValue(Integer.parseInt(value));
* doc.add(numericField);
*/

iw.addDocument(doc);
}

/**
* This method will create indexWriter object and return using
* indexConfiguration
*
* @return
* @throws CorruptIndexException
* @throws LockObtainFailedException
* @throws IOException
*/
private IndexWriter createIndexWriter() throws CorruptIndexException,
LockObtainFailedException, IOException {
// 1. Create the index
File file = new File(INDEX_DIR);
// If its linux system you can use NIOFSDirectory to make faster
Directory index = NIOFSDirectory.open(file);

// 0. Specify the analyzer for tokenizing text.
// The same analyzer should be used for indexing and searching. For
// English use standardAnalyzer
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);

// If the file size is huge. This is alternate for MaxFieldLength
LimitTokenCountAnalyzer limitTokenCountAnalyzer = new LimitTokenCountAnalyzer(
analyzer, Integer.MAX_VALUE);

// Documents will be stored in the segments. If there were more
// documents we need to keep in different segment to make faster
final int segmentSize = 10;
int mergeFactorSize = 100;
int RAMBufferSizeMB = 32;
int maxBufferedDocs = 500;
LogMergePolicy logMergePolicy = new LogMergePolicy() {

@Override
protected long size(SegmentInfo info) throws IOException {
return segmentSize;
}
};
// This parameter determines how many documents you can store in the
// original segment index and how often you can merge together the
// segment indexes in the disk
logMergePolicy.setMergeFactor(mergeFactorSize);

logMergePolicy.setUseCompoundFile(true);
// This parameter determines the maximum number of documents per segment
// index. The default value is Integer.MAX_VALUE. Large values are
// better for batched indexing and speedier searches.
logMergePolicy.setMaxMergeDocs(Integer.MAX_VALUE);

IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_31,
limitTokenCountAnalyzer);
config.setRAMBufferSizeMB(RAMBufferSizeMB);
config.setMergeScheduler(new SerialMergeScheduler());
config.setMaxBufferedDocs(maxBufferedDocs);

IndexWriter iw = new IndexWriter(index, config);
return iw;
}
}

Lucene IndexSearcher

package pocUsingLucene;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.NIOFSDirectory;
import org.apache.lucene.util.Version;

/**
 * @author glakshmanan
 *
 */
public class LuceneIndexSearcher {

// Logger
public static final Logger log = Logger
.getLogger(LuceneIndexSearcher.class);

// IndexSearcher Directory
private static final String INDEX_DIR = "/home/glakshmanan/index";

// Constructor
public LuceneIndexSearcher() {
super();
}

/**
* @param args
* @throws IOException
* @throws ParseException
*/
public static void main(String[] args) throws IOException, ParseException {

// Get the argument for Search
String searchText = args.length > 0 ? args[0] : "dummies";

LuceneIndexSearcher luceneIndexSearcher = new LuceneIndexSearcher();
// Create indexSearcher Object for search
IndexSearcher searcher = luceneIndexSearcher.createIndexSearcher();

// Create QueryParser Object and Collector object for search
Query queryParser = luceneIndexSearcher.createQueryParser(searchText);

// Using TopScoreDocCollector collect the documents for the
// corresponding search value. hitsPerPage is important for usage of
// paging and Filter. Paging and Filter needed to make search faster
int hitsPerPage = 10;
TopScoreDocCollector collector = TopScoreDocCollector.create(
hitsPerPage, true);
searcher.search(queryParser, collector);

ScoreDoc[] scoreDocs = collector.topDocs().scoreDocs;

// Display the search values using top scored docs
luceneIndexSearcher.displaySearchResult(searcher, scoreDocs);

// searcher can only be closed when there
// is no need to access the documents any more.
searcher.close();
}

/**
* Display the search value result from ScoreDocs
*
* @param searcher
* @param scoreDocs
* @throws CorruptIndexException
* @throws IOException
*/
private void displaySearchResult(IndexSearcher searcher,
ScoreDoc[] scoreDocs) throws CorruptIndexException, IOException {
log.info("List of docs found : " + scoreDocs.length);
for (ScoreDoc scoreDoc : scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);
log.info("Name : " + doc.get("name"));
}
}

/**
* Create QueryParser object for indexSearcher using search text
*
* @param searchText
* @return
* @throws ParseException
*/
private Query createQueryParser(String searchText) throws ParseException {

// Create a queryParserObject for Search using search field and
// search value. Use same Analyzer you used for indexWriter to
// indexSearch also
Query queryParser = new QueryParser(Version.LUCENE_31, "name",
new StandardAnalyzer(Version.LUCENE_31)).parse(searchText);

// NumericRangeQuery search for numeric search
// Query queryParser = NumericRangeQuery.newIntRange("title", 4, 40,
// 500, true, true);
return queryParser;
}

/**
* Create IndexSearcher object for searcher
*
* @return
* @throws IOException
*/
private IndexSearcher createIndexSearcher() throws IOException {
// 1. Create the index
File file = new File(INDEX_DIR);
Directory index = NIOFSDirectory.open(file);

// Create indexSearcher Object using index Directory
IndexSearcher indexSearcher = new IndexSearcher(index, true);

return indexSearcher;
}

}



Thursday, December 8, 2011

MySQL Performance Tuning

1) TIMESTAMP would be better than DATETIME
2) ENUM will be better than VARCHAR

Wednesday, December 7, 2011

Eclipse

How to Load Javadocs / Java Source attachment for a specific jar (for example lucene-core):
In the project Explorer->select lucene-core*.jar -> right click and select properties -> select Javadoc location -> add lucene doc location folder to it. (Extract lucene doc jar from the distribution and provide the location)

How to load External Jars
Create / Open a class file -> Go to Project tab -> Properties -> (Search) java build path -> Click 'Add External Jars' -> select your jar and 'ok'

To see all shortcuts in Eclipse
Shift + Ctrl + l (L) - To open the preference page or to see all shortcuts
Alt + Shift + j - Add javadocs for methods or classes. Keep the cursor over method and press it.
ctrl + shift + r - Open Resource
ctrl + shift + c - comment a line of code / un-comment a line of code
ctrl + shift + g - Reference
ctrl + shift + d - To remove a single line
ctrl + shift + / (backslash) - comment a block (select a block you want to comment and use the shortcut key)
ctrl + shift + \ (forwardslash) - uncomment a block (select a block you want to comment and use the shortcut key)

http://codeofdoom.com/wordpress/2009/03/05/favorite-eclipse-shortcuts/
Alt+Shirt+R – rename refactoring
ctrl + shift + o (oh!) - Remove unwanted warnings
ctrl  + shift + f - Align the file.
ctrl + d = delete a line 

Line Numbers in Eclipse

To turn on line numbering in Eclipse, simply do the following;
1) Windows -> Preferences
2) General – > Editors -> Text Editors
and click the “Show Line Numbers” checkbox

How to modify project into DynamicwebProject, Java and JavaScript
Select the project folder -> right click go to properties -> select "Project Facets" -> select "Dynamic Web Module", "java", "JavaScript"

Need to add folder structure in Project
Select the project folder -> right click go to properties -> select "java build path" -> go to Source tab add Folder

Add Jars using Variable
Select Project (Folder - Folder with J, S, tag on it) -> properties -> java build path -> Libraries -> Add Variable -> select variable and click 'Extend...'. -> select the jar and ok.
If you need to add variables click 'Configure Variables...'

Required project on a build Path :

From 1 project if you need to load other projects classes, libraries from "java build path" "Projects" you can add projets. So, in 1 project DAO, DO you can call from other projects.

How to add Spring Capabilities to your Project :
Right click the project folder, and select Spring Tools -> Add Spring Project Nature, to add Spring capabilities to the web project. This feature will be available once you install the Spring IDE.

Extract a Jar file
jar -xvf .jar

Import images into Eclipse folder
Drag and drop the images into the eclipse folder you want.
eclipse.ini

-XX:MaxPermSize=512m
-Xms1024m
-Xmx2048m
-XX:-UseParallelGC 
-XX:+AggressiveOpts 
-XX:-UseConcMarkSweepGC 
-XX:+UseFastAccessorMethods

Install subversion plug-in on eclipse
In Eclipse -> Help -> Eclipse Market Place -> search "subclipse" and install.
// Below script tag for SyntaxHighLighter