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: flink_rest_client.frego.dev
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.1.tar.gz
.
File metadata
- Download URL: flink-rest-client-1.0.1.tar.gz
- Upload date:
- Size: 18.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f902d767915a02537a4033f0a8951f82ca5bb8f1847d5b8a75fada716fdd508 |
|
MD5 | 1a760522653b2a8c436cf347dd2823f6 |
|
BLAKE2b-256 | 833f511ea60d2eceeeb7ba376bd25be456023dad214b47210486f8e5005609d8 |
File details
Details for the file flink_rest_client-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: flink_rest_client-1.0.1-py3-none-any.whl
- Upload date:
- Size: 24.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3feb5e3c64c3411ba8d440d149178ad6164496e1e6b1c7e8ade5dc3340416f1 |
|
MD5 | c88481b999c7e39303bc3524c2d4f4df |
|
BLAKE2b-256 | e08a565f04a4288df33bcca9c87fd1636b0533bbaf07a9d1eea2cfaee0041d29 |