Search code examples
pythonrabbitmqgzippika

Sending GZIP json using Pika and RabbitMQ


I am currently generating a json which I am sending over RabbitMQ. However it suffers with a limit of 128 MB and my json messages are more than 500 MB. The only feasible step is moving forward with sending the gzip version of the file. Howvever I am not able to find proper documentation to do so using PIKA, python and RabbitMQ

I have tried Encoding and Decoding the Json and sending a message. But technically it would same as sending the message via JSON


Solution

  • Python has standard library for gzip

    import json
    import gzip
    
    data: dict = get_data(...)
    data_bytes = json.dumps(data).encode()
    compressed = gzip.compress(data_bytes)
    send(compressed)
    
    # ... sending the data over AMQP
    
    compressed = receive(...)
    decompressed = gzip.decompress(compressed)
    data_decompressed = json.loads(decompressed.decode())
    
    assert data == data_decompressed