编辑python脚本以将JSON负载用于MQTT到Elasticsearch索引

时间:2018-08-21 02:38:37

标签: python json elasticsearch mqtt

我当前正在使用https://github.com/mattfield11/mqtt-elasticSearch上提供的代码,文件格式如下。我需要从MQTT发送到Elasticsearch的两种类型的消息有效负载(JSON)。我一直在努力尝试使其工作,但我似乎无法对此脚本进行任何积极的更改以导入数据,从而无法在Kibana中搜索诸如Payload.C​​ontent之类的每个字段。

{"device":"37F1B2","deviceType":"Sigfox-Tracker","version":34,"time":"1534818609","rssi":"-140.00","battery":2.03,"temp":0,"soil":21044,"xAxis":5201,"yAxis":21034,"zAxis":5204}

{"device":"368640","deviceType":"Sigfox-Tracker","version":3,"time":"Tue Aug 21 2018 02:33:05 GMT+0000 (UTC)","rssi":"-120.00","battery":3.28,"temp":20.87,"soil":1915,"xAxis":59,"yAxis":-38,"zAxis":994}

我将如何使用它?

#this code is developed by Matthew Field http://www.smart-factory.net
#distributed under GNU public license https://www.gnu.org/licenses/gpl.txt

#this program requires the script to be run on the same server as you
#have elasticsearch running
#change the server and port data according to your installation
#the program is simple, but should work fine for testing
#the program will cope with a mixture of string and numeric data
#however it would be wise to develop further if a variety of data types
#such as json is to be used


mqttServer="192.168.1.10"
mqttPort="1883"

channelSubs="$SYS/#"
#use below as alternative to subscribe to all channels
#channelSubs="#"

import paho.mqtt.client as mqtt
from datetime import datetime
from elasticsearch import Elasticsearch


# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe(channelSubs)

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

# this is the syntax to follow for the elasticSearch update taken from documentation
#    es.index(index="my-index", doc_type="test-type", id=42, body={"any": +str(msg.payload, "timestamp": datetime.now()})
#    {u'_id': u'42', u'_index': u'my-index', u'_type': u'test-type', u'_version': 1, u'ok': True}

#our implementation uses this to separate numeric(float) from string data

    try:
    float(msg.payload)
    es.index(index="my-index", doc_type="numeric", body={"topic" : msg.topic, "dataFloat" : float(msg.payload), "timestamp": datetime.utcnow()})

    except:
    es.index(index="my-index", doc_type="string", body={"topic" : msg.topic, "dataString" : msg.payload, "timestamp": datetime.utcnow()})

# by default we connect to elasticSearch on localhost:9200
es = Elasticsearch()

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(mqttServer,mqttPort, 60)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()

0 个答案:

没有答案