検索操作において、検索した結果をある特定の要素の値を集計して取得したい場合があります。
場所を検索条件式に指定して、最も安い宿泊費、最も高い宿泊費、宿泊費の平均値を取得する例を用いて、Java APIの使用例を示します。
「2006年7月18日に宿泊可能なホテルのうち、最も安い宿泊費、最も高い宿泊費、宿泊費の平均値を地域別に集計して欲しい。」
年月日(2006年7月18日)を条件に指定します。また、取得する結果に集計式(min, max, avg)を指定し、集計結果を取得します。
以下にJava APIを使用したプログラミング例を示します。
import com.fujitsu.shun.ShunConnection; import com.fujitsu.shun.ShunPreparedStatement; import com.fujitsu.shun.ShunResultSet; import com.fujitsu.shun.common.ShunException; /*** 指定された検索条件に該当するデータを集計して取得します ***/ public class JavaAPISample5 { public static void main(String[] args) { ShunConnection con = null; ShunPreparedStatement pstmt = null; ShunResultSet rs = null; try { // 検索条件式 String sQuery = "/document/information/date == '2006年07月18日'"; // リターン式 String sReturn = "/document/base/prefecture/text(), min(/document/base/price/text()), max(/document/base/price/text()), avg(/document/base/price/text())"; // ソート式 String sSort = "/document/base/prefecture/text()"; // ShunConnectionオブジェクトを作成 con = new ShunConnection("DirSvr1", 23101); // 検索式を指定し、ShunPreparedStatementオブジェクトを作成 pstmt = con.prepareSearch(sQuery, sReturn); // ソート式を指定 pstmt.setSort(sSort); //最大取得件数を設定 pstmt.setRequest(1, 30); //検索を実行し、ShunResultSetオブジェクトを作成 rs = pstmt.executeSearch(); // 検索条件に該当するデータを取得 int counter = 0; String[][] result; while (rs.next()) { counter++; System.out.println("[結果]" + counter + "件目:"); result = rs.getStringArray(); System.out.println(" 地域 :" + result[0][0]); System.out.println(" 最も安い宿泊費:" + result[1][0]); System.out.println(" 最も高い宿泊費:" + result[2][0]); System.out.println(" 宿泊費の平均値:" + result[3][0]); } rs.close(); pstmt.close(); con.close(); } // アプリケーション実行時にエラーが発生した場合の処理 catch (ShunException ex) { int errorLevel = ex.getErrLevel(); switch( errorLevel ) { case ShunException.SHUN_ERROR : System.out.println("エラーレベル :SHUN_ERROR"); break; case ShunException.SHUN_ERROR_CONNECTION_TERMINATED : System.out.println("エラーレベル :SHUN_ERROR_CONNECTION_TERMINATED"); break; } System.out.println("エラーメッセージ:" + ex.getMessage()); ex.printStackTrace(); } catch (Exception ex) { System.out.println("エラーメッセージ:" + ex.getMessage()); ex.printStackTrace(); } finally { try { if (rs != null) rs.close(); } catch (ShunException ex) { int errorLevel = ex.getErrLevel(); switch( errorLevel ) { case ShunException.SHUN_ERROR : System.out.println("エラーレベル :SHUN_ERROR"); break; case ShunException.SHUN_ERROR_CONNECTION_TERMINATED : System.out.println("エラーレベル :SHUN_ERROR_CONNECTION_TERMINATED"); break; } System.out.println("エラーメッセージ:" + ex.getMessage()); ex.printStackTrace(); } try { if (pstmt != null) pstmt.close(); } catch (ShunException ex) { int errorLevel = ex.getErrLevel(); switch( errorLevel ) { case ShunException.SHUN_ERROR : System.out.println("エラーレベル :SHUN_ERROR"); break; case ShunException.SHUN_ERROR_CONNECTION_TERMINATED : System.out.println("エラーレベル :SHUN_ERROR_CONNECTION_TERMINATED"); break; } System.out.println("エラーメッセージ:" + ex.getMessage()); ex.printStackTrace(); } try { if (con != null) con.close(); } catch (ShunException ex) { int errorLevel = ex.getErrLevel(); switch( errorLevel ) { case ShunException.SHUN_ERROR : System.out.println("エラーレベル :SHUN_ERROR"); break; case ShunException.SHUN_ERROR_CONNECTION_TERMINATED : System.out.println("エラーレベル :SHUN_ERROR_CONNECTION_TERMINATED"); break; } System.out.println("エラーメッセージ:" + ex.getMessage()); ex.printStackTrace(); } } } } |
[結果]1件目: 地域 :神奈川 最も安い宿泊費:6000 最も高い宿泊費:8000 宿泊費の平均値:7000 [結果]2件目: 地域 :大阪 最も安い宿泊費:6000 最も高い宿泊費:9000 宿泊費の平均値:7500 |