当前位置:首页 > 开发 > 开源软件 > 正文

OLAP4J访问XML/A

发表于: 2013-06-05   作者:SuperCustomer   来源:转载   浏览:
摘要: 简介 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 co

简介

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

}

 

OLAP4J访问XML/A

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号