Convert live data into redis

1973 views python
9

I need help about how t store live data into Redis.

I use Splunk ResultReader to get the live data from Splunk API.

I also use requests method to connect the Redis Server.

But I am not sure how to put them together.

My code is below:

import splunklib.client as client
import splunklib.results as results
import json
HOST = "XXXX"
PORT = XXXX
USERNAME = "XXXX"
PASSWORD = "XXXXX"

# Create a Service instance and log in 
service = client.connect(
    host=HOST,
    port=PORT,
    username=USERNAME,
    password=PASSWORD)
rr = results.ResultsReader(service.jobs.export("Splunk query"))
for result in rr:
    if isinstance(result, results.Message):
        # Diagnostic messages might be returned in the results
        print ('%s: %s' % (result.type, result.message))
    elif isinstance(result, dict):
        print(result)

import requests
BASE_URL = "https://redis-sre-demo.apps.net"
body ="bodyString"

## Subscribe to all events for {key}
getRequest = requests.get('{baseURL}/SUBSCRIBE/__keyspace@0__:{key}'.format    (baseURL=BASE_URL, key="sre-metrics"), auth=('redis', 'ccbsre'), stream=True)
generator = getRequest.iter_content(chunk_size=None, decode_unicode=True)

for line in generator:
    print(line)

answered question

1 Answer

2

redis has a python module which make it easier to write/read data to redis server, you can use it.

Example,

>>> import redis
>>> r = redis.StrictRedis(host='localhost', port=6379, db=0)
>>> r.set('foo', 'bar')
True
>>> r.get('foo')
'bar'

posted this

Have an answer?

JD

Please login first before posting an answer.