Java 如何在Guava中表示SQL表和选择查询?
考虑一个典型的客户表: 和一个典型的SQL模式匹配查询: 我们如何使用Google Guava库表集合和谓词表示这个表和SQL SELECT查询 编辑 正如mfulton26所提到的,Guava表可能不是与数据库表等效的最理想的数据结构 那么,对于能够提供以下功能的内存中数据表,哪种数据结构最合适: 1次迭代可能使用迭代器 2可能使用谓词进行筛选 3个带索引的多数据列,可快速访问。表 谓词 用法示例Java 如何在Guava中表示SQL表和选择查询?,java,sql,pattern-matching,guava,predicate,Java,Sql,Pattern Matching,Guava,Predicate,考虑一个典型的客户表: 和一个典型的SQL模式匹配查询: 我们如何使用Google Guava库表集合和谓词表示这个表和SQL SELECT查询 编辑 正如mfulton26所提到的,Guava表可能不是与数据库表等效的最理想的数据结构 那么,对于能够提供以下功能的内存中数据表,哪种数据结构最合适: 1次迭代可能使用迭代器 2可能使用谓词进行筛选 3个带索引的多数据列,可快速访问。表 谓词 用法示例 请注意,SQL表通常不会映射到Guava表。番石榴的表格是当你需要两个索引时使用的,请参见。SQ
请注意,SQL表通常不会映射到Guava表。番石榴的表格是当你需要两个索引时使用的,请参见。SQL表结果集通常表示为简单集合或Iterable 话虽如此,下面是查询番石榴表的方法: 爪哇8 table.rowMap.values.stream.filterrow->{ 返回row.getFirstName.containsab | | row.getLastName.containsan; }; Java 7/6 FluentIterable.fromtable.rowMap.values.filternew谓词{ @凌驾 公共布尔applyMap行{ 返回row.getFirstName.containsab | | row.getLastName.containsan; } };
还要注意的是,Guava的表与数据库表不同,您可以在其中添加额外的索引等。您只得到两个索引:行和列。我最终根据Evgeny的答案实现了基本表结构,并根据mfulton26的答案实现了Fluentiable
SELECT FirstName, LastName, City
FROM Customers
WHERE FirstName LIKE '%ab%'
OR LastName LIKE '%an%'
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;
}
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);
}
}
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);
}