Easy to use Flink REST API client implementation
Project description
Flink REST Client
What is it?
The Flink REST Client provides an easy-to-use python API for Flink REST API. The client implements all available REST API endpoints that are documented on the official Flink site. Using this client, you can easily query your Flink cluster status, or you can upload and run arbitrary Flink jobs wrapped in a Java archive file.
Installation
The source code is currently hosted on GitHub at: https://github.com/frego-dev/flink-rest-client
The installer for the latest released version is available at the Python Package Index (PyPI).
# via PyPI
pip install flink_rest_client
Documentation
The official documentation is hosted on: official documentation
License
API structure
In the client implementation, the API end points are categorized based on their functionality:
- cluster level: API endpoints for managing the whole cluster.
- jobmanager: API endpoints for managing the job manager(s).
- taskmanager: API endpoints for managing the taskmanagers.
- jars: API endpoints for managing the uploaded jars.
- jobs: API endpoints for managing the submitted jobs.
You find the REST API endpoint -> python method mapping in the documentation: [link to mapping]
Usage examples
How to create a new FlinkRestClient instance
The simplest way to create a new FlinkRestClient instance is using its static factory method:
from flink_rest_client import FlinkRestClient
rest_client = FlinkRestClient.get(host="localhost", port=8082)
To check that the client can connect to Flink Jobmanager's webserver the overview method can be used, which returns an overview over the Flink cluster.
from flink_rest_client import FlinkRestClient
rest_client = FlinkRestClient.get(host="localhost", port=8082)
result = rest_client.overview()
print(result)
Output:
{
"taskmanagers": 1,
"slots-total": 4,
"slots-available": 4,
"jobs-running": 0,
"jobs-finished": 0,
"jobs-cancelled": 0,
"jobs-failed": 0,
"flink-version": "1.12.4",
"flink-commit": "5364a6b"
}
How to upload and run a Flink job
from flink_rest_client import FlinkRestClient
rest_client = FlinkRestClient.get(host="localhost", port=8082)
# Path to the jar file on your file system
path_to_my_jar = "/path/to/StateMachineExample.jar"
# The upload_and_run method returns with the unique identifier of the already started Flink job
job_id = rest_client.jars.upload_and_run(path_to_jar=path_to_my_jar)
# Using the job_id, you can query the current status of the submitted job.
job = rest_client.jobs.get(job_id=job_id)
print(job)
Output:
{
"jid": "d8a3c7f257231678c1ca4b97d2316c45",
"name": "State machine job",
"isStoppable": false,
"state": "RUNNING",
"start-time": 1625758267958,
"end-time": -1,
"duration": 206345,
"now": 1625758474303,
"timestamps": {
"FAILING": 0,
"FINISHED": 0,
"INITIALIZING": 1625758267958,
"RESTARTING": 0,
"CREATED": 1625758268002,
"FAILED": 0,
"SUSPENDED": 0,
"CANCELLING": 0,
"CANCELED": 0,
"RECONCILING": 0,
"RUNNING": 1625758268038
},
"vertices": [
...
],
"status-counts": {
"CREATED": 0,
"CANCELING": 0,
"FAILED": 0,
"CANCELED": 0,
"FINISHED": 0,
"SCHEDULED": 0,
"RUNNING": 2,
"DEPLOYING": 0,
"RECONCILING": 0
},
"plan": {
"jid": "d8a3c7f257231678c1ca4b97d2316c45",
"name": "State machine job",
"nodes": [
...
]
}
}
Sometimes you need to pass arguments/parameters to successfully start your Flink job.
For example, you have the following Java Main class:
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
public class MyFlinkJob {
private static final String PARAM_THRESHOLD = "my.flink.job.threshold";
public static void main(String[] args) {
// Reading configuration
ParameterTool argsParams = ParameterTool.fromArgs(args);
int threshold = argsParams.getInt(PARAM_THRESHOLD);
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
// ...
}
}
The required argument can be passed via upload_and_run method's arguments
parameter:
from flink_rest_client import FlinkRestClient
rest_client = FlinkRestClient.get(host="localhost", port=8082)
# Path to the jar file on your file system
path_to_my_jar = "/path/to/StateMachineExample.jar"
# Put the arguments in a dictionary
job_id = rest_client.jars.upload_and_run(path_to_jar=path_to_my_jar, arguments={
"my.flink.job.threshold": 55
})
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flink-rest-client-1.0.8.tar.gz
.
File metadata
- Download URL: flink-rest-client-1.0.8.tar.gz
- Upload date:
- Size: 19.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2148bb7eab59395fb460517314d6f53e24a161f2b62fe6245d07d0219c0f0f15 |
|
MD5 | b962ad6f52f35aa97a6e750589b0e01b |
|
BLAKE2b-256 | cc91b69c7d7259e7b91d2bac1b3eba324d912aa99d65996acd63ca08122de21c |
File details
Details for the file flink_rest_client-1.0.8-py3-none-any.whl
.
File metadata
- Download URL: flink_rest_client-1.0.8-py3-none-any.whl
- Upload date:
- Size: 24.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89a8c8933538e4ece667f8b41d5c6b005693b5f87cd37b88e7414174fa9e4891 |
|
MD5 | d992c1e8821d1f2f7f13032a4dd31b56 |
|
BLAKE2b-256 | e159bd08abf176ec59b7ab3307bc38873458bac7b03961bfe97c2178eef59391 |