java結合kylin的jdbc實現查詢

kylin支持直接的sql查詢。那就意味這我們可以直接使用平時使用的sql從kylin中查出需要的數據

方法如下:

1.statenment的query查詢

/**
	 *
	 * @param sql   查詢的語句
	 * @param projectName   kylin內工程的名字
	 * @return
	 * @throws Exception
	 */
	public static ResultSet queryKylin(String sql,String projectName) throws Exception {
		ResultSet resultSet=null;
		Connection conn=null;
		try {
			System.setProperty("user.timezone","GMT +08");
			// 加載Kylin的JDBC驅動程序
			Driver driver = (Driver) Class.forName("org.apache.kylin.jdbc.Driver").newInstance();
			// 配置登錄Kylin的用戶名和密碼
			Properties info= new Properties();
			info.put("user",userName);
			info.put("password",password);
			// 連接Kylin服務
			String connectStr="jdbc:kylin://"+host+":"+port+"/"+projectName;
			conn= driver.connect(connectStr, info);
			Statement state= conn.createStatement();
			if(sql!=null && !"".equals(sql)){
				System.out.println(projectName+"===="+sql);
				resultSet =state.executeQuery(sql);
			}else{
                 System.out.println("=======sql爲空=======");
			}
		}catch (Exception e){
              throw new Exception("連接kylin報錯======"+e.getMessage());
		}finally {
			if(conn!=null){
				conn.close();
			}
			return  resultSet;
		}
	}

2.preparedstatment的query查詢

/**
	 *
	 * @param sql   查詢的語句
	 * @param projectName   kylin內工程的名字
	 * @return
	 * @throws Exception
	 */
	public static ResultSet queryKylin(String sql,String projectName) throws Exception {
		ResultSet resultSet=null;
		Connection conn=null;
		try {
			System.setProperty("user.timezone","GMT +08");
			// 加載Kylin的JDBC驅動程序
			Driver driver = (Driver) Class.forName("org.apache.kylin.jdbc.Driver").newInstance();
			// 配置登錄Kylin的用戶名和密碼
			Properties info= new Properties();
			info.put("user",userName);
			info.put("password",password);
			// 連接Kylin服務
			String connectStr="jdbc:kylin://"+host+":"+port+"/"+projectName;
			conn= driver.connect(connectStr, info);
			
			if(sql!=null && !"".equals(sql)){
				System.out.println(projectName+"===="+sql);
				PreparedStatement state= conn.prepareStatement(sql);
                state.setInt(1,10);
                 ResultSet resultSet=state.executeQuery();
			}else{
                 System.out.println("=======sql爲空=======");
			}
		}catch (Exception e){
              throw new Exception("連接kylin報錯======"+e.getMessage());
		}finally {
			if(conn!=null){
				conn.close();
			}
			return  resultSet;
		}
	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章