Registry / database / hdfs

hdfs

JSON →
library2.7.3pypypi✓ verified 52d ago

HdfsCLI provides a Python API and command-line interface for interacting with Hadoop HDFS via the WebHDFS (and HttpFS) API. It supports both secure and insecure clusters, offering Python 3 bindings for common HDFS operations. The library includes optional extensions for handling Avro files, Pandas DataFrames, and Kerberos authentication. The current version, 2.7.3, was released on October 12, 2023, indicating active maintenance.

databasedataserialization
pip install hdfs
Install & Compatibility
Where this runs
tested against v2.7.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
5/10 runs
5/10 runs
py 3.11
5/10 runs
5/10 runs
py 3.12
5/10 runs
5/10 runs
py 3.13
5/10 runs
5/10 runs
py 3.9
5/10 runs
5/10 runs
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

InsecureClient
from hdfs.client import InsecureClient
The default and simplest client for insecure HDFS clusters.
Client
from hdfs.client import Client
The base client class, InsecureClient is a subclass. Often used via `Client.from_alias()`.
TokenClient
from hdfs.client import TokenClient
Used for token-based authentication with HDFS.
KerberosClient
from hdfs.ext.kerberos import KerberosClient
Used for Kerberos authenticated clusters, requires the 'kerberos' extension.

This quickstart demonstrates how to establish a connection to an HDFS Namenode using `InsecureClient`, write a simple file, list directory contents, read the file back, and then delete it. It uses environment variables for the Namenode URL and user for flexibility. Ensure your HDFS cluster is running and accessible at the specified URL.

import os from hdfs.client import InsecureClient HDFS_NAMENODE_URL = os.environ.get('HDFS_NAMENODE_URL', 'http://localhost:50070') HDFS_USER = os.environ.get('HDFS_USER', 'guest') # Or a specific HDFS user try: client = InsecureClient(HDFS_NAMENODE_URL, user=HDFS_USER) print(f"Connected to HDFS at {HDFS_NAMENODE_URL} as user {HDFS_USER}") # Example: Create a file hdfs_path = '/user/temp/my_test_file.txt' local_data = b'Hello, HdfsCLI world!' with client.write(hdfs_path, encoding='utf-8', overwrite=True) as writer: writer.write(local_data.decode('utf-8')) print(f"Successfully wrote to {hdfs_path}") # Example: List contents of a directory parent_dir = os.path.dirname(hdfs_path) if parent_dir == '': parent_dir = '/' # handle root edge case print(f"Contents of {parent_dir}:") for item in client.list(parent_dir): print(f"- {item}") # Example: Read the file back with client.read(hdfs_path, encoding='utf-8') as reader: read_data = reader.read() print(f"Read from {hdfs_path}: {read_data}") # Example: Delete the file client.delete(hdfs_path) print(f"Successfully deleted {hdfs_path}") except Exception as e: print(f"An error occurred: {e}") print("Please ensure HDFS is running and HDFS_NAMENODE_URL/HDFS_USER are correctly configured.")
hdfs --version
Debug
Known issues
breakingHdfsCLI version 2.x and above has dropped official support for Python 2.x. It is compatible with Python 3.7+.
fix
Upgrade your Python environment to 3.7 or newer. If you must use Python 2, you'll need to use an older version of the `hdfs` library (e.g., `hdfs<2.0.0`), but this is not recommended due to lack of maintenance.
affects: <2.0.0
gotchaBy default, `client.write()` will raise an `HdfsError` if trying to write to an existing path. To overwrite an existing file, you must explicitly set `overwrite=True`.
fix
When calling `client.write()`, include `overwrite=True` in the arguments if you intend to replace an existing file (e.g., `client.write(path, overwrite=True)`).
affects: All
gotchaDeleting a non-empty directory without `recursive=True` will raise an `HdfsError`. This is a safety mechanism.
fix
To delete a directory and its contents, use `client.delete(path, recursive=True)`. Consider `skip_trash=False` (requires Hadoop 2.9+) if you want files to go to trash instead of being permanently deleted.
affects: All
gotchaUsing `Client.from_alias()` relies on a configuration file (default: `~/.hdfscli.cfg`) which defines cluster connection details. Without proper configuration, this method will fail.
fix
Ensure you have a `~/.hdfscli.cfg` file (or `HDFSCLI_CONFIG` environment variable pointing to one) with valid alias definitions, including `url` and optional `user` or `client` (e.g., `KerberosClient`).
affects: All
gotchaThe `KerberosClient` requires the `hdfs[kerberos]` extra to be installed and proper Kerberos configuration on the client machine and HDFS cluster. Misconfiguration often leads to authentication errors.
fix
Install the kerberos extension (`pip install hdfs[kerberos]`). Ensure your `krb5.conf` is correctly configured and you have a valid Kerberos ticket. Refer to the HdfsCLI documentation for detailed Kerberos setup instructions.
affects: All
Errors
Common errors & fixes
OSError: HDFS connection failed
This error typically occurs when the HDFS client cannot establish a connection to the Hadoop Distributed File System, often due to incorrect host, port, or user parameters in the client initialization.
fix
Verify the HDFS Namenode host and port (e.g., from `core-site.xml`'s `fs.default.name` property) and ensure they are correctly specified when initializing the `hdfs.InsecureClient` or `hdfs.Client`. Ensure the HDFS service is running and accessible from the client machine. 
```python
from hdfs import InsecureClient

# Replace 'your_hdfs_namenode_host' and 'your_hdfs_port' with actual values
# Example: 'http://localhost:9870' or 'http://your_namenode:50070'
client = InsecureClient('http://your_hdfs_namenode_host:your_hdfs_port', user='your_hdfs_user')
# Or, for a secure cluster with Kerberos (requires hdfs[kerberos] installed and kinit):
# from hdfs.ext.kerberos import KerberosClient
# client = KerberosClient('http://your_hdfs_namenode_host:your_hdfs_port', user='your_hdfs_user')
```
ConnectionError: HTTPConnectionPool(host='...', port=...): Max retries exceeded with url:
This error indicates that the Python `hdfs` client, which uses WebHDFS, failed to establish an HTTP connection to the specified HDFS endpoint after multiple retries. This can be caused by an incorrect host/port, network issues (e.g., firewall blocking the port), or the WebHDFS service not running on the Hadoop cluster.
fix
Check that the HDFS Namenode and DataNode services are running, the host and port are correct and reachable from your client machine, and no firewall is blocking the WebHDFS port (typically 50070 or 9870 for the Namenode UI/WebHDFS endpoint, or 50075 for DataNode). Use `curl` to test connectivity to the WebHDFS endpoint from your client machine. 
```bash
# Example: Test WebHDFS API endpoint
curl -i 'http://your_hdfs_namenode_host:your_hdfs_port/webhdfs/v1/?op=GETHOMEDIRECTORY'
```
If `curl` also fails, the issue is with network connectivity or the HDFS cluster setup. If `curl` succeeds, verify the `hdfs` client initialization parameters.
AttributeError: module 'hdfs' has no attribute 'client' OR ImportError: cannot import name 'config' from 'hdfs'
These errors usually stem from using an outdated version of the `hdfs` library or incorrect import statements based on older API versions. The `hdfs` library's structure or common usage patterns might have changed.
fix
Ensure you are using an up-to-date version of the `hdfs` library (version 2.7.3 or newer is recommended) and use the correct import pattern for `InsecureClient` or `Client`. If upgrading, remove the old version first. 
```bash
pip uninstall hdfs
pip install hdfs
```
Then, use the standard import: 
```python
from hdfs import InsecureClient
# client = InsecureClient('http://namenode_host:port', user='your_user')
# For Kerberos:
# from hdfs.ext.kerberos import KerberosClient
# client = KerberosClient('http://namenode_host:port', user='your_user')
```
Authentication failure. Check your credentials.
This `HdfsError` occurs when trying to connect to a secure (Kerberized) HDFS cluster without proper authentication credentials, such as a valid Kerberos ticket.
fix
For Kerberized clusters, ensure you have obtained a valid Kerberos ticket before running your Python application. This typically involves using the `kinit` command. Additionally, ensure the `hdfs[kerberos]` extra is installed if using Kerberos. 
```bash
kinit your_user@YOUR.REALM
# Then, in your Python code:
pip install 'hdfs[kerberos]'
from hdfs.ext.kerberos import KerberosClient
client = KerberosClient('http://your_hdfs_namenode_host:your_hdfs_port')
# The 'user' parameter is often not needed with KerberosClient as it's derived from the kinit ticket.
```
Upgrade
Version history
2.7.3latest on PyPI
Audit
Dependencies
fastavrooptionalRequired for the 'avro' extension to read and write Avro files.
pandasoptionalRequired for the 'dataframe' extension to load and save Pandas DataFrames.
requests-kerberosoptionalRequired for the 'kerberos' extension to enable Kerberos authenticated clusters.
Agent activity
19 hits · last 30 days
node
6
seranking-bot
4
ahrefsbot
3
Meta
1
Amazon
1
bytedance
1
Resources