简体   繁体   English

如何在Guava中表示SQL表和SELECT查询?

[英]How to represent SQL Table and SELECT Query in Guava?

Consider a typical Customers table: 考虑一个典型的客户表:

客户表

And a typical SQL pattern matching query: 和典型的SQL模式匹配查询:

SELECT FirstName, LastName, City 

FROM Customers 

WHERE FirstName LIKE '%ab%'

OR LastName LIKE '%an%'

How can we represent this table and SQL SELECT query using Google Guava Library Table collection and Predicates ? 我们如何使用Google Guava库集合和谓词表示该表和SQL SELECT查询?

EDIT 编辑

As mentioned by mfulton26, the Guava Table is probably not the most ideal data structure equivalent to a database table. 如mfulton26所述,番石榴表可能不是与数据库表等效的最理想的数据结构。

So, which is the most appropriate data structure for an in memory data table capable of providing: 因此,这是内存数据表中最合适的数据结构,它可以提供:

1) Iteration (probably with Iterators) 1)迭代(可能使用迭代器)

2) Filter (Probably with Predicates) 2)筛选器(可能带有谓词)

3) Multiple data columns with indexing for fast access. 3)具有索引的多个数据列,用于快速访问。

Table

public class CustomerTable {

    public enum Column {
        FIRST_NAME, LAST_NAME, ADDRESS_LINE1, CITY, STATE_PROVINCE_CD, POSTAL_CODE;
    }

    private Table<Integer, Column, String> table = HashBasedTable.create();

    @Override
    public String toString() {
        return table.toString();
    }

    public void createRow(String[] values) {
        if (Column.values().length != values.length) {
            throw new IllegalArgumentException();
        }
        Integer rowNum = table.rowKeySet().size() + 1;
        for(int i=0; i < values.length; i ++) {
            table.put(rowNum, Column.values()[i], values[i]);
        }
    }

    public Table<Integer, Column, String> query(Predicate<Map<Column, String>> query) {
        return query(query, allOf(Column.class));
    }

    public Table<Integer, Column, String> query(Predicate<Map<Column, String>> query, EnumSet<Column> columns) {
        Map<Integer, Map<Column, String>> filtered = Maps.filterValues(table.rowMap(), query);
        return createResultTable(filtered, columns);
    }

    private Table<Integer, Column, String> createResultTable(Map<Integer, Map<Column, String>> resultMap, final EnumSet<Column> columns) {

        int i = 0;
        Table<Integer, Column, String> result = HashBasedTable.create();
        for (Map<Column, String> row : resultMap.values()) {
            i++;
            for (Column column : row.keySet()){
                if (columns.contains(column)) {
                    result.put(i, column, row.get(column));
                }
            }
        }
        return result;
    }

Predicate 谓语

class LikePredicate implements Predicate<Map<CustomerTable.Column, String>> {

    private Column column;
    private String value;

    public LikePredicate(Column column, String value) {
        this.column = column;
        this.value = value;
    }

    @Override
    public boolean apply(Map<Column, String> input) {
        return input.get(column) != null && input.get(column).contains(value);
    }

    public static LikePredicate like(Column column, String value) {
        return new LikePredicate(column, value);
    }
}

Usage example 使用范例

public static void main(String[] args) {

    CustomerTable customerTable = new CustomerTable();
    customerTable.createRow(new String[]{"Ben", "Miller", "101 Candy Rd.", "Redmond", "WA", "98052"});
    customerTable.createRow(new String[]{"Garret", "Vargas", "10203 Acorn Avenue", "Calgary", "AB", "T2P 2G8"});
    //Create other rows or read rows from a file


    Table<Integer, Column, String> result;
    /*
    SELECT FirstName, LastName, City 
    FROM Customers 
    WHERE FirstName LIKE '%ab%'
          OR LastName LIKE '%an%'
    */
    result = customerTable.query(or(like(Column.FIRST_NAME, "ab"), like(Column.LAST_NAME, "an")),
            EnumSet.of(Column.FIRST_NAME, Column.LAST_NAME, Column.CITY));

    System.out.println(result);
}

Note that a SQL Table is not usually mapped to a Guava Table. 请注意,SQL表通常不映射到Guava表。 Guava's Table is for when you need two indexes (see NewCollectionTypesExplained · google/guava Wiki ). 番石榴表适用于需要两个索引的情况(请参阅NewCollectionTypesExplained·google / guava Wiki )。 A SQL Table Result Set is usually represented as a simple Collection or Iterable . SQL表结果集通常表示为简单的CollectionIterable

With that said, here is how you can "query" a Guava table: 话虽如此,这是如何“查询” Guava表的方法:

Java 8 Java 8

table.rowMap().values().stream().filter(row -> {
    return row.get("FirstName").contains("ab") || row.get("LastName").contains("an");
});

Java 7/6 Java 7/6

FluentIterable.from(table.rowMap().values()).filter(new Predicate<Map<String, String>>() {
    @Override
    public boolean apply(Map<String, String> row) {
        return row.get("FirstName").contains("ab") || row.get("LastName").contains("an");
    }
});

Also note that Guava's Table is not like a database table where you can add additional indexes, etc. you only get two indexes: row and column. 还要注意,Guava的Table不像可以添加其他索引的数据库表,等等。您只能得到两个索引:行和列。

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

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