简体   繁体   English

如何使用Guava连接两个表

[英]How to join two tables using Guava

Fact table : 事实表:

Id Year Month countryId Sales
1  1999 1     1         3000
2  1999 2     1         2300
3  2000 3     2         3999
4  2000 4     3         2939

Dimension table: 尺寸表:

Id country province
1  US      LA
2  US      CA
3  US      GA
4  EN      LN

and I use Guava table like this : 我像这样使用Guava表:

Table<Integer, String, Object> table = Tables.newCustomTable(
        Maps.<Integer, Map<String, Object>> newLinkedHashMap(),
        new Supplier<Map<String, Object>>() {
            public Map<String, Object> get() {
                return Maps.newLinkedHashMap();
            }
        });

    table.put(1, "Year", 1999);
    table.put(1, "Month", 1);
    table.put(1, "countyId", 1);
    table.put(1, "Sales", 3000);
    // ...... etc


    table1.put(1, "county", "US");
    table1.put(1, "provice", 1999);
    // ......

I want to implement a LEFT JOIN like: 我想实现LEFT JOIN如:

1 1999 1 1 3000 US LA
2 1999 2 1 2300 US LA
3 2000 3 2 3999 US CA
4 2000 4 3 2939 EN LN

What should I do? 我该怎么办?

Guava's Table isn't supposed to be used like any SQL's table, as it is a collection. Guava的Table不应该像任何SQL表一样使用,因为它是一个集合。 SQL's tables are designed to be indexable, sortable, filterable, etc. Guava's Table has only a fraction of those and only indirectly, and joints aren't part of them (unless you play with transformations). SQL的表被设计为可索引,可排序,可过滤等。番石榴的Table只有一小部分,只是间接的,关节不是它们的一部分(除非你使用转换)。

What you need to do is to have your two tables and loop through the elements of table and find the corresponding mapping in table1 . 你需要做的是通过的元素让你的两个表和循环table并找到相应的映射table1

In your case, I believe you're better off with a List replacing table and a Guava Table for table1 . 在你的情况下,我相信你最好使用List替换tabletable1的Guava Table Loop through the list and make your final objects as you get your elements. 循环遍历列表,并在获取元素时制作最终对象。

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

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