[GO] Check for the existence of BigQuery tables in Java

Introduction

How to check if there is a BigQuery table in Java.

Around GCP, it's much easier to use the SDK (com.google.cloud. *) Wrapping it than the API SDK (com.google.api.services. *). If you go around, com.google.api.services will appear more often, so if you copy it, it will be a very complicated code: sweat_smile:

Dependence

build.gradle looks like this.

apply plugin: 'java'
apply plugin: 'application'

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.google.cloud:google-cloud-bigquery:+'
}

mainClassName = "Main"

run {
    if (project.hasProperty('args')) {
        args project.args.split('\\s+')
    }
}

code

If found, it outputs Found, and if not found, it outputs Not found. If you specify the JSON Key of the service account in the argument, it will authenticate with it, otherwise the default account will be used.

import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.bigquery.*;

import java.io.FileInputStream;
import java.io.IOException;

public class Main {
    private static final String DATASET = "yourdataset";
    private static final String TABLE = "yourtable";

    public static void main(String args[]) throws IOException {
        BigQuery bigQuery;
        if (args.length > 0) {
            bigQuery = getClientWithJsonKey(args[0]);
        } else {
            bigQuery = BigQueryOptions.getDefaultInstance().getService();
        }

        Table table = bigQuery.getTable(DATASET, TABLE);
        if (table != null) {
            System.out.println("Found!");
        } else {
            System.err.println("Not found");
        }
    }

    private static BigQuery getClientWithJsonKey(String key) throws IOException {
        return BigQueryOptions.newBuilder()
                .setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream(key)))
                .build()
                .getService();
    }
}

Run

$ ./gradlew run -Pargs="/path/to/key.json"

#Or
$ ./gradlew run

in conclusion

If you do your best only with com.google.api.services, it will look like this, and even just around authentication is insanely complicated: tired_face: https://github.com/googlearchive/bigquery-samples-java/blob/master/src/main/java/com/google/cloud/bigquery/samples/BigQueryJavaGettingStarted.java

The code for this time is here: pencil: https://github.com/nownabe/examples/tree/master/bigquery-table-existence

Recommended Posts

Check for the existence of BigQuery tables in Java
Check the operation of Python for .NET in each environment
Check the existence of the file with python
Existence check of external command in Python (like `which`)
Check the asymptotic nature of the probability distribution in Python
Check the processing time and the number of calls for each process in python (cProfile)
Test code to check for broken links in the page
The result of Java engineers learning machine learning in Python www
Summary of stumbling blocks in Django for the first time
Get the number of occurrences for each element in the list
Google search for the last line of the file in Python
Check for memory leaks in Python
How to determine the existence of a selenium element in Python
Check for external commands in python
Wrap (part of) the AtCoder Library in Cython for use in Python
How to implement Java code in the background of RedHat (LinuxONE)
The story of participating in AtCoder
Check the increase / decrease of Bitcoin for each address from the blockchain
Existence from the viewpoint of Python
How to check the memory size of a dictionary in Python
Chain of Responsibility pattern in Java
[TensorFlow 2] How to check the contents of Tensor in graph mode
The story of the "hole" in the file
Check the memory protection of linux kerne with the code for ARM
Check the data summary in CASTable
The meaning of ".object" in Django
for, continue, break Explain the flow of iterative processing in Python3-Part 1
Approach commentary for beginners to be in the top 1.5% (0.83732) of Kaggle Titanic_3
[Ota Ward] Check the status of application processing for special flat-rate benefits
Check the in-memory bytes of a floating point number float in Python
Approach commentary for beginners to be in the top 1.5% (0.83732) of Kaggle Titanic_1
I made a program to check the size of a file in Python
What Java users thought of using the Go language for a day
Get the key for the second layer migration of JSON data in python
Approach commentary for beginners to be in the top 1.5% (0.83732) of Kaggle Titanic_2
[Understanding in 3 minutes] The beginning of Linux
The story of an error in PyOCR
Beginning of Phtyon programming for Java acquirers ①
How to check the version of Django
Implement part of the process in C ++
Summary of various for statements in Python
Change the list in a for statement
Check if the URL exists in Python
The result of installing python in Anaconda
Let's claim the possibility of pyenv-virtualenv in 2021
Check the path of the Python imported module
MongoDB for the first time in Python
The basics of running NoxPlayer in Python
The third night of the loop with for
Pandas of the beginner, by the beginner, for the beginner [Python]
The second night of the loop with for
In search of the fastest FizzBuzz in Python
[IOS] Change the display time for each frame of GIF animation in Pythonista3.
How to mention a user group in slack notification, how to check the id of the user group
Use the Java SDK of GoogleMapsAPI to get the result of reverse GeoCoding in Japanese.
CERTIFICATE_VERIFY_FAILED in Python 3.6, the official installer for macOS
Output the number of CPU cores in Python
Check the operation of OpenCV3 installed by Anaconda
[python] Check the elements of the list all, any
The meaning of {version-number} in the mysql rpm package
The story of low learning costs for Python