hmsclient
A Python Thrift client that wraps the Hive Metastore RPC service in an ergonomic API.
Repository Health
Technical Analysis
hmsclient is a Python client for the Apache Hive Metastore, built directly on top of Thrift-generated stubs for the ThriftHiveMetastore service. Rather than requiring callers to hand-construct Thrift structs and manage socket transports themselves, it wraps the generated client in a single HMSClient class with context-manager semantics (with client as c:) and a handful of convenience helpers for common metastore operations.
The package bundles pre-generated Python bindings for both the hive_metastore and fb303 Thrift services, along with a generate.py script that can re-run the Thrift compiler against newer .thrift IDL files when the upstream Hive Metastore protocol changes. This makes it a thin, purpose-built adapter for teams that need to query or mutate Hive Metastore state (databases, tables, partitions) from Python without writing raw Thrift RPC code by hand.
What You Get
- An
HMSClientclass that extends the generatedThriftHiveMetastore.Clientwithopen()/close()/context-manager support so aTSocket/TBufferedTransport/TBinaryProtocolconnection is set up and torn down automatically - Partition helpers —
make_partition()derives aPartitionobject’s storage descriptor from its parent table, andadd_partition()/drop_partitions()/drop_all_partitions()wrap the raw Thrift calls for common partition lifecycle operations - Schema helpers —
make_schema()builds a list ofFieldSchemaobjects from shortname:typestrings, andparse_schema()convertsFieldSchemaobjects back into readablename\ttypestrings check_for_named_partition()turns the metastore’sNoSuchObjectExceptioninto a plain boolean existence check, avoiding a manual try/except at every call site- Pre-generated Python bindings for the full
hive_metastoreandfb303Thrift services (types, exceptions, and RPC client classes), plusgenerate.pyto regenerate them from newer upstream.thriftfiles - Configuration via
HMS_HOST/HMS_PORTenvironment variables or explicit constructor arguments, defaulting tolocalhost:9083
Common Use Cases
- Scripting Hive Metastore partition maintenance (adding, checking, or dropping partitions) from Python jobs instead of shelling out to
hiveorbeeline - Building internal tooling or Airflow/orchestration tasks that need to read or write Hive table and partition metadata programmatically
- Polling the metastore’s notification event log (
get_current_notification_id()) to track schema or partition changes over time - Regenerating the Thrift client bindings against a newer Hive Metastore IDL version when upgrading a Hive cluster
Under The Hood
Architecture
The package centers on a single HMSClient class (hmsclient/hmsclient.py) that subclasses the Thrift-generated ThriftHiveMetastore.Client and layers on context-manager semantics plus a handful of convenience methods — make_schema, parse_schema, make_partition, add_partition, check_for_named_partition, drop_partitions, drop_all_partitions, and get_current_notification_id. The hmsclient/genthrift/ package holds the raw Thrift-compiler output for both the hive_metastore and fb303 services (types, exceptions, and RPC client classes), regenerable via the root-level generate.py script against newer .thrift IDL files. It’s a flat, single-purpose wrapper with no plugin system or dependency injection — data flow is a direct translation of Python method calls into Thrift RPC calls over a TBufferedTransport/TSocket pair, so any change to the upstream Hive Metastore Thrift schema would need to propagate through the generated stubs and directly affect the wrapper’s assumptions.
Tech Stack
Pure Python 3 (targeting 3.5/3.6 per its classifiers), packaged with a classic setup.py/setuptools layout rather than pyproject.toml. Runtime dependencies are thrift (the Apache Thrift Python bindings used for the RPC transport and protocol layers) and click (used by the standalone generate.py CLI, not by the client library itself). There’s no web framework, ORM, or database driver involved — communication happens directly over a raw socket to the Hive Metastore server (default port 9083) using TBinaryProtocol. Regeneration of the bundled Thrift stubs is a separate, manually-invoked process that fetches hive_metastore.thrift and fb303.thrift from upstream and re-runs the Thrift compiler.
Code Quality
No test files exist anywhere in the repository, and there is no CI configuration (no GitHub Actions workflow, no .travis.yml, no tox.ini). Naming follows conventional Python snake_case, and several public methods carry Sphinx-style docstrings with :param/:type/:rtype annotations, but there are no type hints and no linter or formatter configuration. Error handling is minimal and selective — check_for_named_partition catches the specific NoSuchObjectException from the generated Thrift types to produce a boolean, while other methods simply propagate whatever exceptions the underlying Thrift layer raises.
What Makes It Unique
The library’s value is a narrow but real convenience layer over an auto-generated RPC client: a context manager that opens and closes the socket transport automatically, plus helpers like make_schema (short name:type strings instead of hand-built FieldSchema objects) and make_partition (deriving a partition’s storage descriptor from its parent table) that remove boilerplate teams would otherwise write by hand against the raw generated Thrift client. It doesn’t introduce a novel technical approach — it’s a standard adapter pattern applied to one specific external service’s RPC interface.