Skip to content

How can I receive Data from Google BigQuery with Eclipse and Java? #2370

Closed

Description

@Electricks94

I’m trying to receive some Data from google BigQuery and show the result in my eclipse console to get started with BigQuery and Java. Basically, I followed the Instructions provided by google, which I found here: https://cloud.google.com/bigquery/docs/reference/libraries

So my example Java Code looks like the quick start example from google:

import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.Dataset;
import com.google.cloud.bigquery.DatasetInfo;

public class QuickstartSample {
  public static void main(String... args){
        // Instantiates a client
        BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

        // The name for the new dataset
        String datasetName = "bigquery-public-data:hacker_news.comments";

        // Prepares a new dataset
        Dataset dataset = null;
        DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build();

        // Creates the dataset
        dataset = bigquery.create(datasetInfo);

        System.out.printf("Dataset %s created.%n", dataset.getDatasetId().getDataset());
  } 
}

But when I run this Code I get the following error:

HTTP ERROR 500

Problem accessing /getbigquery. Reason: 
    Could not get the access token.


Caused by:
com.google.cloud.bigquery.BigQueryException: Could not get the access token.
    at com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc.translate(HttpBigQueryRpc.java:86)
    at com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc.create(HttpBigQueryRpc.java:141)
    at com.google.cloud.bigquery.BigQueryImpl$1.call(BigQueryImpl.java:172)
    at com.google.cloud.bigquery.BigQueryImpl$1.call(BigQueryImpl.java:169)
    at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:94)
    at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:54)
    at com.google.cloud.bigquery.BigQueryImpl.create(BigQueryImpl.java:169)
    at testpackage.dto.QuickstartSample.main(QuickstartSample.java:27)
    at testpackage.getbigquery.doPost(getbigquery.java:27)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:134)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:48)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:122)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
I tried to authenticate with Default Credentials so I ran “gcloud auth application-default login“ in my google SDK console. Although I could login in SDK, eclipse gave me the previous shown error.

Then I tried a few other things like downloading my token as Json file and revering to it with java in my application or setting my environment variable manually but it didn’t work as well.

Does anyone have an idea what I’m doing wrong? Any help is appreciated.

Activity

  1. added
    api: bigqueryIssues related to the BigQuery API.
    type: questionRequest for information or clarification. Not an issue.
    on Aug 23, 2017
  2. michaelbausor commented on Aug 23, 2017

    @michaelbausor
    Contributor

    Hi @Electricks94,

    It looks like you are trying to fetch an existing dataset, but your code is creating it. Can you try the following code to see if you get the same problem?

      public static void main(String... args){
        // Instantiates a client
        BigQuery bigquery = BigQueryOptions.getDefaultInstance()
            .toBuilder()
            .setProjectId("bigquery-public-data")
            .build()
            .getService();
    
        // The name for the new dataset
        String datasetName = "hacker_news";
    
        // Fetches the dataset
        Dataset dataset = bigquery.getDataset(datasetName);
    
        System.out.printf("Dataset %s fetched.%n", dataset.getDatasetId().getDataset());
      }
    
  3. Electricks94 commented on Aug 23, 2017

    @Electricks94
    Author

    Thank you very much! This worked well.

    Just that I understand correctly. What I did with the code I posted was creating a new table in bigquery, but what I really wanted to do was to fetch data from bigquery and that’s why the code didn’t work?

  4. michaelbausor commented on Aug 23, 2017

    @michaelbausor
    Contributor

    Yes, I think so. Your code was trying to create a dataset in the public bigquery-public-data project. The code I suggested did the following:

    • Set the project ID before creating the BigQuery object
    • Get the dataset, instead of creating it

    I am closing this issue now, but if you have any more problems, please feel free to reopen it!

  5. Electricks94 commented on Aug 23, 2017

    @Electricks94
    Author

    Thanks again for your help.

    Now I can receive some basic Information about my tables. I tried to run this code to get started with BigQuery and Java.
    What I really want to do is get complete rows as arrays and show the information in Chars.
    I did not find options to get complete rows in the methods of the BigQuery object.
    So, my Questions are:

    • Do I need to set up a Pipeline or is there an easier way?

    • Is this a good thing to do or is there an even better way to show the information stored in BigQuery as Chars?

    I’m sorry to ask this question here although it’s not really related to my original question.
    Should I open another Question in the issues or is there something else I should do to ask such Questions? (Please excuse my behavior. I’m very new to Github)

  6. michaelbausor commented on Aug 23, 2017

    @michaelbausor
    Contributor

    No worries!

    I would try working through some of the How-To guides for Bigquery, available here: https://cloud.google.com/bigquery/docs/how-to

    You should find something there that can help - if you want all of the data in a table, take a look at the Using Tables how-tos: https://cloud.google.com/bigquery/docs/tables

    If you have other problems, it is best to open another issue, rather than commenting on this one.

  7. Electricks94 commented on Aug 23, 2017

    @Electricks94
    Author

    Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: bigqueryIssues related to the BigQuery API.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions