Skip to content

Commit c2fc97a

Browse files
committed
Merge remote-tracking branch 'upstream/master' into flex_logging_fix
2 parents 535f422 + 776d39f commit c2fc97a

20 files changed

Lines changed: 899 additions & 73 deletions

File tree

google-cloud-datastore/README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ Java idiomatic client for [Google Cloud Datastore](https://cloud.google.com/data
1212
- [Homepage](https://googlecloudplatform.github.io/google-cloud-java/)
1313
- [API Documentation](https://googlecloudplatform.github.io/google-cloud-java/apidocs/index.html?com/google/cloud/datastore/package-summary.html)
1414

15-
> Note: This client is a work-in-progress, and may occasionally
16-
> make backwards-incompatible changes.
17-
1815
Quickstart
1916
----------
2017
If you are using Maven, add this to your pom.xml file
@@ -170,9 +167,7 @@ Versioning
170167

171168
This library follows [Semantic Versioning](http://semver.org/).
172169

173-
It is currently in major version zero (``0.y.z``), which means that anything
174-
may change at any time and the public API should not be considered
175-
stable.
170+
It is currently in major version one (``1.y.z``), which means that the public API should be considered stable.
176171

177172
Contributing
178173
------------
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2017 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* EDITING INSTRUCTIONS
19+
* This file is referenced in READMEs. Any change to this file should be reflected in
20+
* the project's READMEs.
21+
*/
22+
23+
package com.google.cloud.examples.language.snippets;
24+
25+
import com.google.cloud.language.spi.v1.LanguageServiceClient;
26+
27+
import com.google.cloud.language.v1.Document;
28+
import com.google.cloud.language.v1.Document.Type;
29+
import com.google.cloud.language.v1.Sentiment;
30+
31+
/**
32+
* A snippet for Google Cloud Speech API showing how to analyze text message sentiment.
33+
*/
34+
public class AnalyzeSentiment {
35+
36+
public static void main(String... args) throws Exception {
37+
// Instantiates a client
38+
LanguageServiceClient language = LanguageServiceClient.create();
39+
40+
// The text to analyze
41+
String[] texts = {"I love this!", "I hate this!"};
42+
for (String text : texts) {
43+
Document doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build();
44+
// Detects the sentiment of the text
45+
Sentiment sentiment = language.analyzeSentiment(doc).getDocumentSentiment();
46+
47+
System.out.printf("Text: \"%s\"%n", text);
48+
System.out.printf(
49+
"Sentiment: score = %s, magnitude = %s%n",
50+
sentiment.getScore(), sentiment.getMagnitude());
51+
}
52+
}
53+
}

google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.examples.pubsub.snippets;
1818

1919
import com.google.api.core.ApiFuture;
20+
import com.google.api.core.ApiFutures;
2021
import com.google.cloud.pubsub.spi.v1.Publisher;
2122
import com.google.cloud.pubsub.spi.v1.TopicAdminClient;
2223
import com.google.protobuf.ByteString;
@@ -31,30 +32,54 @@
3132
* publish messages to it.
3233
*/
3334
public class CreateTopicAndPublishMessages {
34-
public static void main(String... args) throws Exception {
35+
36+
public static void createTopic() throws Exception {
3537
TopicName topic = TopicName.create("test-project", "test-topic");
3638
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
3739
topicAdminClient.createTopic(topic);
3840
}
41+
}
3942

43+
public static void publishMessages() throws Exception {
44+
// [START publish]
45+
TopicName topicName = TopicName.create("test-project", "test-topic");
4046
Publisher publisher = null;
47+
List<ApiFuture<String>> messageIdFutures = new ArrayList<>();
48+
4149
try {
42-
publisher = Publisher.defaultBuilder(topic).build();
50+
// Create a publisher instance with default settings bound to the topic
51+
publisher = Publisher.defaultBuilder(topicName).build();
52+
4353
List<String> messages = Arrays.asList("first message", "second message");
44-
List<ApiFuture<String>> messageIds = new ArrayList<>();
54+
55+
// schedule publishing one message at a time : messages get automatically batched
4556
for (String message : messages) {
4657
ByteString data = ByteString.copyFromUtf8(message);
58+
// message data is converted to base64-encoding
4759
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
60+
61+
// Once published, returns a server-assigned message id (unique within the topic)
4862
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
49-
messageIds.add(messageIdFuture);
50-
}
51-
for (ApiFuture<String> messageId : messageIds) {
52-
System.out.println("published with message ID: " + messageId.get());
63+
messageIdFutures.add(messageIdFuture);
5364
}
5465
} finally {
66+
// wait on any pending publish requests.
67+
List<String> messageIds = ApiFutures.allAsList(messageIdFutures).get();
68+
69+
for (String messageId : messageIds) {
70+
System.out.println("published with message ID: " + messageId);
71+
}
72+
5573
if (publisher != null) {
74+
// When finished with the publisher, shutdown to free up resources.
5675
publisher.shutdown();
5776
}
5877
}
78+
// [END publish]
79+
}
80+
81+
public static void main(String... args) throws Exception {
82+
createTopic();
83+
publishMessages();
5984
}
6085
}

google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherSnippets.java

Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,24 @@
2424
import com.google.api.core.ApiFuture;
2525
import com.google.api.core.ApiFutureCallback;
2626
import com.google.api.core.ApiFutures;
27+
import com.google.api.gax.batching.BatchingSettings;
28+
import com.google.api.gax.batching.FlowControlSettings;
29+
import com.google.api.gax.batching.FlowController.LimitExceededBehavior;
30+
import com.google.api.gax.core.CredentialsProvider;
31+
import com.google.api.gax.core.FixedCredentialsProvider;
32+
import com.google.api.gax.grpc.ChannelProvider;
33+
import com.google.api.gax.grpc.ExecutorProvider;
34+
import com.google.api.gax.grpc.InstantiatingExecutorProvider;
35+
import com.google.api.gax.retrying.RetrySettings;
36+
import com.google.auth.oauth2.ServiceAccountCredentials;
2737
import com.google.cloud.pubsub.spi.v1.Publisher;
38+
import com.google.cloud.pubsub.spi.v1.TopicAdminSettings;
2839
import com.google.protobuf.ByteString;
2940
import com.google.pubsub.v1.PubsubMessage;
3041
import com.google.pubsub.v1.TopicName;
42+
import org.threeten.bp.Duration;
43+
44+
import java.io.FileInputStream;
3145

3246
/** This class contains snippets for the {@link Publisher} interface. */
3347
public class PublisherSnippets {
@@ -41,7 +55,6 @@ public PublisherSnippets(Publisher publisher) {
4155
// [TARGET publish(PubsubMessage)]
4256
// [VARIABLE "my_message"]
4357
public ApiFuture<String> publish(String message) {
44-
// [START publish]
4558
ByteString data = ByteString.copyFromUtf8(message);
4659
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
4760
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
@@ -54,24 +67,112 @@ public void onFailure(Throwable t) {
5467
System.out.println("failed to publish: " + t);
5568
}
5669
});
57-
// [END publish]
5870
return messageIdFuture;
5971
}
6072

6173
/** Example of creating a {@code Publisher}. */
6274
// [TARGET newBuilder(TopicName)]
6375
// [VARIABLE "my_project"]
6476
// [VARIABLE "my_topic"]
65-
public static void newBuilder(String projectName, String topicName) throws Exception {
66-
// [START newBuilder]
67-
TopicName topic = TopicName.create(projectName, topicName);
77+
public static void newBuilder(String projectId, String topicId) throws Exception {
78+
TopicName topic = TopicName.create(projectId, topicId);
6879
Publisher publisher = Publisher.defaultBuilder(topic).build();
6980
try {
7081
// ...
7182
} finally {
7283
// When finished with the publisher, make sure to shutdown to free up resources.
7384
publisher.shutdown();
7485
}
75-
// [END newBuilder]
86+
}
87+
88+
public Publisher getPublisherWithCustomBatchSettings(TopicName topicName) throws Exception {
89+
// [START publisherBatchSettings]
90+
// Batch settings control how the publisher batches messages
91+
long requestBytesThreshold = 5000L; // default : 1kb
92+
long messageCountBatchSize = 10L; // default : 100
93+
94+
Duration publishDelayThreshold = Duration.ofMillis(100); // default : 1 ms
95+
96+
// Publish request get triggered based on request size, messages count & time since last publish
97+
BatchingSettings batchingSettings = BatchingSettings.newBuilder()
98+
.setElementCountThreshold(messageCountBatchSize)
99+
.setRequestByteThreshold(requestBytesThreshold)
100+
.setDelayThreshold(publishDelayThreshold)
101+
.build();
102+
103+
Publisher publisher = Publisher.defaultBuilder(topicName)
104+
.setBatchingSettings(batchingSettings).build();
105+
// [END publisherBatchSettings]
106+
return publisher;
107+
}
108+
109+
public Publisher getPublisherWithCustomRetrySettings(TopicName topicName) throws Exception {
110+
// [START publisherRetrySettings]
111+
// Retry settings control how the publisher handles retryable failures
112+
Duration retryDelay = Duration.ofMillis(100); // default : 1 ms
113+
double retryDelayMultiplier = 2.0; // back off for repeated failures
114+
Duration maxRetryDelay = Duration.ofSeconds(5); // default : 10 seconds
115+
116+
RetrySettings retrySettings = RetrySettings.newBuilder()
117+
.setInitialRetryDelay(retryDelay)
118+
.setRetryDelayMultiplier(retryDelayMultiplier)
119+
.setMaxRetryDelay(maxRetryDelay)
120+
.build();
121+
122+
Publisher publisher = Publisher.defaultBuilder(topicName)
123+
.setRetrySettings(retrySettings).build();
124+
// [END publisherRetrySettings]
125+
return publisher;
126+
}
127+
128+
public Publisher getPublisherWithCustomFlowControlSettings(TopicName topicName) throws Exception {
129+
// [START publisherFlowControlSettings]
130+
131+
// Flow control settings restrict the number of outstanding publish requests
132+
int maxOutstandingBatches = 20;
133+
int maxOutstandingRequestBytes = 500000;
134+
135+
// override behavior on limits exceeded if needed, default behavior is to block
136+
LimitExceededBehavior limitExceededBehavior = LimitExceededBehavior.ThrowException;
137+
138+
FlowControlSettings flowControlSettings = FlowControlSettings.newBuilder()
139+
.setMaxOutstandingElementCount(maxOutstandingBatches)
140+
.setMaxOutstandingRequestBytes(maxOutstandingRequestBytes)
141+
.setLimitExceededBehavior(limitExceededBehavior)
142+
.build();
143+
144+
Publisher publisher = Publisher.defaultBuilder(topicName)
145+
.setFlowControlSettings(flowControlSettings).build();
146+
// [END publisherFlowControlSettings]
147+
return publisher;
148+
}
149+
150+
public Publisher getSingleThreadedPublisher(TopicName topicName) throws Exception {
151+
// [START singleThreadedPublisher]
152+
// create a publisher with a single threaded executor
153+
ExecutorProvider executorProvider = InstantiatingExecutorProvider.newBuilder()
154+
.setExecutorThreadCount(1).build();
155+
Publisher publisher = Publisher.defaultBuilder(topicName)
156+
.setExecutorProvider(executorProvider).build();
157+
// [END singleThreadedPublisher]
158+
return publisher;
159+
}
160+
161+
private Publisher createPublisherWithCustomCredentials(TopicName topicName) throws Exception {
162+
// [START publisherWithCustomCredentials]
163+
// read service account credentials from file
164+
CredentialsProvider credentialsProvider =
165+
FixedCredentialsProvider
166+
.create(ServiceAccountCredentials.fromStream(
167+
new FileInputStream("credentials.json")));
168+
ChannelProvider channelProvider =
169+
TopicAdminSettings.defaultChannelProviderBuilder()
170+
.setCredentialsProvider(credentialsProvider).build();
171+
172+
Publisher publisher = Publisher.defaultBuilder(topicName)
173+
.setChannelProvider(channelProvider)
174+
.build();
175+
// [START publisherWithCustomCredentials]
176+
return publisher;
76177
}
77178
}

0 commit comments

Comments
 (0)