In this tutorial, we will get to know the standard Python interface to send and receive data over ZebraStream in Python. We provide a python package with different interfaces, aiming at smooth integration into existing code and workflows. All these interfaces are abstractions over the ZebraStream Connect API and Data API, and use plain HTTP requests, and can be considered a lightweight SDK and reference implementation of the ZebraStream APIs in Python.
Installation
Our open-source Python package is hosted on PyPI and can be installed through standard tools.
pip install zebrastream-io
File-like Objects
The simplest way to use ZebraStream is to use the file-like streaming interface, which makes passing data to a remote system or organization as easy as writing to a local file. You will only need two things to connect: 1) the stream path and 2) a corresponding access token. Both can obtained using the ZebraStream CLI.
Here is a producer that sends two chunks of data with a short break in between:
import time
import zebrastream.io.file as zsfile with zsfile.open(mode="wb", stream_path="/my-stream", access_token=token) as f:
f.write(b"Hello ZebraStream!")
time.sleep(60)
f.write(b"that's it")
Note that connecting and waiting for the consumer happens in the open()
call, which blocks until both peers are connected. An already waiting consumer will bet notified, or this code can simply act as a long-running waiter, like a web server.
Here is a consumer that pulls all data and displays it as text:
import zebrastream.io.file as zsfile
with zsfile.open(mode="rb", stream_path="/my-stream", access_token=token) as f:
msg = f.read()
print(msg.decode())
This consumer simply connects and waits for the producer, then gathers all data in one piece and prints it to the console. Note that the interface implements a binary stream, and returns bytes
objects.
File-like objects in Python are a widely adopted interface and work with different packages that expect to read and write data to and from files, pipes, or sockets. In the next tutorials, we'll see how we can use those packages with ZebraStream to stream data objects like tables.
Full Interoperability
The best type of integration depends on the use case and system, and often differs between producer and consumer. Due to ZebraStream's generic approach and protocol, producers and consumers with different stacks can be freely combined. For instance you can write a tiny Python server that pulls data from a database and delivers it as a CSV file, once the URL is clicked in a web browser. Similarly, you can use our BASH producer to stream a system log and process it on-the-fly with a Python consumer.