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

Forgot to add helpingFunctions class to commit

parent 0e4e5077
No related branches found
No related tags found
No related merge requests found
package com.application.DB;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.bigquery.*;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import static com.application.DB.Constants.PROJECT_ID;
public class HelpingFunctions {
static Map<String,String> manMoist = new HashMap<>();
/**
* Creates a simple date format to use for converting millis in numbers to a usefull date format
*
* @return returns the date format
*/
static SimpleDateFormat getDateFormat() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat;
}
/**
* Retrieves the credentials file
*
* @return the credentials
* @throws Exception for potential errors
*/
private static GoogleCredentials getCredentials() throws Exception {
File credentialsPath = new File("./src/main/resources/com.application/sf-drying-optimization-1e234ad2b0f4.json");
// Load credentials from JSON key file. If you can't set the GOOGLE_APPLICATION_CREDENTIALS
// environment variable, you can explicitly load the credentials file to construct the
// credentials.
GoogleCredentials credentials;
try (FileInputStream serviceAccountStream = new FileInputStream(credentialsPath)) {
credentials = ServiceAccountCredentials.fromStream(serviceAccountStream);
}
return credentials;
}
/**
* Creates a bigquery builder
*
* @return a builder
* @throws Exception returns potential error
*/
private static BigQuery getBuilder() throws Exception {
// Step 1: Initialize BigQuery service
// Here we set our project ID and get the `BigQuery` service object
// this is the interface to our BigQuery instance that
// we use to execute jobs on
return BigQueryOptions.newBuilder().
setCredentials(getCredentials()).
setProjectId(PROJECT_ID)
.build().getService();
}
/**
* Creates a job
*
* @param queryConfig query configuration information
* @return a job
* @throws Exception returns potential error
*/
private static Job getJob(JobConfiguration queryConfig) throws Exception {
// Step 3: Run the job on BigQuery
// create a `Job` instance from the job configuration using the BigQuery service
// the job starts executing once the `create` method executes
Job queryJob = getBuilder().create(JobInfo.newBuilder(queryConfig).build());
queryJob = queryJob.waitFor();
// the waitFor method blocks until the job completes
// and returns `null` if the job doesn't exist anymore
if (queryJob == null) {
throw new Exception("job no longer exists");
}
// once the job is done, check if any error occured
if (queryJob.getStatus().getError() != null) {
throw new Exception(queryJob.getStatus().getError().toString());
}
return queryJob;
}
/**
* This function creates a query job that uses the query statement
* in order to retrieve information from the database
*
* @param sqlStatement input for the query statement
* @return returns the queryjob with the results
* @throws Exception Throws exception in case of error
*/
static TableResult createQueryJob(String sqlStatement) throws Exception {
// Creates a job configuration
Job queryJob = getJob(QueryJobConfiguration.newBuilder(sqlStatement).build());
// Retrieves the results from the queryjob
return queryJob.getQueryResults();
}
public static Map<String, String> getManMoist() {
return manMoist;
}
public static void setManMoist(Map<String, String> manMoist) {
HelpingFunctions.manMoist = manMoist;
}
}
No preview for this file type
No preview for this file type
File added
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