【问题标题】:How to get some original json fields after mapping geo-point with python Elasticsearch?使用python Elasticsearch映射地理点后如何获取一些原始json字段?
【发布时间】:2020-09-25 04:39:06
【问题描述】:

我想通过地理点映射获取一些原始数据。我需要单独使用“geo”获取卫星名称和时间戳

我使用 python Elasticsearch 从 Restful API 获取数据。

settings = { "settings": {
                 "number_of_shards":1,
                  'number_of_replicas':0
                 },
      "mappings" : { 
           "document" : {
                "properties":{
                    "geo": {
                       "type": "geo_point"
                            }
                          }
                        } 
                     } 
                  }

es.indices.create(index = "new", body=settings)

def collect_data():
  data = requests.get(url = URL).json() 
  del data['positions'][1]

  new_data = {'geo':{'lat':data['positions'][0]['satlatitude'],
               'lon':data['positions'][0]['satlongitude']}}, {data['info'][0]['satname']} ,   
                {data['positions'][0]['timestamp']} 



 es.index(index='new', doc_type='document', body=new_data)
schedule.every(10).seconds.do(collect_data)


while True:
 schedule.run_pending()
 time.sleep(1) 

Error received:
SerializationError: (({'geo': {'lat': 37.43662067, 'lon': -26.09384821}}, {1591391688}), 
TypeError("Unable to serialize {1591391688} (type: <class 'set'>)"))

RESTful json 数据样本--- {'info': {'satname': 'SPACE STATION', 'satid': 25544, 'transactionscount': 0}, '仓位': [{'satlatitude': 28.89539607, 'satlongitude':90.44547739,'sataltitude':420.36,'azimuth':12.46, “海拔”:-52.81,“ra”:215.55022984,“dec”:-5.00234017,“时间戳”:1591196844,“黯淡”:
真的}]}

我需要有“geo”、“satnam”和“timestamp”。我想知道如何才能获得正确的结果。

【问题讨论】:

  • 您是否曾在回复中获得多个职位?我认为这里的问题是您需要将{data['positions'][0]['timestamp']} 更改为'timestamp': data['positions'][0]['timestamp']}

标签: python json elasticsearch


【解决方案1】:

看起来您在没有密钥的情况下设置了时间戳和卫星名称,试试这个来处理数据:

import json
from datetime import datetime

response_json = '''
{
    "info": {
        "satname": "SPACE STATION",
        "satid": 25544,
        "transactionscount": 0
    },
    "positions": [
        {
            "satlatitude": 28.89539607,
            "satlongitude": 90.44547739,
            "sataltitude": 420.36,
            "azimuth": 12.46,
            "elevation": -52.81,
            "ra": 215.55022984,
            "dec": -5.00234017,
            "timestamp": 1591196844,
            "eclipsed": true
        }
    ]
}
'''

response_data = json.loads(response_json)



def process_data(data):
    return {
        'satname': response_data['info']['satname'],
        # comvert unix timestamp to iso time
        'timestamp': datetime.fromtimestamp(response_data['positions'][0]['timestamp']).isoformat(),
        'geo': {
            'lat': response_data['positions'][0]['satlatitude'],
            'lon': response_data['positions'][0]['satlongitude']
        }
    }

print(process_data(response_data))

输出:

{'satname': 'SPACE STATION', 'timestamp': '2020-06-03T15:07:24', 'geo': {'lat': 28.89539607, 'lon': 90.44547739}}

【讨论】:

  • 当我从网站流式传输数据时(我提到此数据来自 RESTful API),因此数据每 10 秒更改一次。是否有可能每次都是您指定的数据?实际上我流式传输数据。我用我的完整编码更新了我的代码,以便更好地理解我在做什么。
  • 我使用静态数据只是为了获取正确的 json 格式进出。您可以在代码中使用此 process_data() 函数,并将您的 new_data = ... 行替换为 new_data = process_data(data)
  • @user 根据你的错误,问题只是你的字典定义格式
  • 是的,你是对的!!我遵循了你的两种方式..有工作。谢谢。请稍微澄清一下,如果我需要转换这个 UNIX 时间戳,我是否必须添加 "date": { "type": "date", "format" :“yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis”映射?我不知道在“属性”下我可以有很多映射?
  • 你可以去掉地理标签,将 lat 和 lon 放在与 satname 和 timestamp 相同的级别上
猜你喜欢
  • 2015-10-14
  • 2018-01-08
  • 2022-06-15
  • 2016-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多