全部,
我正在尝试学习如何使用 GCP PubSub,并且可以通过 CLI 命令对其进行测试(创建主题、订阅、发布到主题、从订阅中拉取等),但是当我跳转到 python 时(v 2.7,当前公司标准)我正在努力以同步方式拉取消息。
我已经查看了这个网址,它告诉你去睡觉和“While True”,但我无法想象有人在现实世界中这样做,对吗? https://cloud.google.com/pubsub/docs/quickstart-client-libraries#pubsub-subscribe-python
这个网址告诉您可以使用 future.result(),我尝试过,但它不会像您想象的那样阻塞会话/线程: http: //google-cloud-python.readthedocs.io/en/latest/ pubsub/订阅者/index.html
有人还有其他想法吗?这是我的函数,它几乎直接来自示例之一:
def sample_receive_messages(subscription_name="my-sub", project=None):
"""Receives messages from a pull subscription."""
if not project:
credentials, project = google.auth.default()
subscriber = psub.SubscriberClient()
subscription_path = subscriber.subscription_path(
project, subscription_name)
def callback(message):
# print('Received message: {}'.format(message))
message.ack()
print('<message>' + str(message.data) + '</message>')
subscription = subscriber.subscribe(subscription_path)
future = subscription.open(callback)
myResult = future.result()
subscription.close()
print("done")
Run Code Online (Sandbox Code Playgroud)
我最后的目标是有一个进程经常醒来,抓取消息并确认它们,将消息写入文件,然后结束。
到目前为止,该进程读取消息并将其打印出来(很棒),但它然后坐啊坐啊坐,最后错误地出现了一些乱码:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pubSubTools.py", …Run Code Online (Sandbox Code Playgroud) 全部,
我在使用大约两周前发布的 0.28 版本的 bq 库在 python 中创建 Google BigQuery 视图时遇到问题。我很确定问题出在我这边,我错过了一些东西,但我找不到问题所在。
请温柔点,我不会在网上问很多问题,但我很困惑。我也不是完全无能,这里有一些细节:
我已经查看了 https://cloud.google.com/bigquery/docs/python-client-migration
我认为问题在于“修复” https://github.com/GoogleCloudPlatform/google-cloud-python/pull/4038 BigQuery:用 client.create_table() 替换 table.create() #4038
问题?下面第二块中的代码创建一个表,没有模式也没有记录。显然应该创建一个 VIEW,对吗?
sudo pip install -Iv google-cloud-bigquery==0.27.0
from google.cloud import bigquery
project=None
dataset_name = 'my_dataset_id'
view_name = 'vw_dummy_data20'
sqlQuery = 'select record_id as id, UPPER(first_name) as first_name, UPPER(last_name) as last_name from [my_project_code:my_dataset_id.dummy_data13]'
bigquery_client = bigquery.Client(project=project)
dataset = bigquery_client.dataset(dataset_name)
table = dataset.table(view_name)
table.view_query = sqlQuery …Run Code Online (Sandbox Code Playgroud) 有没有人有一个可行的示例,说明如何直接从Shell通过CURL类型的命令将消息发布到GCP PubSub主题?
我试图不使用CLI,也不使用客户端库,而我却迷上了OAUTH。
如果能够在Linux上运行bash脚本,那么我将有一个项目清单,如果有人拥有或可以将它们拼凑在一起,我将非常感激。
我已经拥有的物品:
我完全认识到Google建议使用CLI或API客户端库,但是我需要能够在主机上以最少的安装(没有CLI,没有python库等)运行它。
我认为我需要执行以下操作:
想法很感激,非常感谢...丰富
参考链接:
https://cloud.google.com/pubsub/docs/publisher#pubsub-publish-message-protocol https://groups.google.com/forum/#!topic/cloud-pubsub-discuss/8fGaG5cWiTk https:// groups.google.com/forum/?hl=sw#!topic/cloud-pubsub-discuss/8fGaG5cWiTk https://developers.google.com/identity/protocols/OAuth2WebServer https://developers.google.com/identity/协议/ OAuth2ServiceAccount