My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
GettingStarted  
Getting started with hs4j
Featured
Updated Jan 12, 2011 by killme2...@gmail.com

Introduction

HS4J is a practical HandlerSocket client for java.HandlerSocket is a MySql plugin developed by akira higuchi to make MySql as a NoSQL storage.

New a client

import com.google.code.hs4j.HSClient;
import com.google.code.hs4j.impl.HSClientImpl;

   HSClient hsClient = new HSClientImpl(new InetSocketAddress(9999));

HSFClient is a main class to talk with handlersocket,it's thread-safe.

HS4J also could be configured a connection pool:

   //100-connections pool
   HSClient hsClient = new HSClientImpl(new InetSocketAddress(9999),100);

Open Index

import com.google.code.hs4j.IndexSession;

      IndexSession session = hsClient.openIndexSession(db, table,
				"PRIMARY", columns);

The IndexSession represent an opened-index with a auto-generated indexid and it's thread-safe too.You can special a indexid:

IndexSession session = hsClient.openIndexSession(indexid,db, table,
				"PRIMARY", columns);

Find data

import java.sql.ResultSet;

                final String[] keys = { "dennis", "killme2008@gmail.com" };
		ResultSet rs = session.find(keys);
                while(rs.next()){
                   String name=rs.getString(1);
                   String mail=rs.getString(2);
                }

The result returned as a java.sql.ResultSet.

Insert Data

this.session.insert(new String[] { "0", "dennis",
				"killme2008@gmail.com", "27", "2010-11-28 13:24:00" })

But ModifyStatement is recommended

	        ModifyStatement stmt = this.session.createStatement();
		stmt.setInt(1, 0);
		stmt.setString(2, "dennis");
		stmt.setString(3, "killme2008@gmail.com");
		stmt.setInt(4, 27);
		stmt.setString(5, "2010-11-28 13:24:00");
                boolean result=stmt.insert();

Update data

import com.google.code.hs4j.FindOperator;

   int result=session.update(keys, new String[] { "1", "dennis",
				"test@163.com", "109" }, FindOperator.EQ);

And also you can use ModifyStatement:

                stmt = this.session.createStatement();
		stmt.setInt(1, 1);
		stmt.setString(2, "dennis");
		stmt.setString(3, "test@163.com");
		stmt.setInt(4, 109);
		int result=stmt.update(keys, FindOperator.EQ);

Delete data

   int result= session.delete(new String[] { "dennis" },
				FindOperator.EQ)

More info

HS4J will heal a connection when it was disconnected by networking error or other exception,and when it was reconnected successfully then all opened-index will reopen on new connection.

HS4J is a new client,and it maybe has some bugs that i don't know,please email me,thanks.

Comment by fresco....@gmail.com, Mar 28, 2011

Nice work!


Sign in to add a comment
Powered by Google Project Hosting