Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Bacheloroppgave_2022
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Eilert Tunheim
Bacheloroppgave_2022
Commits
e6428c76
Commit
e6428c76
authored
3 years ago
by
Karin Pettersen
Browse files
Options
Downloads
Patches
Plain Diff
Created sql statement in sort
parent
e26419c0
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/com/application/DB/Sort.java
+90
-0
90 additions, 0 deletions
src/main/java/com/application/DB/Sort.java
with
90 additions
and
0 deletions
src/main/java/com/application/DB/Sort.java
0 → 100644
+
90
−
0
View file @
e6428c76
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.io.IOException
;
public
class
Sort
{
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
;
}
// 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
private
static
BigQuery
getBuilder
()
throws
Exception
{
BigQuery
bigquery
=
BigQueryOptions
.
newBuilder
().
setCredentials
(
getCredentials
()).
setProjectId
(
"sf-drying-optimization"
)
.
build
().
getService
();
return
bigquery
;
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
Sort
.
getFromExistingTable
();
}
public
static
void
getFromExistingTable
()
throws
Exception
{
// Step 2: Prepare query job
// A "QueryJob" is a type of job that executes SQL queries
// we create a new job configuration from our SQL query and
final
String
GET_WORD_COUNT
=
"SELECT CalculatedStart, CalculatedStop FROM sf-drying-optimization.124.int_dk_valmaticsdryingbatches W"
+
"HERE Name Like '%Gran%' OR Name LIKE '%2ex%' OR Name LIKE '%47x175%"
+
"AND BETWEEN \"2020-06-09\" AND \"2020-06-29\" ORDER BY TimeStamp"
;
QueryJobConfiguration
queryConfig
=
QueryJobConfiguration
.
newBuilder
(
GET_WORD_COUNT
).
build
();
// 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
());
}
// Step 4: Display results
// Print out a header line, and iterate through the
// query results to print each result in a new line
System
.
out
.
println
(
"Timestamp\tVarient value"
);
TableResult
result
=
queryJob
.
getQueryResults
();
for
(
FieldValueList
row
:
result
.
iterateAll
())
{
// We can use the `get` method along with the column
// name to get the corresponding row entry
int
variantValue
=
row
.
get
(
"VariantValue"
).
getNumericValue
().
intValue
();
String
timeStamp
=
row
.
get
(
"TimeStamp"
).
getStringValue
();
System
.
out
.
printf
(
"%s\t%d\n"
,
timeStamp
,
variantValue
);
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment