for (TableRow row : rows) { for (TableCell field : row.getF()) { System.out.printf("%-50s", field.getV());
private String formatRows(int totalNumRows) { StringBuilder samples = new StringBuilder(); List<TableRow> rows = response.getRows(); for (int i = 0; i < totalNumRows && i < rows.size(); i++) { samples.append(String.format("%n\t\t")); for (TableCell field : rows.get(i).getF()) { samples.append(String.format("%-10s", field.getV())); } } if (rows.size() > totalNumRows) { samples.append(String.format("%n\t\t...")); } return samples.toString(); } }
private String generateHash(@Nonnull List<TableRow> rows) { List<HashCode> rowHashes = Lists.newArrayList(); for (TableRow row : rows) { List<String> cellsInOneRow = Lists.newArrayList(); for (TableCell cell : row.getF()) { cellsInOneRow.add(Objects.toString(cell.getV())); Collections.sort(cellsInOneRow); } rowHashes.add(Hashing.sha1().hashString(cellsInOneRow.toString(), StandardCharsets.UTF_8)); } return Hashing.combineUnordered(rowHashes).toString(); }
/** * Tries to parse the JSON {@link TableRow} from BigQuery. * * <p>Only supports basic types and arrays. Doesn't support date types. */ public static Row toBeamRow(Schema rowSchema, TableSchema bqSchema, TableRow jsonBqRow) { List<TableFieldSchema> bqFields = bqSchema.getFields(); Map<String, Integer> bqFieldIndices = IntStream.range(0, bqFields.size()) .boxed() .collect(toMap(i -> bqFields.get(i).getName(), i -> i)); List<Object> rawJsonValues = rowSchema .getFields() .stream() .map(field -> bqFieldIndices.get(field.getName())) .map(index -> jsonBqRow.getF().get(index).getV()) .collect(toList()); return IntStream.range(0, rowSchema.getFieldCount()) .boxed() .map(index -> toBeamValue(rowSchema.getField(index).getType(), rawJsonValues.get(index))) .collect(toRow(rowSchema)); }
private void verifyLegacyQueryRes() throws Exception { LOG.info("Starting verifyLegacyQueryRes in outputTable {}", outputTable); List<String> legacyQueryExpectedRes = ImmutableList.of("apple", "orange"); QueryResponse response = bqClient.queryWithRetries(String.format("SELECT fruit from [%s];", outputTable), project); LOG.info("Finished to query result table {}", this.outputTable); List<String> tableResult = response .getRows() .stream() .flatMap(row -> row.getF().stream().map(cell -> cell.getV().toString())) .sorted() .collect(Collectors.toList()); assertEquals(legacyQueryExpectedRes, tableResult); }
/** * Makes an API call to the BigQuery API * * @param bigquery an authorized BigQuery client * @param projectId a string containing the current project ID * @param completedJob to the completed Job * @throws IOException */ private static void displayQueryResults(Bigquery bigquery, String projectId, Job completedJob) throws IOException { GetQueryResultsResponse queryResult = bigquery.jobs() .getQueryResults( projectId, completedJob .getJobReference() .getJobId() ).execute(); List<TableRow> rows = queryResult.getRows(); System.out.print("\nQuery Results:\n------------\n"); for (TableRow row : rows) { for (TableCell field : row.getF()) { System.out.printf("%-50s", field.getV()); } System.out.println(); } } // [END display_result]
@Test public void testE2ETrafficMaxLaneFlow() throws Exception { this.options.setBigQuerySchema(FormatMaxesFn.getSchema()); this.options.setProject(this.projectId); this.options.setBigQueryDataset(this.outputDatasetId); this.options.setBigQueryTable(this.outputTable); TrafficMaxLaneFlow.runTrafficMaxLaneFlow(this.options); QueryResponse response = this.bqClient.queryWithRetries( String.format( "SELECT count(*) as total FROM [%s:%s.%s]", this.projectId, this.outputDatasetId, this.outputTable), this.projectId); String res = response.getRows().get(0).getF().get(0).getV().toString(); assertEquals("9763", res); } }
@Test public void testE2ETrafficRoutes() throws Exception { this.options.setBigQuerySchema(FormatStatsFn.getSchema()); this.options.setProject(this.projectId); this.options.setBigQueryDataset(this.outputDatasetId); this.options.setBigQueryTable(this.outputTable); TrafficRoutes.runTrafficRoutes(options); QueryResponse response = this.bqClient.queryWithRetries( String.format( "SELECT count(*) as total FROM [%s:%s.%s]", this.projectId, this.outputDatasetId, this.outputTable), this.projectId); String res = response.getRows().get(0).getF().get(0).getV().toString(); assertEquals("27", res); } }