简体   繁体   English

使用 python 在 Azure 事件中心收集 Websocket 流数据

[英]Collect Websocket streaming data in Azure Event Hub using python

I am trying to collect bitcoin live streaming data to Azure event hub.我正在尝试将比特币实时流数据收集到 Azure 事件中心。 Below is the code how we can do in on local machine.下面是我们如何在本地机器上执行的代码。

How can I use this code to collect the streaming in Azure Event Hub.如何使用此代码在 Azure 事件中心收集流。 All examples and documents provided by Microsoft are unclear.微软提供的所有示例和文档都不清楚。

import logging
import websocket, json

cc = "btcusd"
interval = '1m'
socket = f'wss://stream.binance.com:9443/ws/{cc}t@kline_{interval}'

def on_message(ws, message):
    json_message = json.loads(message)
    candle = json_message['k']
    is_candle_close = candle['x']
    close = candle['c']
    high = candle['h']
    low = candle['l']
    open = candle['o']
    quote = {
        'close': close,
        'high': high,
        'low': low,
        'open': open
    }
    print(f'{cc} {is_candle_close} {quote}')

def on_error(ws, error):
    logging.error(error)

def on_close(ws):
    logging.info("### closed ###")
    
logging.info("Start")
logging.info("Connecting to websocket")
websocket.enableTrace(True)
ws = websocket.WebSocketApp(socket,
                            on_message = on_message,
                            on_error = on_error,
                            on_close = on_close)
ws.run_forever()

You have 2 tasks at hand here:您手头有 2 个任务:

  • Sending the stream of data to Event Hub.将数据流发送到事件中心。 I would get started on that first, and use the Event Hub Python SDK in your local app.我将首先开始,并在您的本地应用程序中使用事件中心 Python SDK
  • Hosting your app in Azure.在 Azure 中托管您的应用程序。 Once you're done on the Event Hub side, you can deploy your app in Azure Functions.在事件中心端完成后,您可以在 Azure Functions 中部署您的应用程序。 See the Python dev guide for that (note that you can develop on Windows locally but will need to deploy to a Linux runtime, but that shouldn't be a problem).请参阅Python 开发指南(请注意,您可以在本地 Windows 上进行开发,但需要部署到 Linux 运行时,但这应该不是问题)。 See here to get started from scratch.请参阅此处从头开始。

Functions is not the only option, but potentially here the simplest.函数不是唯一的选择,但这里可能是最简单的。 See that list for the alternatives.有关替代方案,请参阅该列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM