Skip to content

Move examples from the cloud-storage-docs-xml-api-examples repo, and update them to use Application Default Credentials. #14

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

Merged
merged 27 commits into from
May 29, 2015
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bafd8c3
Move from cloud-storagedocs-xml-api-examples
May 22, 2015
fed4ae8
Update READMEs for new dir structure
May 22, 2015
9ebe7fa
Rename directories to be less redundant.
May 22, 2015
dcf91cc
Update to use App Default Creds.
May 22, 2015
ccf5fc8
Remove package, so eclipse isn't confused.
May 22, 2015
dd10b2c
Add Application Default Credentials quirks.
May 22, 2015
309d399
Update: no longer using service accounts.
May 26, 2015
f606873
Merge remote-tracking branch 'origin/master' into jerjou/xml-api
May 28, 2015
014079d
Add checkstyle plugin
May 28, 2015
c2b6935
More check style configuration
May 28, 2015
1b9f309
Fix style on DeferSampleServlet
May 28, 2015
45920e1
More check style changes
May 28, 2015
0e4514f
Finish fixing Async Query and Defer Sample
May 28, 2015
99a7533
Fix style in BigqueryServiceFactory
May 28, 2015
3f58947
Fix style errors on BigqueryUtils
May 28, 2015
8fc1c6d
Fix style on ExportDataCloudStorageSample
May 28, 2015
e514139
Fix style on LoadDataCsvSample
May 28, 2015
d989529
Remove vestigial key.json. README points to doc
May 28, 2015
78daa80
Fix StreamingSample checkstyle
May 28, 2015
005689f
Fix SyncQuerySample
May 28, 2015
69e24dc
Fail on bad style
May 28, 2015
b34b01c
Fix java style violations.
May 28, 2015
5019fb6
Merge branch 'jerjou/xml-api' of github.com:GoogleCloudPlatform/java-…
May 28, 2015
957da6b
Fix java style violations.
May 28, 2015
a0d74ee
Remove unused 'all' tag.
May 28, 2015
3f26d5b
Style: prettier line break.
May 29, 2015
a33bbb0
AsyncQuerySample extends BigqueryUtils, so can't be final
May 29, 2015
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
Prev Previous commit
Next Next commit
Fix StreamingSample checkstyle
  • Loading branch information
Bill Prin committed May 28, 2015
commit 78daa804edca12698d823100469eb1fed6b06840
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*
Copyright 2015, 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
Copyright 2015, 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.google.cloud.bigquery.samples;
Expand All @@ -31,14 +31,25 @@


/**
* TODO: Insert description here. (generated by elibixby)
* Example of Bigquery Streaming.
*/
public class StreamingSample extends BigqueryUtils {
public class StreamingSample {

/**
* Empty constructor since this is just a collection of static methods.
*/
protected StreamingSample() {

}




/**
* Command line that demonstrates Bigquery streaming.
* @param args Command line args, should be empty
* @throws IOException IOexception
*/
// [START main]
public static void main(String[] args) throws IOException{
public static void main(final String[] args) throws IOException {
final Scanner scanner = new Scanner(System.in);
System.out.println("Enter your project id: ");
String projectId = scanner.nextLine();
Expand All @@ -47,54 +58,70 @@ public static void main(String[] args) throws IOException{
System.out.println("Enter your table id: ");
String tableId = scanner.nextLine();
scanner.close();

System.out.println("Enter JSON to stream to BigQuery: \n"
+ "Press End-of-stream (CTRL-D) to stop");

JsonReader fromCLI = new JsonReader(new InputStreamReader(System.in));

Iterator<TableDataInsertAllResponse> responses = run(projectId,
datasetId,
tableId,
fromCLI);
while(responses.hasNext()){

while (responses.hasNext()) {
System.out.println(responses.next());
}

fromCLI.close();
}
// [END main]



// [START run]


/**
* Run the bigquery ClI.
* @param projectId Project id
* @param datasetId datasetid
* @param tableId tableid
* @param rows The source of the JSON rows we are streaming in.
* @return Returns Iterates through the stream responses
* @throws IOException Thrown if there is an error connecting to Bigquery.
* @throws InterruptedException Should never be thrown
*/
// [START run]
public static Iterator<TableDataInsertAllResponse> run(final String projectId,
final String datasetId,
final String datasetId,
final String tableId,
final JsonReader rows) throws IOException{
final JsonReader rows) throws IOException {


final Bigquery bigquery = BigqueryServiceFactory.getService();
final Gson gson = new Gson();
rows.beginArray();

return new Iterator<TableDataInsertAllResponse>(){

return new Iterator<TableDataInsertAllResponse>() {

/**
* Get the next row in the stream
* @return True if there is another row in the stream
*/
public boolean hasNext() {
try {
return rows.hasNext();
} catch (IOException e) {
// TODO(elibixby): Auto-generated catch block
e.printStackTrace();
}
return false;
}

/**
*
* @return Next page of data
*/
public TableDataInsertAllResponse next() {
try {
Map<String, Object> rowData = gson.<Map<String, Object>>fromJson(
rows,
rows,
(new HashMap<String, Object>()).getClass());
return streamRow(bigquery,
projectId,
Expand All @@ -112,25 +139,35 @@ public TableDataInsertAllResponse next() {
public void remove() {
this.next();
}

};

}
// [END run]


/**
*
* @param bigquery The bigquery service
* @param projectId project id from Google Developers console
* @param datasetId id of teh dataset
* @param tableId if the table we're streaming
* @param row Id of the row we're inserting
* @return Response from the insert
* @throws IOException ioexception
*/
// [START streamRow]
public static TableDataInsertAllResponse streamRow(Bigquery bigquery,
String projectId,
String datasetId,
String tableId,
TableDataInsertAllRequest.Rows row) throws IOException{
public static TableDataInsertAllResponse streamRow(final Bigquery bigquery,
final String projectId,
final String datasetId,
final String tableId,
final TableDataInsertAllRequest.Rows row) throws IOException {

return bigquery.tabledata().insertAll(
projectId,
datasetId,
tableId,
new TableDataInsertAllRequest().setRows(Collections.singletonList(row))).execute();

projectId,
datasetId,
tableId,
new TableDataInsertAllRequest().setRows(
Collections.singletonList(row))).execute();
}
// [END streamRow]
}