-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add shakespeare sample for BigQuery. #390
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Copyright 2016, Google, Inc. | ||
|
||
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 all] | ||
// [START create_client] | ||
import com.google.cloud.bigquery.BigQuery; | ||
import com.google.cloud.bigquery.BigQueryOptions; | ||
import com.google.cloud.bigquery.FieldValue; | ||
import com.google.cloud.bigquery.QueryRequest; | ||
import com.google.cloud.bigquery.QueryResponse; | ||
import com.google.cloud.bigquery.QueryResult; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
// [END create_client] | ||
|
||
public class SimpleApp { | ||
public static void main(String... args) throws Exception { | ||
// [START create_client] | ||
BigQuery bigquery = BigQueryOptions.defaultInstance().service(); | ||
// [END create_client] | ||
// [START run_query] | ||
QueryRequest queryRequest = | ||
QueryRequest | ||
.builder( | ||
"SELECT " | ||
+ "APPROX_TOP_COUNT(corpus, 10) as title, " | ||
+ "COUNT(*) as unique_words " | ||
+ "FROM `publicdata.samples.shakespeare`;") | ||
// Use standard SQL syntax for queries. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SQL or GQL? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's called standard SQL publicly. https://cloud.google.com/bigquery/sql-reference/enabling-standard-sql |
||
// See: https://cloud.google.com/bigquery/sql-reference/ | ||
.useLegacySql(false) | ||
.build(); | ||
QueryResponse response = bigquery.query(queryRequest); | ||
// [END run_query] | ||
|
||
// [START print_results] | ||
QueryResult result = response.result(); | ||
|
||
while (result != null) { | ||
Iterator<List<FieldValue>> iter = result.iterateAll(); | ||
|
||
while (iter.hasNext()) { | ||
List<FieldValue> row = iter.next(); | ||
List<FieldValue> titles = row.get(0).repeatedValue(); | ||
System.out.println("titles:"); | ||
|
||
for (FieldValue titleValue : titles) { | ||
List<FieldValue> titleRecord = titleValue.recordValue(); | ||
String title = titleRecord.get(0).stringValue(); | ||
long uniqueWords = titleRecord.get(1).longValue(); | ||
System.out.printf("\t%s: %d\n", title, uniqueWords); | ||
} | ||
|
||
long uniqueWords = row.get(1).longValue(); | ||
System.out.printf("total unique words: %d\n", uniqueWords); | ||
} | ||
|
||
result = result.nextPage(); | ||
} | ||
// [END print_results] | ||
} | ||
} | ||
// [END all] | ||
|
57 changes: 57 additions & 0 deletions
57
bigquery/cloud-client/src/test/java/com/example/bigquery/SimpleAppIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
Copyright 2016, Google, Inc. | ||
|
||
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 org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
|
||
/** | ||
* Tests for simple app sample. | ||
*/ | ||
@RunWith(JUnit4.class) | ||
@SuppressWarnings("checkstyle:abbreviationaswordinname") | ||
public class SimpleAppIT { | ||
private ByteArrayOutputStream bout; | ||
private PrintStream out; | ||
|
||
@Before | ||
public void setUp() { | ||
bout = new ByteArrayOutputStream(); | ||
out = new PrintStream(bout); | ||
System.setOut(out); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
System.setOut(null); | ||
} | ||
|
||
@Test | ||
public void testQuickstart() throws Exception { | ||
SimpleApp.main(); | ||
String got = bout.toString(); | ||
assertThat(got).contains("hamlet"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't you add a manifest line in your pom.xml so you don't need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have an example of that?