Archive

Author Archive

How to deploy a neo4j instance in Amazon EC2 in 10 minutes

December 27th, 2011 negarnil 4 comments

Neo4j is a high-performance, NOSQL graph database with all the features of a mature and robust database. In this post I will explain how to deploy a neo4j instance in Amazon EC2 web service.

For this tutorial to take you no more than 10 minutes you should be able to execute properly some bash commands like mv, tar, ssh and scp (secure copy). I also assume that you have an account in Amazon Web Services and you are familiar to the process of launching instances. If not, I strongly recommend you to follow this starting guide and complete it till you manage to connect to your instance with ssh.

Start downloading the latest stable version of neo4j. Which you can find here. The “Community Edition” fits well for development purposes. Do not forget to select the Unix version of the server. This will download a tar.gz file which you will copy to your EC2 instance later.

While you download the neo4j server open the AWS Management Console and launch a Basic 32-bit Amazon Linux AMI. If you want to launch an Ubuntu AMI please notice that it doesn’t ship with Java, which is required for running neo4j.

If you are not familiar with key pairs, pem files or security groups I insist you to follow the EC2 starting guide I mentioned above. You can either create a new security group or use the default, but you will need to configure a new security rule for the neo4j server port.

After launching the instance, create a TCP rule on port 7474 with source 0.0.0.0/0. Here you are opening port 7474 for anyone. If you are planning to use the neo4j REST API and remotely call it from another server, for example a Rails application hosted in Heroku, for security reasons, you may want to change the source field to the address of your Heroku server.

Do not forget to open port 22 (SSH), this is typically the first rule normal people create after launching an instance.

You are almost done! You should now install neo4j in your instance. Open a terminal in your localhost and navigate to the path where you downloaded neo4j. Copy the file to your Amazon instance by using the scp command:

scp -i your_pem_file.pem neo4j-community-1.6.M01-unix.tar.gz ec2-user@YOUR_PUBLIC_INSTANCE_DNS:/home/ec2-user

Please notice that you will need to change the path to your pem file, typically placed in ~/.ssh, the filename of the neo4j server you just downloaded and the plublic DNS of your instance.

Now connect to your instance with SSH:

ssh -i your_pem_file.pem ec2-user@YOUR_PUBLIC_INSTANCE_DNS

Untar the neo4j server:

tar xvfz neo4j-community-1.6.M01-unix.tar.gz.tar.gz

Move it to /usr/local and rename the folder to neo4j:

sudo mv neo4j-community-1.6.M01 /usr/local/neo4j

Almost done!!! You should now open neo4j-server.properties under the conf directory and add the following line:

org.neo4j.server.webserver.address=0.0.0.0

This lines allows anyone to connect remotely to your neo4j database server.

Now run the start script. From the neo4j server folder.

sudo ./bin/neo4j start

Finally, open a browser and access the webadmin interface of your neo4j database by typing http://YOUR_PUBLIC_INSTANCE_DNS:7474. You should see the Neo4j Monitoring and Management Tool, pretty cool!

If not, ask me :)

You can now try using the REST API and the curl bash command to insert nodes and relationships.

I hope this post helped you, good luck! Follow me on Twitter @negarnil

Share
Categories: Database, Java, neo4j Tags: , ,

A Handy and Highly Reusable JPA 2.0 Data Access Object

January 4th, 2011 negarnil 5 comments

Hi ! In this post I’ll show you a highly reusable Data Access Object for Java web applications which will reduce the development time of your DAOs.

Consider the following DAO and it’s implementation.

public interface UserDao {

     User storeUser(User user);

     void removeUser(User user);

     List<User> findAllUsers();

     User findUserById(long id);

}
public class JpaUserDao implements UserDao {

     @PersistentContext
     private EntityManager em;

     @Override
     public User storeUser(User user) {
          User merged = em.merge(user);
          em.persist(merged);
          return merged;
     }

     @Override
     public void removeUser(User user) {
          em.remove(user);
     }

     @Override
     public List<User> findAllUsers() {
          return (List <User>) em.createQuery("from User").getResultList();
     }

     @Override
     public User findUserById(Long id) {
        return em.find(User.class, id);
     }
}

It looks pretty cool ! We just coded a JPA 2.0 Data Access Object. This means we used standard Java libraries (javax.persistence package).

The problem with the above code is that you are going to repeat it for each entity in your application. Having those 40 lines of CRUD (Create-Remove-Update-Delete) code doesn’t only lead you to more development effort but only to a poorly maintainable application. A code generator like Spring Roo can do the work but we can avoid any code generation tools by applying some Java inheritance and spicy code like using Java Generics and Reflection. You’ll see how to use those forty lines for infinite entities DAOs.

First of all let’s create a generic DAO:

public interface BaseDao<T> {

	T store(T t);

	void remove(T t);

	List<T> findAll();

	T findById(long id);

}

The above interface will be extended by all your DAO interfaces.

We should now create a generic implementation of the above interface which will be extended by all the DAO implementations of your application.

@SuppressWarnings("unchecked")
@Repository
public abstract class JpaBaseDao<T extends Serializable> implements BaseDao<T> {

	private Class<T> clazz;

	@PersistenceContext
	protected EntityManager em;

	protected JpaBaseDao() {
		Class<T> getClass = (Class<T>) getClass();
		ParameterizedType genericSuperclass = (ParameterizedType) getClass.getGenericSuperclass();
		clazz = (Class<T>) genericSuperclass.getActualTypeArguments()[0];
	}

	@Override
	public T store(T t) {
		T merged = em.merge(t);
		em.persist(merged);
		return merged;
	}

	@Override
	public void remove(T t) {
		em.remove(t);
	}

	@Override
	public List<T> findAll() {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<T> query = cb.createQuery(clazz);
		query.from(clazz);
		return em.createQuery(query).getResultList();
	}

	@Override
	public T findById(long id) {
		return em.find(clazz, id);
	}
}

The @Repository annotation is a Spring annotation which Indicates that an annotated class is a “Repository” (or “DAO”). A class thus annotated is eligible for Spring DataAccessException translation. The annotated class is also clarified as to its role in the overall application architecture for the purpose of tools, aspects, etc. Note that you don’t need it if you are not using Spring.

The @PersistenContext is a javax.persistence annotation which expresses a dependency on an EntityManager persistence context.

The final code:

public interface UserDao extends BaseDao<User> {

     // Custom access methods (which are not CRUD's) go here

}
public class JpaUserDao extends JpaBaseDao<User> implements UserDao {

   // Custom access methods implementations go here

}

Then in a service layer, let’s say a Spring service, you can access the BaseDao CRUD methods from any DAO which inherits it.

@Service
public class UserService {

     @Autowired
     private UserDao userDao;

     @Transactional
     public User saveUser(User user) {
          return userDao.store(user);
     }

}

Well, that’s all ! I hope you find it useful.

If you enjoyed this post follow me on twitter or leave a feedback !

Share

Amazon Scales MySQL for Heavy Traffic

November 27th, 2010 negarnil No comments

Amazon Scales MySQL for Heavy Traffic
— Amazon Web Services (AWS) has added Read Replicas to its Relational Database Service (RDS) to make it easier to scale MySQL deployments to meet the performance demands of high-traffic web applications.

Read Replicas lets users create one or more copies of a MySQL Database Instance and an application serve read traffic from the multiple replicas. They can create or delete replicas in minutes using the point-and-click interface of the AWS Management Console.

Amazon says Read Replicas can be made to another Availability Zone as a disaster-ready failover measure under its Multi-AZ deployments program. Once a Read Replica is created, any subsequent updates to the source will automatically be replicated in the Read Replica. And Read Replicas can be elastically added to any RDS database deployment to keep query response times fast, even as request volumes scale.

Amazon figures it now offers advantages in scalability, multi-data center availability, elasticity and administration at a fraction of the cost of operating MySQL servers on-premise.

Sony Computer Entertainment America says it ran benchmarks comparing Amazon RDS to its hand-tuned MySQL instances and found RDS performed significantly better.

Amazon RDS has also lowered prices for High Memory Database Instances. The prices for 68GB Quadruple Extra Large Database Instances and 34GB Double Extra Large Database Instances have dropped ~15%.

See http://aws.amazon.com/rds/.

If you enjoyed this post follow me on twitter or leave a feedback !

Share

Getting the Best MySQL Performance in Your Products: Part 1, The Fundamentals

September 4th, 2010 negarnil No comments

Hi ! I found this must-to-read webcast which explains the basics of MySQL performance, it is quite new ! In my opinion anyone who aims to manage a database application should at least be aware of these topics !

If you use Hibernate to manage the database layer of your web applications you should never be satisfied with the default configuration it provides. Hibernate does provides caching strategies and other tuning features like c3p0 but you can go much further of that configurations to better optimize the database.

I recommend you to launch the webcast and follow it using the slides which you can download as a PDF. Apparently, the MySQL expert who speaks during the webcast had problems to change the slides just before starting explaining the next one, so for some moments it’s kind of messy.

Launch the webcast from here or click the image below.

You can also check this video from Google Talks which explains some other topics about performance tuning in MySQL.

Bye ! Keep around !

If you enjoyed this post follow me on twitter or leave a feedback !

Share

Singleton your JRapid Services

August 27th, 2010 negarnil 1 comment

Hi ! In this post I’ll explain the Singleton Pattern and how you can use it to increase the performance of your web applications, specially in JRapid ’s.

There is no need to use a Profiler to know that the Java ‘new’ operator increases the memory allocation in the heap. The JVM’s heap stores all objects created by an executing Java program. Objects are created by Java’s “new” operator, and memory for new objects is allocated on the heap at run time.

But, fortunately, the Java Garbage Collector “throws away” the objects which are not needed anymore freeing space in the heap for new objects to be allocated. This frees the programmer from having to keep track of when to free allocated memory.

A potential disadvantage of a garbage-collected heap is that it adds an overhead that can affect program performance. The JVM has to keep track of which objects are being referenced by the executing program, and finalize and free unreferenced objects on the fly. This activity will likely require more CPU time than would have been required if the program explicitly freed unnecessary memory. In addition, programmers in a garbage-collected environment have less control over the scheduling of CPU time devoted to freeing objects that are no longer needed.

There is a common antipattern in mainly all Java applications which consists in instantiating a new Object each time it is required to so something.

In the case of JRapid applications, there is no need to instantiate a new Service each time there is a need to call a find(), store() or any method inside the class.

Example of the antipattern in JRapid applications:

public void pokeUser(User fromUser, User toUser) {

    Poke poke = new Poke();
    poke.setFrom(fromUser);
    poke.setToUser(toUser);
    poke.store();

    new EmailServices().notifyPoked(toUser);
}

This method instantiates a new EmailServices object each time a user pokes another user. Imagine you have an average of 1000 pokes per minute (or imagine you are Facebook).

A possible solution for this excessive memory allocation is to Singleton the EmailServices class and call always the same instance when a new email has to be send.

public class EmailServices extends EmailServicesAbstract {

    // Static instance
    private static EmailServices INSTANCE = null;

    // Singleton classes should have private constructors but the XMLRPC server needs this constructor to make the remote      calls
    public EmailServices() {
    }

    // Static getter of the instance, if it is null then call the constructor, else return it.
    public static synchronized EmailServices getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new EmailServices();
        }
        return INSTANCE;
    }

    public void notifyPoked(User user) {
        // Send a mail to user
    }
}

So now, the pokeUser method will look like this:

public void pokeUser (User fromUser, User toUser) {

    Poke poke = new Poke();
    poke.setFrom(fromUser);
    poke.setToUser(toUser);
    poke.store();

    EmailServices.getInstance().notifyPoked(toUser);
}

This way, only one EmailServices instance will be created in the app the server and the CPU usage will be reduced considerably.

Keep around !

If you enjoyed this post follow me on twitter or leave a feedback !

Share
Categories: JRapid, Java Tags: ,

Applet: Java Graph Algorithms

May 16th, 2010 negarnil No comments

Hi ! I have one more Applet to share with you (it turned that I was a Java 2D developer before discovering JEE !). This Applet was built to watch the 6 Java Graph Algorithms in “action”. It’s pretty cool !

Powered by Cincopa WordPress plugin

Launch the applet from here.

Bye, keep around !

If you enjoyed this post follow me on twitter or leave a feedback !

Share
Categories: Java Tags: ,

6 Clean implementations of Java Graph algorithms

May 15th, 2010 negarnil 1 comment

Hi ! In this post I will share with you 6 Java Graph algorithms I had to code for a discrete mathematics final exam in college. The algorithms were presented in an Applet I built and which I also include for you to play around with them.

Before presenting the algorithms I will give a general background about Graph Theory.

What is a graph?

(from Wikipedia) A graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected objects are represented by mathematical abstractions called vertices, and the links that connect some pairs of vertices are called edges.

Type of graphs

There are many, many, many type graphs, most of them associated to maths properties. For the purpose of the API I developed for the algorithms implementations you should know the following:

In terms of representation, a graph can be represented either by an adjacency matrix or an adjacency list. In an adjacency matrix, a true/false (x,y) cell represents if exists a link between node “x” and node “y”. This is the one which I used because of its better performance. In an adjacency list, each node has an own list for storing referenced lists.

Graphs can be also classified by its links, we say that a graph is “weighted” when all links haves a “weight” o “cost” for going from one node to another.

Finally, graphs can be “directed” o “undirected” which means if it’s links are bidirectional or not.

So, the API of my implementations is something like:

public abstract class GraphADT { ... }

Implementations:

public class Graph extends GraphADT { ... }
public class WeightedGraph extends GraphADT { ... }

Prim

Given a connected weighted graph and a source vertex, returns a minimum spanning tree which is a tree that includes every vertex but the total weight of all the edges in the tree is minimized.

WeightedGraph prim(WeightedGraph graph, int s) { ... }

Warshall

Warshall’s algorithm calculates a transitive closure for a given graph. In mathematics, the transitive closure of a binary relation R on a set X is the smallest transitive relation on X that contains R.

void warshall(Graph g) { ... }

BFS

Breadth-first search (BFS) is a graph search algorithm that begins at the root node and explores all the neighboring nodes. Then for each of those nearest nodes, it explores their unexplored neighbor nodes, and so on, until it finds the goal.

int BFS(GraphADT graph, int v, int[] levelOrder) { ... }

DFS

Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph. One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking.

int DFS(GraphADT graph, int v, int[] pre, int[] post) { ... }

Dijkstra

Dijkstra’s algorithm, conceived by Dutch computer scientist Edsger Dijkstra in 1959, is a graph search algorithm that solves the single-source shortest path problem for a graph with nonnegative edge path costs, producing a shortest path tree. This algorithm is often used in routing.

Integer[] dijkstra(WeightedGraph graph, int source) { ... }

Floyd

The Floyd–Warshall algorithm is a graph analysis algorithm for finding shortest paths in a weighted graph (with positive or negative edge weights).

int[][] floyd(WeightedGraph graph) { ... }

Applet

I built an Applet to test the algorithms and graph implementations.

In it you can build many type of graphs and run the discussed algorithms.

Hava a look !


Download !

Checkout the project from http://code.google.com/p/javagraphalgorithms/.

Direct download from here !

Bye ! Keep around !

If you enjoyed this post follow me on twitter or leave a feedback !

Share
Categories: Java Tags: , ,

Applet: Pictorial representations of Java Sorting Algorithms

May 4th, 2010 negarnil No comments

Hi ! In this occasion I will share with you a Java Applet I built to visualize the dynamic characteristics of some of the 17 Clean implementations of Java Sorting Algorithms I posted some days ago.

Sorting algorithms are especially amenable to pictorial representations that make plain their dynamic characteristics. Visual representations are useful for understanding the operations of sorting algorithms.

Powered by Cincopa WordPress plugin

I had to add thread interruptions to the fastest sorters (QuickSort, MergeSort, etc…) because if not, the animations were too fast. So don’t ever, never, use this Applet to take conclusions about which sorter is the fastest.

Launch it !

If you enjoyed this post follow me on twitter or leave a feedback !

Share
Categories: Java Tags: , ,

17 Clean implementations of Java Sorting Algorithms

May 2nd, 2010 negarnil 17 comments

Well, in this post I will share with you 17 Java Sorting implementations I had to code in an algorithms course at college ! The package includes the following implementations (from the easiest to the more complex ones).

  • Bubble Sort (SorterType.BUBBLE)
  • Selection Sort (SorterType.SELECTION)
  • Insertion Sort (SorterType.INSERTION)
  • HSort (SorterType.H)
  • Shell Sort (SorterType.SHELL)
  • Quick Sort (SorterType.QUICK)
  • Quick “Cut” Sort (SorterType.QUICK_CUT)
  • Quick “Median of Three” Sort (SorterType.QUICK_MED_OF_THREE)
  • Quick “Non recursive” Sort (SorterType.QUICK_NON_RECURSIVE)
  • Quick “Three way Partition” Sort (SorterType.QUICK_THREE_PARTITION)
  • Bottom-Up Merge Sort (SorterType.MERGE_BOTTOM_UP)
  • Bottom-Up Linked-List Merge Sort
  • Top-Down Merge Sort (SorterType.MERGE)
  • Top-Down Linked-List Merge Sort
  • Binary Heap Sort (SorterType.HEAP_BINARY)
  • Ternary Heap Sort (SorterType.HEAP_TERNARY)

All implementations come with a brief explanation for you to understand the basic idea of how the algorithm sorts the elements !

API

All Sorting implementations extends AbstractSorter which implements Sorter. The Sorter interface is generified. This allows subsequent implementations to be capable to sort any collection of objects as long as exists a Comparator for that Type of objects.

public interface Sorter {

<T> void sort(Comparator<T> comparator, List<T> list) ;

SorterType getType();

}
public abstract class AbstractSorter implements Sorter { ... }

Implementations:

public class InsertionSorter extends AbstractSorter { ... }

How to use?

  • Get a DataGenerator instance:
DataSetGenerator<Integer> dataGenerator = new IntegerDataSetGenerator();
  • Get a SorterProvider instance:
SorterProvider sorterProvider = new SorterProvider();
  • Create a random list and sort it using QuickSort:
List<Integer> randomList = dataGenerator.createRandom(1000);
Sorter quickSort = sorterProvider.getSorterForType(SorterType.QUICK);
quickSort.sort(dataGenerator.getComparator(), randomList);
  • Create a descending list and sort it using SelectionSort:
List<Integer> descendingList = dataGenerator.createDescending(1000);
Sorter selectionSort = sorterProvider.getSorterForType(SorterType.SELECTION);
selectionSort.sort(dataGenerator.getComparator(), descendingList);
  • It isn’t a must to create a DataGenerator instance but at some point you will have to implement a java.util.Comparator.
public class StringComparator implements Comparator<String>  {

@Override
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}

}
String[] names = new String[]{"Mery", "Paul", "Bruce"};
Sorter mergeSort = sorterProvider.getSorterForType(SorterType.MERGE);
mergeSort.sort(new StringComparator(), Arrays.asList(names));
  • It also isn’t a must to instance a SorterProvider. You can directly instance the Sorter implementation.
String[] surnames = new String[]{"Smith", "Garnil", "Valentino"};
Sorter shellSort = new ShellSorter();
shellSort.sort(new StringComparator(),  Arrays.asList(surnames));

Testing the sorters

You can find a JUnit TestCase named SortersTest.java inside the anaydis.ngarnil.test package. This class tests correctness of the Sorter implementations.

A JUnit JAR (junit.jar) file is included in the lib folder.

public class SortersTest extends TestCase { ... }

I also built an Applet to run the sorters and study their dynamic characteristics.

Powered by Cincopa WordPress plugin

Download !

Checkout the project from http://javasorting.googlecode.com. Don’t doubt to make questions or post issues, I will answer them as soon as I can.

Direct download: From here!

If you liked this post please vote it in dzone !

Bye !

If you enjoyed this post follow me on twitter or leave a feedback !

Share
Categories: Java Tags: ,

Introducing JRapid WebForms (Part 2): Embed forms in Ning Networks

April 28th, 2010 negarnil No comments

Hi ! In the first part of this post I introduced JRapid WebForms which, synthetically speaking, allows non-programmer users to embed and manage web forms in their websites.

In this post I will explain you how to embed a WebForm in a Ning network as a Ning App.

First things first… what is Ning and what is a Ning App?

Ning is an online platform for people to create their own social networks. Ning networks are Facebook like, people can register in your network, make friends, leave messages in peoples “Wall”, share photos, write in a forum, etc. As well as Facebook, Ning created the Ning Apps which allows creators to add extra functionality to their networks.

Okey… and how does WebForms fits in all this stuff?

You can embed a WebForm as a Ning App and allow network-users to submit any kind of information like feedback, polls, product orders, etc !

Note: Currently, the JRapid Community site (which is a Ning network) uses four Ning apps: “Views”, “Widgets”, “Transformers” and “Templates”. These are not WebForms but are Ning Apps created through JRapid.

You can find them in the “Plugins” tab.

Nice ! How simple is to embed a WebForm inside a Ning network?

Extremely easy ! So easy that I will not explain it…

Okey… I will explain it.

Follow the 5-steps creation process as if you were creating another WebForm. Stop in step 5 and look at the bottom, you will see something like this:

Click in the “Do you want to use your forms in Ning?” link.

A form will appear below. Complete it with the name of your Ning network and press ‘Tab’.

Finally, add the Ning App to your network.

You should not have any kind of problems !

This is the result:

Try it !

Don’t doubt to leave comments !

If you enjoyed this post follow me on twitter or leave a feedback !

Share
Categories: JRapid Tags: ,