Skip to content
Snippets Groups Projects
Commit fdd73087 authored by Eilert Tunheim's avatar Eilert Tunheim
Browse files

Connection established!

parent bde961ad
No related branches found
No related tags found
No related merge requests found
package com.application; package com.application;
public class Credentials { public class Credentials {
String projectId = "MY_PROJECT_ID"; String projectId = "sf-drying-optimization";
} }
package com.application.DataBase; package com.application.DataBase;
import com.application.Credentials;
import com.google.auth.oauth2.GoogleCredentials; import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials; import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.bigquery.BigQuery; import com.google.cloud.bigquery.BigQuery;
...@@ -23,9 +25,11 @@ import java.util.List; ...@@ -23,9 +25,11 @@ import java.util.List;
public class DB { public class DB {
Credentials credentialsObject;
private static void insertSampleData() { private static void insertSampleData() {
// Step 1: Initialize BigQuery service // Step 1: Initialize BigQuery service
BigQuery bigquery = BigQueryOptions.newBuilder().setProjectId("sample-project-330313") BigQuery bigquery = BigQueryOptions.newBuilder().setProjectId("sf-drying-optimization")
.build().getService(); .build().getService();
// Step 2: Create insertAll (streaming) request // Step 2: Create insertAll (streaming) request
...@@ -97,9 +101,9 @@ public class DB { ...@@ -97,9 +101,9 @@ public class DB {
// Step 2: Prepare query job // Step 2: Prepare query job
// A "QueryJob" is a type of job that executes SQL queries // A "QueryJob" is a type of job that executes SQL queries
// we create a new job configuration from our SQL query and // we create a new job configuration from our SQL query and
final String GET_WORD_COUNT = final String GET_WORD_COUNT = "SELECT VariantValue, TimeStamp FROM sf-drying-optimization.124.int_sd_winccsensordata WHERE TimeStamp BETWEEN \"2020-06-09\" AND \"2020-06-29\" ORDER BY TimeStamp";
"SELECT word, word_count FROM `bigquery-public-data.samples.shakespeare` WHERE corpus='juliuscaesar' ORDER BY word_count DESC limit 10;";
QueryJobConfiguration queryConfig = QueryJobConfiguration queryConfig =
QueryJobConfiguration.newBuilder(GET_WORD_COUNT).build(); QueryJobConfiguration.newBuilder(GET_WORD_COUNT).build();
// Step 3: Run the job on BigQuery // Step 3: Run the job on BigQuery
...@@ -120,14 +124,14 @@ public class DB { ...@@ -120,14 +124,14 @@ public class DB {
// Step 4: Display results // Step 4: Display results
// Print out a header line, and iterate through the // Print out a header line, and iterate through the
// query results to print each result in a new line // query results to print each result in a new line
System.out.println("word\tword_count"); System.out.println("Timestamp\tVarient value");
TableResult result = queryJob.getQueryResults(); TableResult result = queryJob.getQueryResults();
for (FieldValueList row : result.iterateAll()) { for (FieldValueList row : result.iterateAll()) {
// We can use the `get` method along with the column // We can use the `get` method along with the column
// name to get the corresponding row entry // name to get the corresponding row entry
String word = row.get("word").getStringValue(); int variantValue = row.get("VariantValue").getNumericValue().intValue();
int wordCount = row.get("word_count").getNumericValue().intValue(); String timeStamp = row.get("TimeStamp").getStringValue();
System.out.printf("%s\t%d\n", word, wordCount); System.out.printf("%s\t%d\n", timeStamp, variantValue);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment