下面列出了com.google.common.collect.Table#columnKeySet ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void cleanPullLogAndCheckpoint() {
final Table<String, String, PullLog> pullLogs = storage.allPullLogs();
if (pullLogs == null || pullLogs.size() == 0) return;
// delete all pull log without max pulled message sequence
for (final String groupAndSubject : pullLogs.columnKeySet()) {
final GroupAndSubject gs = GroupAndSubject.parse(groupAndSubject);
final long maxPulledMessageSequence = storage.getMaxPulledMessageSequence(gs.getSubject(), gs.getGroup());
if (maxPulledMessageSequence == -1) {
for (final Map.Entry<String, PullLog> entry : pullLogs.column(groupAndSubject).entrySet()) {
final String consumerId = entry.getKey();
LOG.info("remove pull log. subject: {}, group: {}, consumerId: {}", gs.getSubject(), gs.getGroup(), consumerId);
storage.destroyPullLog(gs.getSubject(), gs.getGroup(), consumerId);
}
}
}
}
public QuickStringIntTable(Table<String, String, Integer> table) {
ArrayList<String> labelList = Lists.newArrayList(table.rowKeySet());
labelBase = findABase(labelList);
labelSize = labelBase.length;
data = new int[labelSize * labelSize];
for (String rowKey : table.rowKeySet()) {
for (String colKey : table.columnKeySet()) {
int rowid = labelBase[rowKey.hashCode() % labelSize];
int colid = labelBase[colKey.hashCode() % labelSize];
data[rowid * labelSize + colid] = table.get(rowKey, colKey);
}
}
}
public QuickStringDoubleTable(Table<String, String, Double> table) {
ArrayList<String> labelList = Lists.newArrayList(table.rowKeySet());
labelBase = findABase(labelList);
labelSize = labelBase.length;
data = new double[labelSize * labelSize];
for (String rowKey : table.rowKeySet()) {
for (String colKey : table.columnKeySet()) {
int rowid = labelBase[rowKey.hashCode() % labelSize];
int colid = labelBase[colKey.hashCode() % labelSize];
data[rowid * labelSize + colid] = table.get(rowKey, colKey);
}
}
}
public void writeCSV(String outputFile, Table<Integer, Integer, String> resultTable)
throws IOException {
FileWriter writer = new FileWriter(outputFile);
for (Integer row : resultTable.rowKeySet()) {
boolean first = true;
for (Integer column : resultTable.columnKeySet()) {
if (!first) {
writer.append(',');
}
String value = resultTable.get(row, column);
if (value != null) {
writer.append(value);
}
first = false;
}
writer.append('\n');
}
writer.flush();
writer.close();
}
static
private Map<String, RowFilter> parseInlineTable(InlineTable inlineTable){
Map<String, RowFilter> result = new LinkedHashMap<>();
Table<Integer, String, Object> table = InlineTableUtil.getContent(inlineTable);
Set<String> columns = table.columnKeySet();
for(String column : columns){
Map<Integer, Object> columnValues = table.column(column);
RowFilter rowFilter = new RowFilter(columnValues);
result.put(column, rowFilter);
}
return result;
}
/**
* Converts Guava table to a CSV table
*
* @param table table
* @param csvFormat CSV format
* @param missingValuePlaceholder print if a value is missing (empty string by default)
* @param <T> object type (string)
* @return table
* @throws IOException exception
*/
public static <T> String tableToCsv(Table<String, String, T> table, CSVFormat csvFormat,
String missingValuePlaceholder)
throws IOException
{
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, csvFormat);
List<String> firstRow = new ArrayList<>();
firstRow.add(" ");
firstRow.addAll(table.columnKeySet());
printer.printRecord(firstRow);
for (String rowKey : table.rowKeySet()) {
printer.print(rowKey);
for (String columnKey : table.columnKeySet()) {
T value = table.get(rowKey, columnKey);
if (value == null) {
printer.print(missingValuePlaceholder);
}
else {
printer.print(value);
}
}
printer.println();
}
printer.close();
return sw.toString();
}
/**
* parse the ini file and return a map with all data
*
* @param resourcePath
* The path of the input file
* @param conf
* The configuration info
* @return the result of sentry mapping data in map structure.
*/
@Override
public Map<String, Map<String, Set<String>>> parse(String resourcePath, Configuration conf)
throws Exception {
Map<String, Map<String, Set<String>>> resultMap = Maps.newHashMap();
// SimpleFileProviderBackend is used for parse the ini file
SimpleFileProviderBackend policyFileBackend = new SimpleFileProviderBackend(conf, resourcePath);
ProviderBackendContext context = new ProviderBackendContext();
context.setAllowPerDatabase(true);
// parse the ini file
policyFileBackend.initialize(context);
// SimpleFileProviderBackend parsed the input file and output the data in Table format.
Table<String, String, Set<String>> groupRolePrivilegeTable = policyFileBackend
.getGroupRolePrivilegeTable();
Map<String, Set<String>> groupRolesMap = Maps.newHashMap();
Map<String, Set<String>> rolePrivilegesMap = Maps.newHashMap();
for (String groupName : groupRolePrivilegeTable.rowKeySet()) {
for (String roleName : groupRolePrivilegeTable.columnKeySet()) {
// get the roles set for the current groupName
Set<String> tempRoles = groupRolesMap.get(groupName);
if (tempRoles == null) {
tempRoles = Sets.newHashSet();
}
Set<String> privileges = groupRolePrivilegeTable.get(groupName, roleName);
// if there has privilege for [group,role], if no privilege exist, the [group, role] info
// will be discard.
if (privileges != null) {
// update [group, role] mapping data
tempRoles.add(roleName);
groupRolesMap.put(groupName, tempRoles);
// update [role, privilege] mapping data
rolePrivilegesMap.put(roleName, privileges);
}
}
}
resultMap.put(PolicyFileConstants.GROUPS, groupRolesMap);
resultMap.put(PolicyFileConstants.ROLES, rolePrivilegesMap);
return resultMap;
}
@Test
public void givenTable_whenColKeySet_returnsSuccessfully() {
final Table<String, String, Integer> universityCourseSeatTable = HashBasedTable.create();
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
final Set<String> courseSet = universityCourseSeatTable.columnKeySet();
assertThat(courseSet).hasSize(3);
}
/**
* Store a collection of Providers and their related information into this data store.
* @param providers - collection of Providers to store.
* @return Whether or not the entire collection of Providers was
* stored successfully (true) or not (false).
*/
public boolean store(Collection<? extends Provider> providers) {
try (Connection connection = getConnection()) {
// CREATE TABLE IF NOT EXISTS PROVIDER (id varchar, name varchar)
PreparedStatement providerTable = connection
.prepareStatement("INSERT INTO PROVIDER (id, name) VALUES (?,?);");
// create table provider_attribute (provider_id varchar, name varchar, value varchar)
PreparedStatement attributeTable = connection.prepareStatement(
"INSERT INTO PROVIDER_ATTRIBUTE (provider_id, name, value) VALUES (?,?,?);");
// CREATE TABLE IF NOT EXISTS UTILIZATION (provider_id varchar, encounters int, procedures
// int, labs int, prescriptions int)
PreparedStatement utilizationTable = connection.prepareStatement(
"INSERT INTO UTILIZATION "
+ "(provider_id, year, encounters, procedures, labs, prescriptions) "
+ "VALUES (?,?,?,?,?,?)");
// CREATE TABLE IF NOT EXISTS UTILIZATION_DETAIL (provider_id varchar, year int, category
// string, value int)
PreparedStatement utilizationDetailTable = connection.prepareStatement(
"INSERT INTO UTILIZATION_DETAIL (provider_id, year, category, value) VALUES (?,?,?,?)");
for (Provider p : providers) {
String providerID = p.getResourceID();
Map<String, Object> attributes = p.getAttributes();
providerTable.setString(1, providerID);
providerTable.setString(2, (String) attributes.get("name"));
providerTable.addBatch();
for (Object key : attributes.keySet()) {
attributeTable.setString(1, providerID);
attributeTable.setString(2, (String) key);
attributeTable.setString(3, String.valueOf(attributes.get(key)));
attributeTable.addBatch();
}
Table<Integer, String, AtomicInteger> u = p.getUtilization();
for (Integer year : u.rowKeySet()) {
utilizationTable.setString(1, providerID);
utilizationTable.setInt(2, year);
utilizationTable.setInt(3, pickUtilization(u, year, Provider.ENCOUNTERS));
utilizationTable.setInt(4, pickUtilization(u, year, Provider.PROCEDURES));
utilizationTable.setInt(5, pickUtilization(u, year, Provider.LABS));
utilizationTable.setInt(6, pickUtilization(u, year, Provider.PRESCRIPTIONS));
utilizationTable.addBatch();
for (String category : u.columnKeySet()) {
if (!category.startsWith(Provider.ENCOUNTERS)) {
continue;
}
int count = pickUtilization(u, year, category);
if (count == 0) {
// don't bother storing 0 in the database
continue;
}
utilizationDetailTable.setString(1, providerID);
utilizationDetailTable.setInt(2, year);
utilizationDetailTable.setString(3, category);
utilizationDetailTable.setInt(4, count);
utilizationDetailTable.addBatch();
}
}
}
for (int year = 1900; year <= Utilities.getYear(System.currentTimeMillis()); year++) {
for (int t = 0; t < EncounterType.values().length; t++) {
utilizationDetailTable.setString(1, "None");
utilizationDetailTable.setInt(2, year);
utilizationDetailTable.setString(3, EncounterType.values()[t].toString());
utilizationDetailTable.setInt(4, 0);
utilizationDetailTable.addBatch();
}
}
providerTable.executeBatch();
attributeTable.executeBatch();
utilizationTable.executeBatch();
utilizationDetailTable.executeBatch();
connection.commit();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
private static void processCosts(Connection connection, JsonWriter writer)
throws IOException, SQLException {
writer.name("costs").beginObject();
PreparedStatement stmt = connection.prepareStatement(
"select year, type, sum(cost) from "
+ "(SELECT c.cost, YEAR(DATEADD('SECOND', e.start/ 1000 , DATE '1970-01-01')) as year, "
+ "e.type FROM ENCOUNTER e, CLAIM c where e.id = c.encounter_id) group by year, type "
+ "order by year asc");
ResultSet rs = stmt.executeQuery();
Table<Integer, String, BigDecimal> table = HashBasedTable.create();
int firstYear = 0;
int lastYear = 0;
while (rs.next()) {
int year = rs.getInt(1);
String type = rs.getString(2);
BigDecimal total = rs.getBigDecimal(3);
if (firstYear == 0) {
firstYear = year;
}
lastYear = year;
table.put(year, type, total);
}
writer.name("first_year").value(firstYear);
for (String encType : table.columnKeySet()) {
writer.name(encType).beginArray();
for (int y = firstYear; y <= lastYear; y++) {
BigDecimal count = table.get(y, encType);
if (count == null) {
count = BigDecimal.ZERO;
}
writer.value(count);
}
writer.endArray(); // encType
}
writer.endObject(); // costs
}