Skip to content
Snippets Groups Projects
Commit 691d3159 authored by Mads Greni Arnesen's avatar Mads Greni Arnesen
Browse files

Connecting BigQuery

parent fc2bacb2
No related branches found
No related tags found
No related merge requests found
......@@ -12,7 +12,7 @@ public class DB {
Dataset dataset = null;
DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build();
/*
/*
dataset = bigquery.create(datasetInfo);
System.out.printf("Dataset %s created.%n", dataset.getDatasetId().getDataset());
......@@ -28,5 +28,7 @@ public class DB {
.setUseLegacySql(false)
.build();
System.out.println(queryConfig);
*/
*/
}
package com.application.DataBase;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.FormatOptions;
import com.google.cloud.bigquery.Job;
import com.google.cloud.bigquery.JobInfo;
import com.google.cloud.bigquery.LoadJobConfiguration;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardSQLTypeName;
import com.google.cloud.bigquery.TableId;
import java.io.File;
// Sample to load JSON data from Cloud Storage into a new BigQuery table
public class LoadJson {
public static void runLoadJsonFromGCS() {
// TODO(developer): Replace these variables before running the sample.
String datasetName = "124";
String tableName = "int_sd_winccsensordata";
String credentialsPath = (".\\src\\main\\resources\\com.application\\sf-drying-optimization-1e234ad2b0f4.json");
Schema schema =
Schema.of(
Field.of("VariantValue", StandardSQLTypeName.INT64),
Field.of("TimeStamp", StandardSQLTypeName.INT64));
loadJsonFromGCS(datasetName, tableName, credentialsPath, schema);
}
public static void loadJsonFromGCS(
String datasetName, String tableName, String sourceUri, Schema schema) {
try {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
TableId tableId = TableId.of(datasetName, tableName);
LoadJobConfiguration loadConfig =
LoadJobConfiguration.newBuilder(tableId, sourceUri)
.setFormatOptions(FormatOptions.json())
.setSchema(schema)
.build();
// Load data from a GCS JSON file into the table
Job job = bigquery.create(JobInfo.of(loadConfig));
// Blocks until this load table job completes its execution, either failing or succeeding.
job = job.waitFor();
if (job.isDone()) {
System.out.println("Json from GCS successfully loaded in a table");
} else {
System.out.println(
"BigQuery was unable to load into the table due to an error:"
+ job.getStatus().getError());
}
} catch (BigQueryException | InterruptedException e) {
System.out.println("Column not added during load append \n" + e.toString());
}
}
}
package com.application.DataBase;
import com.google.cloud.bigquery.*;
// Sample to run query total rows
public class LoadTable {
public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
String projectId = "f-drying-optimization";
String datasetName = "124";
String tableName = "int_sd_winccsensordata";
getTable(projectId, datasetName, tableName);
}
public static void getTable(String projectId, String datasetName, String tableName) {
try {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
TableId tableId = TableId.of(projectId, datasetName, tableName);
Table table = bigquery.getTable(tableId);
System.out.println("Table info: " + table.getDescription());
} catch (BigQueryException e) {
System.out.println("Table not retrieved. \n" + e.toString());
}
}
}
\ No newline at end of file
package com.application;
import com.application.DataBase.Authentication;
import com.application.DataBase.LoadJson;
import com.google.cloud.bigquery.Schema;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
......@@ -20,6 +22,8 @@ public class Main extends Application {
//System.out.println("Hello world!");
//Authentication test = explicit();
launch(args);
new LoadJson().runLoadJsonFromGCS();
}
/**
......
File added
No preview for this file type
File added
File added
No preview for this file type
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