小编Jus*_*n S的帖子

从ElasticSearch结果创建DataFrame

我正在尝试使用ElasticSearch的一个非常基本的查询结果在pandas中构建一个DataFrame.我得到了我需要的数据,但它的结果是以一种方式构建正确的数据框.我真的只关心每个结果的时间戳和路径.我尝试了一些不同的es.search模式.

码:

from datetime import datetime
from elasticsearch import Elasticsearch
from pandas import DataFrame, Series
import pandas as pd
import matplotlib.pyplot as plt
es = Elasticsearch(host="192.168.121.252")
res = es.search(index="_all", doc_type='logs', body={"query": {"match_all": {}}}, size=2, fields=('path','@timestamp'))
Run Code Online (Sandbox Code Playgroud)

这给出了4块数据.[u'hits',u'_shards',u'took',u'timed_out'].我的结果是在命中.

res['hits']['hits']
Out[47]: 
[{u'_id': u'a1XHMhdHQB2uV7oq6dUldg',
  u'_index': u'logstash-2014.08.07',
  u'_score': 1.0,
  u'_type': u'logs',
  u'fields': {u'@timestamp': u'2014-08-07T12:36:00.086Z',
   u'path': u'app2.log'}},
 {u'_id': u'TcBvro_1QMqF4ORC-XlAPQ',
  u'_index': u'logstash-2014.08.07',
  u'_score': 1.0,
  u'_type': u'logs',
  u'fields': {u'@timestamp': u'2014-08-07T12:36:00.200Z',
   u'path': u'app1.log'}}]
Run Code Online (Sandbox Code Playgroud)

我唯一关心的是获取时间戳和每次点击的路径.

res['hits']['hits'][0]['fields']
Out[48]: 
{u'@timestamp': u'2014-08-07T12:36:00.086Z',
 u'path': u'app1.log'}
Run Code Online (Sandbox Code Playgroud)

我不能为我的生活弄清楚谁将这个结果,放到熊猫的数据框中.所以对于我返回的2个结果,我希望有一个数据帧.

   timestamp                   path
0  2014-08-07T12:36:00.086Z    app1.log
1  2014-08-07T12:36:00.200Z    app2.log
Run Code Online (Sandbox Code Playgroud)

python elasticsearch pandas

27
推荐指数
5
解决办法
2万
查看次数

如何在避免“太多论点”的同时进行 grep

我试图清除一些垃圾邮件,但遇到了一个问题。队列中的文件数量太大,我通常的命令无法处理。它会给我一个关于太多参数的错误。

我通常这样做

grep -i user@domain.com 1US* | awk -F: '{print $1}' | xargs rm
Run Code Online (Sandbox Code Playgroud)

1US* 可以是 1US[a-zA-Z] 之间的任何值。我唯一能做的就是运行这个可怕的装置。它的一个文件,包含 1USa、1USA、1USb 等,贯穿整个字母表。我知道他们必须是一种更有效地运行它的方法。

grep -s $SPAMMER /var/mailcleaner/spool/exim_stage1/input/1USa* | awk -F: '{print $1}' | xargs rm
grep -s $SPAMMER /var/mailcleaner/spool/exim_stage1/input/1USA* | awk -F: '{print $1}' | xargs rm
Run Code Online (Sandbox Code Playgroud)

linux bash grep

6
推荐指数
1
解决办法
1万
查看次数

如何使用Ruby 2.2.3和rest-client保存文件

我正在尝试使用rest API下载文件,它似乎可以正常工作,但是我实际上没有下载文件。我假设它是因为它将存储在内存中,而不是存储在我的文件系统中。

以下是负责的代码部分。在下面粘贴我的URL时,对其进行了稍微的编辑,并且我的authToken有效。

backup_url = "#{proto}://#{my_host}/applications/ws/migration/export?noaudit=#{include_audit}&includebackup=#{include_backup_zips}&authToken=#{my_token}"
resource = RestClient::Resource.new(
  backup_url,
  :timeout => nil,
  :open_timeout => nil)
response = resource.get
if response.code == 200
    puts "Backup Complete"
else
    puts "Backup Failed"
    abort("Response Code was not 200: Response Code #{response.code}")
end
Run Code Online (Sandbox Code Playgroud)

返回值:

# => 200 OK | application/zip 222094570 bytes
Backup Complete
Run Code Online (Sandbox Code Playgroud)

但是没有文件。

谢谢,

ruby rest-client

5
推荐指数
1
解决办法
1879
查看次数

使用 MyPy 的项目中的 FastAPI/Pydantic

我目前正在学习 fastAPI 教程,我的环境设置了 black、flake8、bandit 和 mypy。本教程中的所有内容都运行良好,但我一直不得不 # type: ignore things 让 mypy 合作。

class Item(BaseModel):
    name: str
    description: str = None
    price: float
    tax: float = None


@app.post("/items/")
async def create_items(item: Item) -> Item:
    return item
Run Code Online (Sandbox Code Playgroud)

Mypy然后错误:

 ? mypy main.py                                                                                                                                                                                                 [14:34:08]
main.py:9: error: Incompatible types in assignment (expression has type "None", variable has type "str")
main.py:11: error: Incompatible types in assignment (expression has type "None", variable has type "float") 
Run Code Online (Sandbox Code Playgroud)

我可以 # type: ignore,但随后我丢失了编辑器中的类型提示和验证。我是否遗漏了一些明显的东西,还是应该为 FastAPI 项目禁用 mypy?

python mypy pydantic fastapi

1
推荐指数
1
解决办法
1227
查看次数

标签 统计

python ×2

bash ×1

elasticsearch ×1

fastapi ×1

grep ×1

linux ×1

mypy ×1

pandas ×1

pydantic ×1

rest-client ×1

ruby ×1