簡體   English   中英

Python: .json 數據來自 splunk

[英]Python: .json data from splunk

以下問題:對於我的大學項目,我將 json 文件上傳到 splunk,現在我想在 python 中將其用作 dataframe2668CFDE63911DCB49.

代碼:

import urllib3
import requests
import json
import pandas as pd

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


server = 'localhost'
port = 8089
username = 'testuid'
password = 'testpw'
url='https://'+ server +':' + str(port)
param = {'shortname', 'permissionId'}

search='?search=search source%3D%22events.json%22%20host%3D%22DESKTOP-9QDQ0FT%22%20index%3D%22projektseminar%22%20sourcetype%3D%22_json%22%20%7Chead%2020%20%7Ctable%20shortname%20permissionId'

output_type =  '&output_mode=json'
search_url = url + '/servicesNS/nobody/search/search/jobs/export' + search + output_type

r = requests.get(search_url, auth=(username, password), verify=False)

到目前為止,效果很好。 現在我想要這個特定的“r”響應 object 作為 dataframe object 與 2 列“shortname”和“permissionId”。 我有幾個問題。 首先,我從 Rest API 獲得的 json 帶有“預覽”、“偏移”和“結果”列。 我想要一個帶有“shortname”和“permissionId”列的 dataframe。 問題是我不能使用json.load(r)r.json()或類似的東西,總是出現“額外數據”錯誤。 So I'm a beginner with splunk and python so maybe there is a better way to do so... Another idea I didn't tried yet is to use a csv output instead of json. 如果你們對如何解決這個問題有一些建議,那就太好了。

謝謝

實現此目的的最佳方法是使用 Splunk API for Python

您可以在此處找到 SDK: https://github.com/splunk/splunk-sdk-python

import sys
import os
from time import sleep
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "splunk-sdk-python-1.6.13"))

import splunklib.client as client
import splunklib.results as results

import pandas as pd

# Note my port, username, and password are specific to my instance. The default port is 8089

service = client.connect(host='localhost', port=MY_PORT,
                   username='MY_USER', password='MY_PASS')

search = """search index=_internal sourcetype="splunkd_access" |table *"""

job = service.jobs.create(search)
while True:
    while not job.is_ready():
        pass
    if job['isDone'] == '1':
        break
    sleep(2)


reader = results.ResultsReader(job.results())


df = pd.DataFrame(reader)
print(df)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM