2013-02-06 168 views
1

事实表连接两张表:
如何利用番石榴

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 

尺寸表:

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

我用番石榴表是这样的:

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); 
    // ...... 

我想要实现a LEFT JOIN like:

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 

我该怎么办?

+0

数据来自哪里?它是一个数据库吗?是否有可能在查询中进行连接? – maksimov

+1

'table1.putAll(table2);'? – assylias

+0

也许一些来自MySQL的diff数据源和另一个来自Excel - –

回答

3

番石榴的Table不应该像任何SQL的表一样使用,因为它是一个集合。 SQL的表格被设计为可索引,可排序,可过滤等。番石榴的Table只有这些的一小部分,并且只是间接的,并且关节不是它们的一部分(除非你玩转换)。

你需要做的是让你的两个表遍历table的元素并在table1中找到相应的映射。

在你的情况,我相信你用List代替table和番石榴Table代替table1更好。循环访问列表,并在获取元素时制作最终对象。

+0

+1同意。 Guava中的'Table'用于描述数据结构,如Map >'。 –