API-JDBC 사용법 ========================= 개요 ------------------------- JDBC버전 ^^^^^^^^^^^^^^^^^^^^^^^^^ IRIS 2.1에서 사용되는 JDBC는 버전은 mobigen-iris-jdbc-2.1.0.2.jar파일이며, 다른 버전에서 사용중인 JDBC와는 호환이 안될 수 있습니다. 필요 파일 ^^^^^^^^^^^^^^^^^^^^^^^^^ IRIS JDBC를 사용하기 위해서 다음 jar 파일이 제공 됩니다. - mobigen-iris-jdbc-2.1.0.2.jar `다운로드 `_ 이 jar 파일을 java의 class path 에 설정하여 사용하시면 됩니다. 지원 쿼리 ^^^^^^^^^^^^^^^^^^^^^^^^^ JDBC에서 지원하는 쿼리의 종류는 다음과 같습니다. - CREATE TABLE - CREATE INDEX - INSERT - UPDATE - DELETE - LOAD - SELECT - DROP INDEX - DROP TABLE 기본 사용법 ------------------------- 이 장에서는 쿼리를 사용하기 위해 JDBC를 이용한 IRIS의 접속 방법 및 예제 코드를 설명 합니다. 기본 구조 ^^^^^^^^^^^^^^^^^^^^^^^^^ .. code:: java String url = "jdbc:iris://{master_node_ip}:5050/{database_name}"; Properties info = /* 계정정보 입력 */; try { Class.forName("com.mobigen.iris.jdbc.IRISDriver"); } catch (java.lang.ClassNotFoundException e) { e.printStackTrace(); return; } Connection conn = null; try { conn = DriverManager.getConnection(url, info); Statement stmt = conn.createStatement(); boolean rs = stmt.execute(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } - Class.forName("com.mobigen.iris.jdbc.IRISDriver"); - IRIS JDBC를 사용하기 위해 동적으로 클래스를 로딩합니다. - DriverManager.getConnection(url, info); - IRIS에 접속하기 위해 host및 계정정보를 입력합니다. - 계정정보는 다음과 같이 두가 지 방식으로 입력이 가능합니다. .. code:: java getConnection(String url, Properties info): Connection - DriverManager getConnection(String url, String user, String password): Connection - DriverManager - Properties 클래스를 이용하는 방법 .. code:: java Properties info = new Properties(); info.setProperty("user", "test"); info.setProperty("password", "test"); info.setProperty("direct", "false"); DriverManager.getConnection("jdbc:iris://master_ip:port/database_name", info); - 직접 입력하는 방법(direct접속은 불가능) .. code:: java DriverManager.getConnection("jdbc:iris://master_ip:port/database_name", "id", "passwd"); MASTER_IP ^^^^^^^^^^^^^^^^^^^^^^^^^ IP는 IRIS의 Master IP를 의미한다. PORT ^^^^^^^^^^^^^^^^^^^^^^^^^ IRIS에서 사용하는 Port는 일반 접속, Direct접속에 따라 다르게 설정을 해야 하며, 기본적인 Port는 다음과 같다. - 일반 접속 - 5050: 일반 접속을 사용할 경우에 사용하는 Port - Direct접속 - 5000 : Direct접속을 사용할 경우에 사용되는 Port - Direct접속을 시도할 경우에는setProperty("direct", "true")를 꼭 사용해야 한다. database name ^^^^^^^^^^^^^^^^^^^^^^^^^ 접속하고자 하는 database name을 설정합니다. 예제코드 ------------------------- 이 장에서는 예제코드를 이용하여 JDBC 사용법을 설명합니다. 모든 쿼리들은 Exception이 발생하지 않을 경우 성공으로 간주합니다. CREATE[table,index]/UPDATE/INSERT/DELETE/DROP[table,index] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code:: java Connection conn = null; try { conn = DriverManager.getConnection(url, info); Statement stmt = conn.createStatement(); boolean rs = stmt.execute("QUERY"); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } INSERT ^^^^^^^^^^^^^^^^^^^^^^^^^ Insert 쿼리의 경우에는 위에서 사용한 createStatement를 이용하는 방법과 PreparedStatement를 이용하는 방법이 존재 합니다. - createStatement .. code:: java Connection conn = null; try { conn = DriverManager.getConnection(url, info); Statement stmt = conn.createStatement(); boolean rs = stmt.execute("QUERY"); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } - PreparedStatement .. code:: java Connection conn = null; try { conn = DriverManager.getConnection(url, info); PreparedStatement pstmt; pstmt = conn.prepareStatement( "INSERT INTO table (k, p, a) VALUES (?, ?, ?);" ); pstmt.setString(1, "1"); pstmt.setString(2, "20110616000000"); pstmt.setString(3, "aaaa"); conn.close(); } catch (SQLException e) { e.printStackTrace(); } SELECT ^^^^^^^^^^^^^^^^^^^^^^^^^ Select 쿼리의 경우에는 위에서 사용한 createStatement를 이용하는 방법과 PreparedStatement를 이용하는 방법이 존재 합니다. - CreateStatement .. code:: java try { conn = DriverManager.getConnection(url, info); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery( "select k, p, a from table;" ); for( int i = 1 ; i <= rs.getRow() ; i++ ) { rs.next(); System.out.println( rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) ); } rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } - PreparedStatement .. code:: java try { conn = DriverManager.getConnection(url, info); PreparedStatement pstmt; pstmt = conn.prepareStatement( "select k, p, a from table where k = ?;" ); pstmt.setString(1, "k2"); ResultSet rs = pstmt.executeQuery(); for( int i = 1 ; i <= rs.getRow() ; i++ ) { rs.next(); System.out.println( rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) ); } rs.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } LOAD ^^^^^^^^^^^^^^^^^^^^^^^^^ LOAD에 성공할 경우 +OK SUCCESS. success count : 4 와 같은 결과를 얻을 수 있습니다. 이 경우 마지막 4의 의미는 4개의 Record가 Load되었다는 의미 입니다. .. code:: java Connection conn = null; try { conn = DriverManager.getConnection(url, info); IRISStatement stmt = (IRISStatement)conn.createStatement(); stmt.SetFieldSep(","); stmt.SetRecordSep("\n"); String table = "LOCAL_TEST_TABLE_JDBC"; String key = "K"; String partition = "20140101000000"; String control_file_path = "LOCAL_TEST_TABLE.ctl"; String data_file_path = "LOCAL_TEST_TABLE.dat"; String result = stmt.Load( table, key, partition, control_file_path, data_file_path ); System.out.println(result); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } IRIS Query ------------------------- IRIS에서는 기본적인 ANSI SQL의 거의 동일하게 지원을 하지만, CREATE TABLE의 경우에는 IRIS에서 사용하는 옵션을 추가로 적어 주어야 합니다. CREATE TABLE ^^^^^^^^^^^^^^^^^^^^^^^^^ .. code:: java CREATE TABLE {table_name} ( {colnum_name} {type}, {colnum_name} {type}, {colnum_name} {type}, … {colnum_name} {type} ) datascope [ LOCAL | GLOBAL ] ramexpire [ n > 0 | 0 ] diskexpire [ n > 0 | 0 ] partitionkey [ key_column_name | NONE ] partitiondate [ part_column_name | NONE ] partitiondate [ n > 0 | 0 ] ; 주의사항 ------------------------- 모든 JDBC의 Connection은 쿼리가 종료 후 재접속 해야 합니다. 즉, 두개의 쿼리를 실행할 경우 첫번째 쿼리의 결과를 얻은 후 Connection을 종료후, Connection을 재 접속 후 두번째 쿼리를 실행해야 합니다. JDBC상에서 쿼리실행도중 에러가 발생할 경우 당시에 사용된 Connection은 강제로 종료가 됩니다. 따라서, 에러가 발생할 경우 재 접속을 해주어야 합니다.