-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Upgrading pubsub to 0.9.2-alpha, quick start update #522
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
Changes from 2 commits
b929c85
730579e
1f74343
758e94a
32dc9f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,18 @@ Install [Maven](http://maven.apache.org/). | |
Build your project with: | ||
|
||
mvn clean package -DskipTests | ||
|
||
You can then run a given `ClassName` via: | ||
|
||
mvn exec:java -Dexec.mainClass=com.example.pubsub.ClassName \ | ||
-DpropertyName=propertyValue \ | ||
-Dexec.args="any arguments to the app" | ||
## Testing | ||
|
||
To run the tests for this sample, first set the `GOOGLE_CLOUD_PROJECT` | ||
environment variable. The project should have a dataset named `test_dataset` | ||
with a table named `test_table`. | ||
|
||
export GOOGLE_CLOUD_PROJECT=my-project | ||
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. Newlines are your friend between 22-23 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. done :) |
||
|
||
Then run the tests with Maven. | ||
|
||
mvn clean verify | ||
|
||
### Creating a new topic (using the quickstart sample) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
Copyright 2016, Google, Inc. | ||
Copyright 2017, Google, Inc. | ||
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 turns out that the copyright year should actually be the date of first publish, and establishes how far back the copyright extends; so it's generally more advantageous for it to be lower. 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. +1 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. Fixed |
||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
|
@@ -18,23 +18,44 @@ | |
|
||
// [START pubsub_quickstart] | ||
// Imports the Google Cloud client library | ||
import com.google.cloud.pubsub.PubSub; | ||
import com.google.cloud.pubsub.PubSubOptions; | ||
import com.google.cloud.pubsub.Topic; | ||
import com.google.cloud.pubsub.TopicInfo; | ||
|
||
public class QuickstartSample { | ||
public static void main(String... args) throws Exception { | ||
// Instantiates a client | ||
PubSub pubsub = PubSubOptions.getDefaultInstance().getService(); | ||
import com.google.api.gax.core.RpcFuture; | ||
import com.google.cloud.pubsub.spi.v1.Publisher; | ||
import com.google.cloud.pubsub.spi.v1.PublisherClient; | ||
import com.google.protobuf.ByteString; | ||
import com.google.pubsub.v1.PubsubMessage; | ||
import com.google.pubsub.v1.TopicName; | ||
|
||
// The name for the new topic | ||
String topicName = "my-new-topic"; | ||
public class QuickstartSample { | ||
|
||
// Creates the new topic | ||
Topic topic = pubsub.create(TopicInfo.of(topicName)); | ||
public static void main(String... args) throws Exception { | ||
|
||
System.out.printf("Topic %s created.%n", topic.getName()); | ||
// Create a new topic | ||
String projectId = args[0]; | ||
TopicName topic = TopicName.create(projectId, "my-new-topic"); | ||
try (PublisherClient publisherClient = PublisherClient.create()) { | ||
publisherClient.createTopic(topic); | ||
} | ||
System.out.printf("Topic %s:%s created.\n", topic.getProject(), topic.getTopic()); | ||
|
||
// Creates a publisher | ||
Publisher publisher = null; | ||
try { | ||
publisher = Publisher.newBuilder(topic).build(); | ||
|
||
//Publish a message asynchronously | ||
String message = "my-message"; | ||
ByteString data = ByteString.copyFromUtf8(message); | ||
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); | ||
RpcFuture<String> messageIdFuture = publisher.publish(pubsubMessage); | ||
|
||
//Print message id of published message | ||
System.out.println("published with message ID: " + messageIdFuture.get()); | ||
} finally { | ||
if (publisher != null) { | ||
publisher.shutdown(); | ||
} | ||
} | ||
} | ||
} | ||
// [END pubsub_quickstart] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
Copyright 2016, Google, Inc. | ||
Copyright 2017, Google, Inc. | ||
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. roll back 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. Fixed |
||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
|
@@ -18,15 +18,17 @@ | |
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.cloud.pubsub.PubSub; | ||
import com.google.cloud.pubsub.PubSubOptions; | ||
import com.google.cloud.pubsub.spi.v1.PublisherClient; | ||
import com.google.pubsub.v1.TopicName; | ||
|
||
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.IOException; | ||
import java.io.PrintStream; | ||
|
||
/** | ||
|
@@ -35,34 +37,44 @@ | |
@RunWith(JUnit4.class) | ||
@SuppressWarnings("checkstyle:abbreviationaswordinname") | ||
public class QuickstartSampleIT { | ||
|
||
private ByteArrayOutputStream bout; | ||
private PrintStream out; | ||
private String projectId; | ||
|
||
private static final void deleteTestTopic() { | ||
PubSub pubsub = PubSubOptions.getDefaultInstance().getService(); | ||
String topicName = "my-new-topic"; | ||
pubsub.deleteTopic(topicName); | ||
private void deleteTestTopic(String projectId) throws Exception { | ||
try (PublisherClient publisherClient = PublisherClient.create()) { | ||
publisherClient.deleteTopic(TopicName.create(projectId, "my-new-topic")); | ||
} catch (IOException e) { | ||
System.err.println("Error deleting topic " + e.getMessage()); | ||
} | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
deleteTestTopic(); | ||
|
||
bout = new ByteArrayOutputStream(); | ||
out = new PrintStream(bout); | ||
System.setOut(out); | ||
projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
assertThat(projectId).isNotNull(); | ||
try { | ||
deleteTestTopic(projectId); | ||
} catch (Exception e) { | ||
//empty catch block | ||
} | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
public void tearDown() throws Exception { | ||
System.setOut(null); | ||
deleteTestTopic(); | ||
deleteTestTopic(projectId); | ||
} | ||
|
||
@Test | ||
public void testQuickstart() throws Exception { | ||
QuickstartSample.main(); | ||
QuickstartSample.main(projectId); | ||
String got = bout.toString(); | ||
assertThat(got).contains("Topic my-new-topic created."); | ||
assertThat(got).contains("my-new-topic created."); | ||
assertThat(got).contains("published with message ID"); | ||
} | ||
} |
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.
Is this the right README change? Isn't pubsub organized by topic, rather than dataset/table?
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.
+1 to name changes
If you are going to require something, show them how to create it.
gcloud ...
LGTM otherwise.
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.
Fixed, copy-paste error