Sunday, November 30, 2014

Elasticsearch Java example (Elasticsearch Java API Basic example)

Simple Code to get started with Elasticsearch Java APIs.



You may need to import following two jar files to run this example. You may find these jars inside your elastic search folder (installation dir) or you may download it from here 

Java Elasticsearch Example-



import java.util.LinkedHashMap;
import java.util.Map;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

public class ElasticSearchExample {

private static Client client;
private String indexName;
static String documentType = "json";
TransportClient transportClient;

public static void main(String[] str) throws Exception {

ElasticSearchExample elasticSearchService;

// create client
elasticSearchService = new ElasticSearchExample("localhost", 9300);

// create index
elasticSearchService.createIndex("test");

// add elements
LinkedHashMap jsonMap = new LinkedHashMap();
// add 1st element
jsonMap = new LinkedHashMap();
jsonMap.put("Content-Type", "application/octet-stream");
jsonMap.put("resourceName", "GFED3_aromatics_1997-2010_8683.nc");
jsonMap.put("user", "user_1");
elasticSearchService.addElement("1", jsonMap);
// add 2nd element
jsonMap = new LinkedHashMap();
jsonMap.put("user", "user_2");
jsonMap.put("Content-Type", "application/octet-stream");
jsonMap.put("resourceName", "GFED3_monoterpenes_1997-2010_8683.nc");
elasticSearchService.addElement("2", jsonMap);

// get element by document ID
elasticSearchService.getElement("1");

}

public ElasticSearchExample(String server, int port) {
transportClient = new TransportClient();
client = transportClient
.addTransportAddress(new InetSocketTransportAddress(server,
port));
}

public void createIndex(String indexName) {
// Create Index and set settings and mappings
this.indexName = indexName;
CreateIndexRequestBuilder createIndexRequestBuilder = client.admin()
.indices().prepareCreate(indexName);
createIndexRequestBuilder.execute().actionGet();
}

public void addElement(String documentId,
LinkedHashMap jsonMap) throws Exception {
IndexRequestBuilder indexRequestBuilder = client.prepareIndex(
indexName, documentType, documentId);
indexRequestBuilder.setSource(jsonMap);
IndexResponse response = indexRequestBuilder.execute().actionGet();
System.out.println("Element added-" + jsonMap);
}

public void getElement(String documentId) {
GetRequestBuilder getRequestBuilder = client.prepareGet(indexName,
documentType, documentId);
GetResponse response = getRequestBuilder.execute().actionGet();
Map map = response.getSourceAsMap();
System.out.print("getElementById -");
for (String key : map.keySet()) {
Object object = map.get(key);
System.out.print("{id:" + documentId + ", name:" + key + ", value:"
+ object + "}, ");
}
}

@Override
protected void finalize() throws Throwable {
transportClient.close();
super.finalize();
}


}



Source Code and jar files - here 


Tuesday, October 30, 2012

Import & Export database oracle

Import & Export database oracle

Export database

C:\>exp old_user/password file=dbFileName

Create new user:
Go to the SQL plush and execute following query
SQL> create user new_user identified by password
Grant DBA role to this user
SQL> grant DBA to new_user;

Import database
C:\>imp new_user/password file=dbFileName fromuser=old_user touser=new_user

Wednesday, September 7, 2011

How to access "internal exception" type in JAVA

how to catch MySQLIntegrityConstraintViolationException exception


Suppose JPA entry is:

@Table(name = "players", uniqueConstraints=@UniqueConstraint(columnNames={"firstname", "lastname"}))


EJB class is:
public void create(Player player) {
try {
em.persist(player);
em.flush();
} catch (Exception e) {
e.printStackTrace();
Throwable t = getLastThrowable(e);
throw new Exception(t);
}
}



/** method to access internal exceptions */

private Throwable getLastThrowable(Exception e) {
Throwable t = null;
for(t = e.getCause(); t.getCause() != null; t = t.getCause());
return t;
}