Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions bigquery/README.md

This file was deleted.

24 changes: 24 additions & 0 deletions bigquery/cloud-client/snippets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,28 @@ limitations under the License.
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.bigquery;

// [START bigquery_query]

import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.TableResult;

public class SimpleQuery {

public static void main(String[] args) {
// TODO(developer): Replace this query before running the sample.
String query =
"SELECT name, SUM(number) as total_people "
+ "FROM `bigquery-public-data.usa_names.usa_1910_2013` "
+ "WHERE state = 'TX' "
+ "GROUP BY name, state "
+ "ORDER BY total_people DESC "
+ "LIMIT 100;";
simpleQuery(query);
}

public static void simpleQuery(String query) {
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();

// Create the query job.
QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query).build();

// Execute the query.
TableResult result = bigquery.query(queryConfig);

// Print the results.
result
.iterateAll()
.forEach(
row -> {
System.out.print("name:" + row.get("name").getStringValue());
System.out.print(", count:" + row.get("total_people").getLongValue());
System.out.println();
});

System.out.println("Query ran successfully");
} catch (BigQueryException | InterruptedException e) {
System.out.println("Query did not run \n" + e.toString());
}
}
}
// [END bigquery_query]
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.bigquery;

import static com.google.common.truth.Truth.assertThat;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class SimpleQueryIT {

private final Logger log = Logger.getLogger(this.getClass().getName());
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);
}

@After
public void tearDown() {
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
log.log(Level.INFO, "\n" + bout.toString());
}

@Test
public void testSimpleQuery() {
String query =
"SELECT name, SUM(number) as total_people "
+ "FROM `bigquery-public-data.usa_names.usa_1910_2013` "
+ "WHERE state = 'TX' "
+ "GROUP BY name, state "
+ "ORDER BY total_people DESC "
+ "LIMIT 100;";

SimpleQuery.simpleQuery(query);
assertThat(bout.toString()).contains("Query ran successfully");
Comment thread
Kef131 marked this conversation as resolved.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
import com.google.cloud.Policy;
import com.google.cloud.Role;
import com.google.cloud.bigquery.Acl;
import com.google.cloud.bigquery.Acl.Entity;
import com.google.cloud.bigquery.Acl.Group;
import com.google.cloud.bigquery.Acl.Role;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
import com.google.cloud.bigquery.BigQueryException;
Expand Down Expand Up @@ -88,8 +85,8 @@ public static Dataset setUpTest_grantAccessToDataset(
DatasetId datasetId = DatasetId.of(projectId, datasetName);
Dataset dataset = bigquery.getDataset(datasetId);

Entity entity = new Group(entityEmail);
Acl newEntry = Acl.of(entity, Role.READER);
Acl.Entity entity = new Acl.Group(entityEmail);
Acl newEntry = Acl.of(entity, Acl.Role.READER);
List<Acl> acls = new ArrayList<>(dataset.getAcl());
acls.add(newEntry);

Expand Down