Hi all,
I had created a key value with index and multi values NoSQL using H2
database for my project.
Hopefully it help others as well. enjoy and comments
Probably you all had a better ideas :-)
Thanks H2 and community
example code :
tzDBStore store = new tzDBStore();
long ll_start = System.currentTimeMillis();
store.open("./test_tzKMStore2");
System.out.println("open:" + ( System.currentTimeMillis() - ll_start
));
ll_start = System.currentTimeMillis();
tzDBMap map = store.openMap("test");
System.out.println("open test:" + ( System.currentTimeMillis() -
ll_start ));
for( int i = 0 ; i < 1000 ; i ++ ){
map.put( "A" + String.valueOf(i), "Avalue" + i );
}
tzDBMap map2 = store.openMap("test3");
for( int i = 0 ; i < 100000 ; i ++ ){
map2.put( "C" + String.valueOf(i), "Cvalue" + i );
}
ll_start = System.currentTimeMillis();
System.out.println( map2.getString("C1123"));
System.out.println("search:" + ( System.currentTimeMillis() -
ll_start ));
map.retrieveAllCol(1);
while( map.next()){
System.out.println( map.getString());
}
map.close();
map2.close();
store.close();
--
You received this message because you are subscribed to the Google Groups "H2
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/d/optout.
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author ivanooi
*
* Thanks to H2 for a such simple and great database.
*
*/
public class tzDBMap {
ResultSet irsts_value ;
Statement istmt_value ;
String is_group ;
public tzDBMap( Statement statement, String group ) {
istmt_value = statement ;
is_group = group ;
}
public String close(){
try {
istmt_value.close();
} catch (SQLException e) {
e.printStackTrace();
return e.toString();
}
return null ;
}
public String put( String key, String value ){
try {
istmt_value.executeUpdate("MERGE INTO tz_map( group_code, key_code, value1 ) KEY(group_code, key_code) VALUES('" + is_group + "', '" + key + "', '" + value + "')");
} catch (SQLException e) {
e.printStackTrace();
return e.toString();
}
return null ;
}
public String getString( String key ){
return getString( key, 1 );
}
public String getString( String key, int col ){
String ls_value = null ;
try {
ResultSet result = istmt_value.executeQuery("SELECT value" + col + " FROM tz_map WHERE key_code = '" + key + "' AND group_code = '" + is_group + "'");
while( result.next()){
ls_value = result.getString("value" + col );
}
} catch (SQLException e) {
e.printStackTrace();
return null ;
}
return ls_value ;
}
public String retrieveAllCol( int col ){
try {
irsts_value = istmt_value.executeQuery("SELECT value" + col + " FROM tz_map WHERE group_code = '" + is_group + "' order by index ");
} catch (SQLException e) {
e.printStackTrace();
return e.toString() ;
}
return null ;
}
public String getString(){
return getString(1);
}
public String getString( int col ){
try {
return irsts_value.getString("value" + col );
} catch (SQLException e) {
e.printStackTrace();
}
return null ;
}
public boolean next(){
if( irsts_value == null )return false ;
boolean lb_result = false ;
try {
lb_result = irsts_value.next();
if( lb_result == false )irsts_value.close();
return lb_result;
} catch (SQLException e) {
e.printStackTrace();
}
return false ;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author ivanooi
*
* Thanks to H2 for a such simple and great database.
*
*/
public class tzDBStore {
private Connection conn ;
// private Statement istat_data ;
public tzDBStore() {
}
public static void main(String[] args) {
tzDBStore store = new tzDBStore();
long ll_start = System.currentTimeMillis();
store.open("./test_tzKMStore2");
System.out.println("open:" + ( System.currentTimeMillis() - ll_start ));
ll_start = System.currentTimeMillis();
tzDBMap map = store.openMap("test");
System.out.println("open test:" + ( System.currentTimeMillis() - ll_start ));
// for( int i = 0 ; i < 1000 ; i ++ ){
// map.put( "A" + String.valueOf(i), "Avalue" + i );
// }
tzDBMap map2 = store.openMap("test3");
// for( int i = 0 ; i < 100000 ; i ++ ){
// map2.put( "C" + String.valueOf(i), "Cvalue" + i );
// }
ll_start = System.currentTimeMillis();
System.out.println( map2.getString("C1123"));
System.out.println("search:" + ( System.currentTimeMillis() - ll_start ));
// map.retrieveAllCol(1);
//
// while( map.next()){
// System.out.println( map.getString());
// }
map.close();
map2.close();
store.close();
// System.out.println("end:" + ( System.currentTimeMillis() - ll_start ));
}
public String close(){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
return e.toString();
}
return null ;
}
public String open( String as_path_n_name ) {
try {
//./test_merge
conn = DriverManager.getConnection("jdbc:h2:" + as_path_n_name , "sa", "");
conn.setAutoCommit(true);
Statement state = conn.createStatement();
state.executeUpdate("CREATE TABLE IF NOT EXISTS tz_map(index bigint auto_increment, group_code VARCHAR(25) NOT NULL, key_code VARCHAR(25) NOT NULL, value1 VARCHAR(60), value2 VARCHAR(60), value3 VARCHAR(60)) ");
state.executeUpdate("CREATE UNIQUE INDEX IF NOT EXISTS uidx_tz_map ON tz_map( group_code, key_code );");
state.close();
conn.commit();
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
return e1.toString();
}
e.printStackTrace();
return e.toString();
}
return null ;
}
public tzDBMap openMap( String group ){
Statement lstmt_value = null ;
try {
lstmt_value = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
return null ;
}
return new tzDBMap( lstmt_value, group );
}
// public String put( String key, String value ){
//
// try {
// istat_data.executeUpdate("MERGE INTO tz_map( code, value1 ) KEY(code) VALUES('" + key + "', '" + value + "')");
//
// } catch (SQLException e) {
// e.printStackTrace();
// return e.toString();
// }
//
// return null ;
// }
}