Reading the data written to s3 by Amazon Kinesis Firehose stream

后端 未结 9 2052
感情败类
感情败类 2021-02-18 15:17

I am writing record to Kinesis Firehose stream that is eventually written to a S3 file by Amazon Kinesis Firehose.

My record object looks like

ItemPurcha         


        
9条回答
  •  忘了有多久
    2021-02-18 15:27

    You can find the each valid JSON by counting the brackets. Assuming the file starts with a { this python snippet should work:

    import json
    
    def read_block(stream):
        open_brackets = 0
        block = ''
        while True:
            c = stream.read(1)
            if not c:
                break
    
            if c == '{':
                open_brackets += 1
            elif c == '}':
                open_brackets -= 1
    
            block += c
    
            if open_brackets == 0:
                yield block
                block = ''
    
    
    if __name__ == "__main__":
        c = 0
        with open('firehose_json_blob', 'r') as f:
            for block in read_block(f):
                record = json.loads(block)
                print(record)
    

提交回复
热议问题