OLAP4J访问XML/A

17 篇文章 0 订阅
10 篇文章 0 订阅

简介

olap4j is a common API for any OLAP server, so you can write an analytic application on one server and easily switch it to another. Built on that API, there is a growing collection of tools and components.

 

安装

下载地址:http://www.olap4j.org/

 

代码

import java.sql.DriverManager;
import java.sql.SQLException;

import org.olap4j.OlapConnection;
import org.olap4j.OlapException;
import org.olap4j.OlapWrapper;

public class OlapConnUtil {

	private static final String DRIVER_CLASS_NAME = "org.olap4j.driver.xmla.XmlaOlap4jDriver";

	static {
		try {
			Class.forName(DRIVER_CLASS_NAME);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static OlapConnection getOlapConn(String server, String catalog,
			String user, String password) throws SQLException {
		String url = "jdbc:xmla:Server=" + server;

		OlapConnection conn = null;
		try {
			conn = (OlapConnection) DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			throw e;
		}

		if (conn != null) {
			try {
				conn.setCatalog(catalog);
			} catch (OlapException e) {
				throw e;
			}

			OlapWrapper wrapper = (OlapWrapper) conn;
			OlapConnection olapConn = null;
			try {
				olapConn = wrapper.unwrap(OlapConnection.class);
				return olapConn;
			} catch (SQLException e) {
				throw e;
			}
		}

		return null;
	}

}

 

import java.io.PrintWriter;
import java.sql.SQLException;

import org.olap4j.Cell;
import org.olap4j.CellSet;
import org.olap4j.OlapConnection;
import org.olap4j.OlapException;
import org.olap4j.OlapStatement;
import org.olap4j.Position;
import org.olap4j.metadata.Member;

public class MdxQueryUtil {

	private static final String PARAM_KEY_SERVER = "server";
	private static final String PARAM_KEY_USER = "user";
	private static final String PARAM_KEY_PASSWORD = "password";
	private static final String PARAM_KEY_CATALOG = "catalog";
	private static final String PARAM_KEY_MDX = "mdx";

	private static final int RETRY_TIMES = 60;
	
	public static void main(String[] args) {
		String server = System.getProperty(PARAM_KEY_SERVER);
		String user = System.getProperty(PARAM_KEY_USER);
		String password = System.getProperty(PARAM_KEY_PASSWORD);
		String catalog = System.getProperty(PARAM_KEY_CATALOG);
		String mdx = System.getProperty(PARAM_KEY_MDX);
		try {
			print(query(server, catalog, user, password, mdx), new PrintWriter(System.out));
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private static OlapConnection getOlapConn(String server, String catalog,
			String user, String password, int retry) {
		int temp = retry;
		OlapConnection olapConn = null;
		while (olapConn == null && retry-- > 0) {
			try {
				if (temp > retry) {
					try {
						Thread.sleep(1000 * 3);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				olapConn = OlapConnUtil.getOlapConn(server, catalog, user, password);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return olapConn;

	}

	private static CellSet query(String server, String catalog, String user,
			String password, String mdx) throws SQLException {
		OlapConnection olapConn = getOlapConn(server, catalog, user, password, RETRY_TIMES);

		if (olapConn != null) {
			OlapStatement stmt = null;
			try {
				stmt = olapConn.createStatement();
			} catch (OlapException e) {
				throw e;
			}

			CellSet cellSet = null;

			if (stmt != null) {
				try {
					cellSet = stmt.executeOlapQuery(mdx);
				} catch (OlapException e) {
					throw e;
				}
			}

			if (cellSet != null) {
				try {
					cellSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}

			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					stmt = null;
				}
			}

			try {
				olapConn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				olapConn = null;
			}

			return cellSet;
		}

		return null;
	}

	private static void print(CellSet cellSet, PrintWriter writer) {
		if (cellSet != null && cellSet.getAxes().size() == 2) {
			for (Position row : cellSet.getAxes().get(1)) {
				for (Member member : row.getMembers()) {
					writer.print(member.getName() + "\t");
				}
				for (Position column : cellSet.getAxes().get(0)) {
					final Cell cell = cellSet.getCell(column, row);
					writer.print(cell.getFormattedValue() + "\t");
				}
				writer.println();
			}
			writer.flush();
		}
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值