List of usage examples for com.google.common.collect Table columnKeySet
Set<C> columnKeySet();
From source file:de.tudarmstadt.ukp.dkpro.c4corpus.hadoop.statistics.StatisticsTableCreator.java
public static void saveTableToCsv(Table<String, String, Long> table, OutputStream outputStream) { PrintWriter pw = new PrintWriter(outputStream); pw.write(";"); for (String columnKey : table.columnKeySet()) { pw.printf("%s;", columnKey); }//from www. ja va 2 s . c om pw.println(); for (String rowKey : table.rowKeySet()) { pw.printf("%s;", rowKey); for (String columnKey : table.columnKeySet()) { Long value = table.get(rowKey, columnKey); pw.printf("%d;", value != null ? value : 0); } pw.println(); } IOUtils.closeQuietly(pw); }
From source file:org.opennms.netmgt.jasper.analytics.DataSourceUtils.java
public static ColumnValuesDataSource toDs(Table<Integer, String, Double> table) { String columnNames[] = table.columnKeySet().toArray(new String[0]); List<ColumnValues> columnValues = Lists.newLinkedList(); int numRows = table.rowKeySet().size(); // Rebuild all of the columns for (String columnName : columnNames) { if ("Timestamp".equalsIgnoreCase(columnName)) { // Always convert the Timestamp column to Date objects Object[] values = new Date[numRows]; for (int i = 0; i < numRows; i++) { values[i] = new Date(table.get(i, columnName).longValue()); }//from w w w . j a v a 2s . co m columnValues.add(new ObjectArrayValues(values)); } else { // Leave every other column as doubles double[] values = new double[numRows]; for (int i = 0; i < numRows; i++) { Double value = table.get(i, columnName); // Convert any nulls to NaNs, avoids NPEs if (value != null) { values[i] = value; } else { values[i] = Double.NaN; } } columnValues.add(new DoubleArrayValues(values)); } } // Join the columns into a data source return new ColumnValuesDataSource(columnNames, numRows, columnValues.toArray(new ColumnValues[0])); }
From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.significance.SignificanceMain.java
/** * Prints table to output string as CSV//from w ww .ja va 2 s . co m * * @param out output * @param <T> value type * @param table table * @throws IOException */ public static <T> String tableToCsv(Table<String, String, Boolean> table) throws IOException { StringWriter sw = new StringWriter(); CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); 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()) { printer.print(table.get(rowKey, columnKey)); } printer.println(); } printer.close(); return sw.toString(); }
From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.evaluation.helpers.FinalTableExtractor.java
public static <T> String tableToCsv(Table<String, String, T> table) throws IOException { StringWriter sw = new StringWriter(); CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); List<String> firstRow = new ArrayList<>(); firstRow.add(" "); firstRow.addAll(table.columnKeySet()); printer.printRecord(firstRow);//from w ww .j a va 2 s . co m for (String rowKey : table.rowKeySet()) { printer.print(rowKey); for (String columnKey : table.columnKeySet()) { printer.print(table.get(rowKey, columnKey)); } printer.println(); } printer.close(); return sw.toString(); }
From source file:com.zxy.commons.poi.excel.ExcelUtils.java
/** * export excel// w w w . j a v a2 s.c o m * * @param sheetName sheet name * @param table table * @return Workbook */ @SuppressWarnings("PMD.ShortVariable") private static Workbook exportExcel(String sheetName, Table<Integer, String, String> table) { Set<Integer> tableRows = table.rowKeySet(); Set<String> tableColumns = table.columnKeySet(); // excel Workbook wb = new HSSFWorkbook(); // sheet?? Sheet sheet = wb.createSheet(sheetName); // ???n? /*for (int i = 0; i < keys.length; i++) { sheet.setColumnWidth((short) i, (short) (35.7 * 150)); }*/ // Row row = sheet.createRow((short) 0); // ??? CellStyle cs = wb.createCellStyle(); CellStyle cs2 = wb.createCellStyle(); // ? Font f1 = wb.createFont(); Font f2 = wb.createFont(); // ???? f1.setFontHeightInPoints((short) 10); f1.setColor(IndexedColors.BLACK.getIndex()); f1.setBold(true); // ?? f2.setFontHeightInPoints((short) 10); f2.setColor(IndexedColors.BLACK.getIndex()); // Font f3=wb.createFont(); // f3.setFontHeightInPoints((short) 10); // f3.setColor(IndexedColors.RED.getIndex()); // ????? cs.setFont(f1); cs.setBorderLeft(BorderStyle.THIN); cs.setBorderRight(BorderStyle.THIN); cs.setBorderTop(BorderStyle.THIN); cs.setBorderBottom(BorderStyle.THIN); cs.setAlignment(HorizontalAlignment.CENTER); // ??? cs2.setFont(f2); cs2.setBorderLeft(BorderStyle.THIN); cs2.setBorderRight(BorderStyle.THIN); cs2.setBorderTop(BorderStyle.THIN); cs2.setBorderBottom(BorderStyle.THIN); cs2.setAlignment(HorizontalAlignment.CENTER); // ?? int i = 0; for (String tableColumn : tableColumns) { Cell cell = row.createCell(i); cell.setCellValue(tableColumn); cell.setCellStyle(cs); i++; } // ?? for (Integer tableRow : tableRows) { // Row ,Cell , Row Cell 0 // sheet checkArgument(tableRow > 0, "Row index must be greater than zero!"); Row row1 = sheet.createRow(tableRow); // row Map<String, String> item = table.row(tableRow); int j = 0; for (Map.Entry<String, String> entry : item.entrySet()) { // for(String v:item.keySet()){ // System.out.println(tableRow + "-" + v + "-" + item.get(v)); Cell cell = row1.createCell(j); cell.setCellValue(entry.getValue()); cell.setCellStyle(cs2); j++; } } return wb; }
From source file:eu.itesla_project.sampling.util.Utils.java
public static void dumpWp41HistoData(Wp41HistoData hdata, Logger log) { log.info("Matrix size=" + hdata.getHdTable().rowKeySet().size() + " x " + hdata.getHdTable().columnKeySet().size()); Table<Integer, String, Float> table = hdata.getHdTable(); StringBuffer sb = new StringBuffer(); for (String colName : table.columnKeySet()) { sb.append(colName + ", "); }//from w w w . jav a 2 s . co m log.info(sb.toString()); for (Integer rowName : table.rowKeySet()) { sb = new StringBuffer(); for (String colName : table.columnKeySet()) { sb.append(table.get(rowName, colName) + ", "); } log.info(sb.toString()); } }
From source file:org.caleydo.data.importer.tcga.FirehoseProvider.java
private static File parseMAF(File maf) { File out = new File(maf.getParentFile(), "P" + maf.getName()); if (out.exists()) return out; log.fine(maf.getAbsolutePath() + " parsing maf file"); final String TAB = "\t"; try (BufferedReader reader = Files.newBufferedReader(maf.toPath(), Charset.forName("UTF-8"))) { List<String> header = Arrays.asList(reader.readLine().split(TAB)); int geneIndex = header.indexOf("Hugo_Symbol"); int sampleIndex = header.indexOf("Tumor_Sample_Barcode"); // gene x sample x mutated Table<String, String, Boolean> mutated = TreeBasedTable.create(); String line = null;/*from w ww. j a v a 2s . c om*/ while ((line = reader.readLine()) != null) { String[] columns = line.split(TAB); mutated.put(columns[geneIndex], columns[sampleIndex], Boolean.TRUE); } File tmp = new File(out.getParentFile(), out.getName() + ".tmp"); PrintWriter w = new PrintWriter(tmp); w.append("Hugo_Symbol"); List<String> cols = new ArrayList<>(mutated.columnKeySet()); for (String sample : cols) { w.append(TAB).append(sample); } w.println(); Set<String> rows = mutated.rowKeySet(); for (String gene : rows) { w.append(gene); for (String sample : cols) { w.append(TAB).append(mutated.contains(gene, sample) ? '1' : '0'); } w.println(); } w.close(); Files.move(tmp.toPath(), out.toPath(), StandardCopyOption.REPLACE_EXISTING); log.fine(maf.getAbsolutePath() + " parsed maf file stats: " + mutated.size() + " " + rows.size() + " " + cols.size()); return out; } catch (IOException e) { log.log(Level.SEVERE, maf.getAbsolutePath() + " maf parsing error: " + e.getMessage(), e); } return null; }
From source file:com.przemo.conjunctions.Joiner.java
protected String[] concatAllColumns(Table[] tables) { Set<String> ret = new HashSet<>(); for (Table t : tables) { for (Object c : t.columnKeySet()) { if (c instanceof String) { ret.add((String) c); }//from ww w .j a v a 2 s.c om } } return ret.toArray(new String[] {}); }
From source file:com.przemo.etl.dataproviders.CSVDataProvider.java
protected String[] defaultHeaders(Table data) { String[] r = new String[data.columnKeySet().size()]; int i = 0;// ww w .j a v a2s .co m for (Object c : data.columnKeySet()) { if (c instanceof String) { r[i] = (String) c; } else { r[i] = "Column " + i; } i++; } return r; }
From source file:org.apache.sentry.provider.db.generic.tools.SentryConfigToolSolr.java
private void checkCompat(SimpleFileProviderBackend backend) throws Exception { Map<String, Set<String>> rolesCaseMapping = new HashMap<String, Set<String>>(); Table<String, String, Set<String>> groupRolePrivilegeTable = backend.getGroupRolePrivilegeTable(); for (String roleName : groupRolePrivilegeTable.columnKeySet()) { String roleNameLower = roleName.toLowerCase(Locale.US); if (!roleName.equals(roleNameLower)) { if (!rolesCaseMapping.containsKey(roleNameLower)) { rolesCaseMapping.put(roleNameLower, Sets.newHashSet(roleName)); } else { rolesCaseMapping.get(roleNameLower).add(roleName); }//ww w . ja v a 2s . c om } } List<String> errors = new LinkedList<String>(); StringBuilder warningString = new StringBuilder(); if (!rolesCaseMapping.isEmpty()) { warningString .append("The following roles names will be lower cased when added to the Sentry Service.\n"); warningString.append("This will cause document-level security to fail to match the role tokens.\n"); warningString.append("Role names: "); } boolean firstWarning = true; for (Map.Entry<String, Set<String>> entry : rolesCaseMapping.entrySet()) { Set<String> caseMapping = entry.getValue(); if (caseMapping.size() > 1) { StringBuilder errorString = new StringBuilder(); errorString.append("The following (cased) roles map to the same role in the sentry service: "); boolean first = true; for (String casedRole : caseMapping) { errorString.append(first ? "" : ", "); errorString.append(casedRole); first = false; } errorString.append(". Role in service: ").append(entry.getKey()); errors.add(errorString.toString()); } for (String casedRole : caseMapping) { warningString.append(firstWarning ? "" : ", "); warningString.append(casedRole); firstWarning = false; } } for (String error : errors) { System.out.println("ERROR: " + error); } System.out.println("\n"); System.out.println("Warning: " + warningString.toString()); if (errors.size() > 0) { SentryConfigurationException ex = new SentryConfigurationException("Compatibility check failure"); ex.setConfigErrors(errors); ex.setConfigWarnings(Lists.<String>asList(warningString.toString(), new String[0])); throw ex; } }