How to generate a JSON file from Mondrian output - mysql

I am new to Mondrian. I am using it in my project for OLAP operations.
I am testing it with Foodmart database.
The problem is that I need the OLAP operations results in JSON format.
I know that mondrian has the same structure as JSON in the form of hierarchies.
I want to generate a JSON file as an output from the result of mondrian MDX query.
The result should be similar to OLAP operations.
I don't know how to iterate over the result generated from MDX query.
Here is the code.
String connStr = "Provider=mondrian;" +
"Catalog=/WEB-INF/FoodMart.xml;" +
"JdbcDrivers=com.mysql.jdbc.Driver;" +
"Jdbc=jdbc:mysql://localhost/foodmart;" +
"jdbcUser=root;" +
"jdbcPassword=;";
String queryStr ="select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].>Store Sales]} ON COLUMNS,"+"Crossjoin(Hierarchize(Union({[Promotion Media].[All Media]}, >[Promotion Media].[All Media].Children)), {[Product].[All Products]})
ON ROWS"+" from [Sales]"+"where [Time].[1997]";
Connection connection = DriverManager.getConnection(connStr, null);
Query query = connection.parseQuery(queryStr);
Result result = connection.execute(query);
result.print(new PrintWriter(System.out));
Actually I need to perform OLAP operations on data warehouse which is stored in MySQL.
The resulted data should be in JSON format which I will pass to D3 http://mbostock.github.com/d3 for visualizations.
For data format I have to use JSON format.
Please any suggestions how to iterate MDX result and convert it in JSON file.
I am using Pentaho Mondrian for this purpose.
Thanks.

if you are working with PHP you could use this library to transform the xmla result into Json
http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/

Here's an example of what i suppose you want to do:
Class.forName("mondrian.olap4j.MondrianOlap4jDriver"); //load the driver
Connection connection = DriverManager.getConnection("Provider=mondrian;" +
"Catalog=/WEB-INF/FoodMart.xml;" +
"JdbcDrivers=com.mysql.jdbc.Driver;" +
"Jdbc=jdbc:mysql://localhost/foodmart;" +
"jdbcUser=root;" +
"jdbcPassword=;");
OlapWrapper wrapper = (OlapWrapper) connection;
OlapConnection olapConnection = wrapper.unwrap(OlapConnection.class);
CellSet cellSet = statement.executeOlapQuery(query);
CellSetAxis rows = cellSet.getAxes().get(1); //cube rows
CellSetAxis columns = cellSet.getAxes().get(0); //cube columns
int resultSize = rows.getPositionCount() * columns.getPositionCount();
String resultValues[] = new String[resultSize];
int valueIndex = 0;
for (Position row : rows) {
for (Position column : columns) {
Cell cell = cellSet.getCell(column, row);
String cellValue = cell.getFormattedValue();
resultValues[valueIndex++] = cellValue;
}
}
Gson gson = new Gson(); //gson library instance
String resultString = gson.toJson(responseValues); //json string
olapConnection.close();
connection.close();

Related

Parsing string data response

I am stuck in parsing a data response receiving from some third party vendor.
response is something like:-
data: ()(responseCode='A01', responseMessage='Approved', accountNumber='qwerty');
I have tried several ways of parsing/stringify but it does not provide me a JSON response. I tried weird combinations of Querystring functions as well but that did not help.
I am badly stuck in this.
I will post a workaround it might not be efficient but will give you the result.
var data = "data: ()(responseCode='A01', responseMessage='Approved', accountNumber='qwerty');";
var temp = data.substring(8,);
temp = temp.replace("(","{");
temp = temp.replace(")","}");
temp = temp.replace(/=/g,":");
temp = temp.replace(";","");
temp = eval('(' + temp + ')');
var Result = JSON.stringify(temp)
Result : {"responseCode":"A01","responseMessage":"Approved","accountNumber":"qwerty"}
You can use regex to convert it to a valid JSON structure.
let data = `data: ()(responseCode='A01', responseMessage='Approved', accountNumber='qwerty');`;
let modified = data
.replace(/\s/g,'')
.replace("data:()(",'{\'')
.replace(");",'}')
.replace(/=/g,"':")
.replace(/,/g,",'")
.replace(/'/g,'"')
let json = JSON.parse(modified);
console.log(json)

Load 3D model in Unity using Resource folder and Mysql

I want to load 3D model using Resource folder. I created an sql database to store the address. In this case I stored the file "deer-3ds" in folder "Models" and also save these information in a table named "modeladdress" in sql.
So please help me to correct my code. I know that it's 100% wrong but I dont know how to fix it. Thank you.
using UnityEngine;
using System.Collections;
using System;
using System.Data;
using Mono.Data.Sqlite;
public class addobject : MonoBehaviour {
// Use this for initialization
void Start () {
//GameObject deer=Instantiate(Resources.Load("deer-3d.bak",typeof(GameObject)))as GameObject;
// GameObject instance = Instantiate(Resources.Load("Models/deer-3ds", typeof(GameObject))) as GameObject;
string conn = "URI=file:" + Application.dataPath + "/modeladdress.s3db"; //Path to database.
IDbConnection dbconn;
dbconn = (IDbConnection) new SqliteConnection(conn);
dbconn.Open(); //Open connection to the database.
IDbCommand dbcmd = dbconn.CreateCommand();
string sqlQuery = "SELECT ordinary,foldername, filename " + "FROM modeladdress";
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader();
while (reader.Read ()) {
int ordinary = reader.GetInt32 (0);
string foldername = reader.GetString (1);
string filename = reader.GetString (2);
string path = foldername + "/" + filename;
//Debug.Log( "value= "+value+" name ="+name+" random ="+ rand);
GameObject instance = Instantiate(Resources.Load(path, typeof(GameObject))) as GameObject;
instance.SetActive (true);
}
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbconn.Close();
dbconn = null;
}
// Update is called once per frame
void Update () {
// GameObject instance = Instantiate(Resources.Load("Models/deer-3ds", typeof(GameObject))) as GameObject;
// instance.SetActive (true);
}
}
First of all, you are using SQLite at your database management system, not MySQL. Second, the way you have written your query,
string sqlQuery = "SELECT ordinary,foldername, filename " + "FROM modeladdress";
Will return the ordinary, foldername, and filename for every model. You need to use a WHERE clause to specify precisely which model you want to use. Thus, you need some way to know which model you want to query from the database before you actually execute the query, and in that case, why even query a database? You're going to have to store some unique identifier anyway so a database solves nothing.
Now concerning the actual code you have written, it appears to be correct (i.e. it should be returning what you want). The problem must be that either your table is empty, your values that are returned are incorrect, or that the object is being instantiated in an incorrect location and thus you are thinking it's not working. If you want a more concrete answer you'll have to comment on this answer with the specific problem you are facing (i.e. what specifically is "wrong"?).

hibernate query for Getting json key value pair with joins

I am using REST , hibernate query language for getting some data from database with the help of joins in that query i need to fetch columns from two or more tables and i am getting the result record , and when i return that object through REST as json i am getting the json as in the follwoing format
{"table1col1value","table2col1value","table3colvalue","table1col2value","table2col2value"}
but i need to get the json data in the following format
{"table1colname1":"table1col1value","table2colname":"table2col1value","table1colname":"table3colvalue","table1colname":"table1col2value","table2colname":"table2col2value"}
for that i am using the following code and its working fine, but its working with sql query only i need it in HQL , please help me in this.
#Override
#Transactional
public List<Map<String,Object>> getMixProperties(List<String> keys,Set<String> s) {
StringBuilder sb = new StringBuilder();
sb.append("select ");
Iterator<String> i = keys.iterator();
int q = keys.size();
while (i.hasNext()) {
q=q-1;
if(q==0){
String n= i.next();
sb.append(" "+n);
}
else{
String n2= i.next();
sb.append(" "+n2+",");}
}
sb.append(" from Book Book "
+ " join Systems Systems ON Systems.idSystems =Book.idSystems "
+ " join Machine Machine ON Machine.id =Systems.id ");
/*Iterator<String> iterator = s.iterator();
int q2 = s.size();String s5 = null;
while(iterator.hasNext()) {
String s4 = iterator.next();
q2=q2-1;
if(q2==0){
sb.append(" "+s4+" "+s4);
}
else{
s5=s4;
sb.append(" "+s4+" "+s4+",");}
}*/
System.out.println("query "+sb);
String sbb = sb.toString();
Query query=sessionFactory.getCurrentSession().createSQLQuery(sbb);
query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
List<Map<String,Object>> aliasToValueMapList=query.list();
return aliasToValueMapList;
}
in this above code List keys will be the column name of different tables and (set s will be table names, but i am not using it in current code)
even this is working only if i have both class and db table name are same and property and column names are same, if its not same then its not working.
I am getting JSON only if i have the pojo class , but in my case result set can be any number of columns so its depends on runtime only , please help me in this

Eclipselink JPA criteria format select date

I'm developing a web app with Spring 4 MVC and EclipseLink with MySQL. I'm currently stucked with a criteria query to get results with a date field. I have the following code:
CriteriaBuilder criteriaBuilder = entityManager().getCriteriaBuilder();
CriteriaQuery<Tuple> query = criteriaBuilder.createTupleQuery();
Root<SsiCheque> fromSsiCheque = query.from(SsiCheque.class);
List<Predicate> predicates = new ArrayList<Predicate>();
if(parameters.containsKey("cheNumero")){
System.out.println("Param " + parameters.get("cheNumero"));
predicates.add(criteriaBuilder.like(fromSsiCheque.<String>get("cheNumero"), "%" + parameters.get("cheNumero") + "%"));
}
if(parameters.containsKey("cheFechas")){
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String[] fechas = parameters.get("cheFechas").split(" - ");
try {
predicates.add(criteriaBuilder.between(fromSsiCheque.<Date>get("cheFecha"), formatter.parse(fechas[0]), formatter.parse(fechas[1])));
} catch (ParseException e) {
e.printStackTrace();
}
}
if(parameters.containsKey("cheReceptor")){
predicates.add(criteriaBuilder.like(fromSsiCheque.<String>get("cheReceptor"), "%" + parameters.get("cheReceptor") + "%" ));
}
query.multiselect(fromSsiCheque.get("cheId"), fromSsiCheque.get("cheNumero"),
fromSsiCheque.get("cheReceptor"), fromSsiCheque.get("cheMonto"),
criteriaBuilder.function("DATE_FORMAT",
String.class, fromSsiCheque.<Date>get("cheFecha"),
criteriaBuilder.literal("'%d/%m/%Y'")).alias("cheFecha"), fromSsiCheque.get("cheConcepto"))
.where(predicates.toArray(new Predicate[]{}));
TypedQuery<Tuple> typed = entityManager().createQuery(query);
The problem is in the multiselect section where I'm defining a date to be returned and formatted in dd/MM/yyyy with MySQL function DATE_FORMAT:
criteriaBuilder.function("DATE_FORMAT",
String.class, fromSsiCheque.<Date>get("cheFecha"),
criteriaBuilder.literal("'%d/%m/%Y'")).alias("cheFecha")
In some posts say the function is called TO_CHAR but it seems to be part of Oracle Database API.
The strange thing here is that I'm not getting errors (maybe I need to change logging level) but is not working.
Also I set the persistence.xml to show the generated sql and is as follows:
SELECT che_id, che_numero, che_receptor, che_monto, DATE_FORMAT(che_fecha, ?), che_concepto FROM ssi_cheque WHERE che_numero LIKE ?
bind => [%d/%m/%Y, %12%]
What Am I missing in my criteria query to show dates formatted?
Thanks
UPDATE
What I am getting from the database is 1416895200000 (the date field in milliseconds I guess).
Thanks

dynamic SQL execution and saving the result in flat file in SSIS

I want to create a SSIS package which writes a file with data generated by executing a SQL Statement. This generic package will be invoked by other packages passing in correct SQL as a variable.
Thus in the generic package :
I want to execute a dynamic SELECT query and fetch dynamic number of columns from a single database instance, the connection string does not per call and store the result into a flat file.
What would be an ideal way to accomplish this in SSIS.
What I tried :
The simplest solution that I could find was a writing a script task which would open a SQL connection , execute the SQL using SQLCommand, populate a datatable using the data fetched and write the contents directly to the file system using System.io.File and Release the connection.
I tried using OLE Database source with the SQLsupplied by a variable (with Validation set to false) and directing the rows into a Flat file connection. However due to the dynamic number and names of the columns I ran into errors.
Is there a more standard way of achieving this without using a script task?
How about this ... concatenate all field values into one field, and map AllFields to a field in a text file destination.
SELECT [f1]+',' + [f2] AS AllFields FROM [dbo].[A]
All of the "other"packages will know how to create the correct SQL. Their only contract with the "generic" package would be to eventually have only one field nameed "AllFields".
To answer your question directly, I do not think there is a "standard" way to do this. I believe the solution from Anoop would work well and while I have not tested the idea I wish I would have investigated it before writing my own solution. You should not need a script task in that solution...
In any case, I did write my own way to generate csv files from SQL tables that may run up against edge cases and need polishing but works rather well right now. I am looping through multiple tables before this task so the CurrentTable variable can be replaced with any variable you want.
Here is my code:
public void Main()
{
string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
try
{
string TableName = Dts.Variables["User::CurrentTable"].Value.ToString();
string FileDelimiter = ",";
string TextQualifier = "\"";
string FileExtension = ".csv";
//USE ADO.NET Connection from SSIS Package to get data from table
SqlConnection myADONETConnection = new SqlConnection();
myADONETConnection = (SqlConnection)(Dts.Connections["connection manager name"].AcquireConnection(Dts.Transaction) as SqlConnection);
//Read data from table or view to data table
string query = "Select * From [" + TableName + "]";
SqlCommand cmd = new SqlCommand(query, myADONETConnection);
//myADONETConnection.Open();
DataTable d_table = new DataTable();
d_table.Load(cmd.ExecuteReader());
//myADONETConnection.Close();
string FileFullPath = Dts.Variables["$Project::ExcelToCsvFolder"].Value.ToString() + "\\Output\\" + TableName + FileExtension;
StreamWriter sw = null;
sw = new StreamWriter(FileFullPath, false);
// Write the Header Row to File
int ColumnCount = d_table.Columns.Count;
for (int ic = 0; ic < ColumnCount; ic++)
{
sw.Write(TextQualifier + d_table.Columns[ic] + TextQualifier);
if (ic < ColumnCount - 1)
{
sw.Write(FileDelimiter);
}
}
sw.Write(sw.NewLine);
// Write All Rows to the File
foreach (DataRow dr in d_table.Rows)
{
for (int ir = 0; ir < ColumnCount; ir++)
{
if (!Convert.IsDBNull(dr[ir]))
{
sw.Write(TextQualifier + dr[ir].ToString() + TextQualifier);
}
if (ir < ColumnCount - 1)
{
sw.Write(FileDelimiter);
}
}
sw.Write(sw.NewLine);
}
sw.Close();
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception exception)
{
// Create Log File for Errors
//using (StreamWriter sw = File.CreateText(Dts.Variables["User::LogFolder"].Value.ToString() + "\\" +
// "ErrorLog_" + datetime + ".log"))
//{
// sw.WriteLine(exception.ToString());
//}
Dts.TaskResult = (int)ScriptResults.Failure;
throw;
}
Dts.TaskResult = (int)ScriptResults.Success;