简体   繁体   English

如何在Mondrian 4模式中使用PostgreSQL的时间戳?

[英]How to use Timestamp from PostgreSQL in Mondrian 4 Schema?

In my fact table (from PostgreSQL) i have a timestamp that looks like this: 在我的事实表(来自PostgreSQL)中,我有一个像这样的时间戳:

2016-07-01 2016-07-01

How can i use this timestamp to show a Year/Month/Quarter/Day dimension? 如何使用此时间戳显示年/月/季/日维度? A Mondrian 4 Schema example would be helpfull. 一个Mondrian 4模式示例将很有帮助。

I don't want to use a extra time table or something like that. 我不想使用额外的时间表或类似的东西。 Just the timestamp. 只是时间戳记。

I found a way to solve this requirement. 我找到了解决此要求的方法。

The problem consisted of three different problems 该问题包括三个不同的问题

  1. Get subfields as year, month or ... from a timestamp 从时间戳获取年,月或...的子字段
  2. Use a fact table column as dimension 使用事实表列作为维度
  3. Use a hierarchy/multiple attributes in a fact table based dimension 在基于事实表的维度中使用层次结构/多个属性

I solved all three problems, as described below, but none of the solutions is a perfect in my eyes, so if you have any improvement suggestions, pls let me know in a comment below. 如下所述,我解决了所有三个问题,但是在我看来,这些解决方案都不是完美的,因此,如果您有任何改进建议,请在下面的评论中告诉我。 Everything described below is for the metamodel version 4.0 and a PostgreSQL database. 下面描述的所有内容都是针对元模型版本4.0和PostgreSQL数据库的。


1. Get subfields as year, month or ... from a timestamp 1.从时间戳获取子字段为年,月或...

I couldn't find a integrated solution and therefor made my own. 我找不到集成的解决方案,因此我制定了自己的解决方案。 In the Mondrian Schema you can define calculated columns for a table. 在Mondrian架构中,您可以为表定义计算列。

<Table name="sales" schema="reporting">
    <ColumnDefs>
        <CalculatedColumnDef name='store2'>
            <ExpressionView>
                <SQL dialect='generic'>
                    <Column name='store'/>
                </SQL>
            </ExpressionView>
        </CalculatedColumnDef>
    </ColumnDefs>
</Table>

Somehow mondrian allways uses generic as dialect and therefor postgres as dialect doesn't work. 蒙德里安总是以某种方式使用generic作为方言,因此使用postgres作为方言不起作用。 With <Column name='column'/> you can use a column value of the same table. 通过<Column name='column'/>您可以使用同一表的列值。 In the example above, it's the store column of the sales table. 在上面的示例中,它是sales表的store列。

We can use the same method to add a year, month, ... column for every subfield we want out of the timestamp. 我们可以使用相同的方法为时间戳之外的每个子字段添加年,月,...列。

For quarter it looks like this: 对于季度,它看起来像这样:

<CalculatedColumnDef name='quarter'>
    <ExpressionView>
        <SQL dialect='generic'>
            'Q' || EXTRACT(QUARTER FROM <Column name='date'/>)
        </SQL>
    </ExpressionView>
</CalculatedColumnDef>

Now you only have to do this for every subfield you need. 现在,您只需要为所需的每个子字段执行此操作。 All supported subfield from PostgreSQL: PostgreSQL - Date/Time Function EXTRACT PostgreSQL支持的所有子字段: PostgreSQL-日期/时间函数EXTRACT


2. Use a fact table column as dimension 2.使用事实表列作为维

A dimension with no defined table is not possible in the mondrian schema. 没有定义表的维在mondrian模式中是不可能的。 Some would think that the schema would just assume the fact table as default table but the mondrian schema does not. 有人会认为该架构将假设事实表为默认表,而mondrian架构则不会。

How to solve this is not stated in the mondrian documentation. mondrian文档中未说明如何解决此问题。 But it's just as simple as using a different link for the DimensionLinks . 但这就像为DimensionLinks使用其他链接一样简单。

Use this link (where xy schould be the dimension name): 使用此链接(其中xy应该是尺寸名称):

<FactLink dimension="xy"/>

3. Use a hierarchy/multiple attributes in a fact table based dimension 3.在基于事实表的维度中使用层次结构/多个属性

At this point it's really confusing what Mondrian does. 在这一点上,这真让Mondrian感到困惑。 With only 1., 2. and a hierarchical dimension Mondrian crashes and says: 只有1、2和一个层次结构维度,Mondrian崩溃并说:

Dimension 'xy'; 尺寸'xy'; omits a defined key, which is only valid for degenerate dimensions with a single attribute. 省略定义的键,该键仅对具有单个属性的退化尺寸有效。

That doesn't make any sense to me and the solution does make no sense at all. 这对我来说没有任何意义,解决方案也没有任何意义。

Just add a key to the dimension and a corresponding key attribute and it works. 只需将一个key添加到维度和相应的键属性即可。 No idea why! 不知道为什么!


Use all subfields in a dimension 使用维度中的所有子字段

The Mondrian documentation does recomment to use the dimension type TIME and the corresponding attribute levelType 's. Mondrian文档确实建议使用维类型TIME和相应的属性levelType

Out of the documentation: 在文档之外:

Time dimensions based on year/month/week/day are coded differently in the Mondrian schema due to the MDX time related functions 由于与MDX时间相关的功能,在Mondrian模式中对基于年/月/周/日的时间维度进行了不同的编码

For me, that made no difference at all, but still i have included it in my dimension: 对我来说,这根本没有什么区别,但是我仍然将其包括在我的维度中:

<Dimension name='Time' key="Timestamp" type="TIME">
    <Attributes>
        <Attribute name='Timestamp' table='sales' keyColumn='slice_date' hasHierarchy="false"/>
        <Attribute name='Year' table='sales' keyColumn='year' levelType="TimeYears" hasHierarchy='false'/>
        <Attribute name='Quarter' table='sales' keyColumn='quarter' levelType="TimeQuarters" hasHierarchy='false'/>
        <Attribute name='Month' table='sales' keyColumn='month' levelType="TimeMonths" hasHierarchy='false'/>
        <Attribute name='Day' table='sales' keyColumn='day' levelType="TimeWeeks" hasHierarchy='false'/>
        <Attribute name='Week' table='sales' keyColumn='week' levelType="TimeDays" hasHierarchy='false'/>
        <Attribute name='Day of Week' table='sales' keyColumn='dayOfWeek' levelType="TimeWeeks" hasHierarchy='false'/>
    </Attributes>
    <Hierarchies>
        <Hierarchy name='Monthly'>
            <Level attribute='Year'/>
            <Level attribute='Quarter'/>
            <Level attribute='Month'/>
        </Hierarchy>
        <Hierarchy name='Weekly'>
            <Level attribute='Year'/>
            <Level attribute='Week'/>
            <Level attribute='Day of Week'/>
        </Hierarchy>
    </Hierarchies>
</Dimension>

Now you only have to use this dimension in a cube with the, in 2. decribed, link: 现在,您只需在带有2.中描述的链接的多维数据集中使用此维度:

<Dimensions>
    <Dimension source="Time"/>
</Dimensions>

I hope this does help someone else. 我希望这对其他人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM