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;
		}
	}

 

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