Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DAE-117] Add method to get partitions with name and type #53

Merged
merged 2 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions docs/source/getstarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ Click on the following links to open the [examples](https://github.com/quintoand

**[#6 Get partition keys objects from a table](https://github.com/quintoandar/hive-metastore-client/blob/main/examples/get_partition_keys_objects.py)**

**[#7 Get partition keys names from a table](https://github.com/quintoandar/hive-metastore-client/blob/main/examples/get_partition_keys_names.py)**
**[#7 Get partition keys (names & types) from a table](https://github.com/quintoandar/hive-metastore-client/blob/main/examples/get_partition_keys.py)**

**[#8 Bulk drop partitions values from a table](https://github.com/quintoandar/hive-metastore-client/blob/main/examples/bulk_drop_partitions.py)**
**[#8 Get partition keys (names only) from a table](https://github.com/quintoandar/hive-metastore-client/blob/main/examples/get_partition_keys_names.py)**

**[#9 Get partition values from a table](https://github.com/quintoandar/hive-metastore-client/blob/main/examples/get_partition_values_from_table.py)**
**[#9 Bulk drop partitions values from a table](https://github.com/quintoandar/hive-metastore-client/blob/main/examples/bulk_drop_partitions.py)**

**[#10 Get partition values from a table](https://github.com/quintoandar/hive-metastore-client/blob/main/examples/get_partition_values_from_table.py)**

## Available methods

Expand All @@ -51,6 +53,7 @@ the [`HiveMetastoreClient`](https://github.com/quintoandar/hive-metastore-client
- [`create_database_if_not_exists`](https://hive-metastore-client.readthedocs.io/en/latest/hive_metastore_client.html#hive_metastore_client.hive_metastore_client.HiveMetastoreClient.create_database_if_not_exists)
- [`create_external_table`](https://hive-metastore-client.readthedocs.io/en/latest/hive_metastore_client.html#hive_metastore_client.hive_metastore_client.HiveMetastoreClient.create_external_table)
- [`get_partition_keys_objects`](https://hive-metastore-client.readthedocs.io/en/latest/hive_metastore_client.html#hive_metastore_client.hive_metastore_client.HiveMetastoreClient.get_partition_keys_objects)
- [`get_partition_keys`](https://hive-metastore-client.readthedocs.io/en/latest/hive_metastore_client.html#hive_metastore_client.hive_metastore_client.HiveMetastoreClient.get_partition_keys)
- [`get_partition_keys_names`](https://hive-metastore-client.readthedocs.io/en/latest/hive_metastore_client.html#hive_metastore_client.hive_metastore_client.HiveMetastoreClient.get_partition_keys_names)
- [`bulk_drop_partitions`](https://hive-metastore-client.readthedocs.io/en/latest/hive_metastore_client.html#hive_metastore_client.hive_metastore_client.HiveMetastoreClient.bulk_drop_partitions)
- [`get_partition_values_from_table`](https://hive-metastore-client.readthedocs.io/en/latest/hive_metastore_client.html#hive_metastore_client.hive_metastore_client.HiveMetastoreClient.get_partition_values_from_table)
- [`get_partition_values_from_table`](https://hive-metastore-client.readthedocs.io/en/latest/hive_metastore_client.html#hive_metastore_client.hive_metastore_client.HiveMetastoreClient.get_partition_values_from_table)
11 changes: 11 additions & 0 deletions examples/get_partition_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from hive_metastore_client import HiveMetastoreClient

HIVE_HOST = "<ADD_HIVE_HOST_HERE>"
HIVE_PORT = 9083

DATABASE_NAME = "database_name"
TABLE_NAME = "table_name"

with HiveMetastoreClient(HIVE_HOST, HIVE_PORT) as hive_client:
# Retrieving the partition keys and types via table schema
returned_value = hive_client.get_partition_keys(DATABASE_NAME, TABLE_NAME)
2 changes: 1 addition & 1 deletion examples/get_partition_keys_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

with HiveMetastoreClient(HIVE_HOST, HIVE_PORT) as hive_client:
# Retrieving the partition keys names via table schema
hive_client.get_partition_keys_names(DATABASE_NAME, TABLE_NAME)
returned_value = hive_client.get_partition_keys_names(DATABASE_NAME, TABLE_NAME)
2 changes: 1 addition & 1 deletion examples/get_partition_keys_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

with HiveMetastoreClient(HIVE_HOST, HIVE_PORT) as hive_client:
# Retrieving the partition keys via table schema
hive_client.get_partition_keys_objects(DATABASE_NAME, TABLE_NAME)
returned_value = hive_client.get_partition_keys_objects(DATABASE_NAME, TABLE_NAME)
2 changes: 1 addition & 1 deletion examples/get_partition_values_from_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

with HiveMetastoreClient(HIVE_HOST, HIVE_PORT) as hive_client:
# Getting partition values as a list from specified table
return_value = hive_client.get_partition_values_from_table(
returned_value = hive_client.get_partition_values_from_table(
DATABASE_NAME, TABLE_NAME
)
19 changes: 18 additions & 1 deletion hive_metastore_client/hive_metastore_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Hive Metastore Client main class."""
import copy
from typing import List, Any
from typing import List, Any, Tuple

from thrift.protocol import TBinaryProtocol
from thrift.transport import TSocket, TTransport
Expand Down Expand Up @@ -262,6 +262,23 @@ def get_partition_keys_names(self, db_name: str, table_name: str) -> List[str]:
)
return [partition.name for partition in partition_keys]

def get_partition_keys(
self, db_name: str, table_name: str
) -> List[Tuple[str, str]]:
"""
Gets the partition keys from a table as a tuple: (name, type).

An empty list will be returned when no table is found or
when the table has no partitions.

:param db_name: database name where the table is at
:param table_name: table name which the partition keys belong to
"""
partition_keys = self.get_partition_keys_objects(
db_name=db_name, table_name=table_name
)
return [(partition.name, partition.type) for partition in partition_keys]

def bulk_drop_partitions(
self,
db_name: str,
Expand Down
50 changes: 50 additions & 0 deletions tests/unit/hive_metastore_client/test_hive_metastore_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,53 @@ def test_get_partition_values_from_table_with_partitioned_table(
mocked_get_partition_values.assert_called_once_with(
expected_partition_values_request
)

@mock.patch.object(
HiveMetastoreClient, "get_partition_keys_objects", return_value=[]
)
def test_get_partition_keys_with_partitioned_table(
self, mocked_get_partition_keys_objects, hive_metastore_client
):
# arrange
db_name = "<db_name>"
table_name = "<table_name>"

partition_1 = Mock()
partition_1.name = "name_1"
partition_1.type = "type_1"

partition_2 = Mock()
partition_2.name = "name_2"
partition_2.type = "type_2"

mocked_get_partition_keys_objects.return_value = [partition_1, partition_2]
expected_value = [("name_1", "type_1"), ("name_2", "type_2")]
# act
returned_value = hive_metastore_client.get_partition_keys(db_name, table_name)

# assert
assert returned_value == expected_value
mocked_get_partition_keys_objects.assert_called_once_with(
db_name=db_name, table_name=table_name
)

@mock.patch.object(
HiveMetastoreClient, "get_partition_keys_objects", return_value=[]
)
def test_get_partition_keys_with_non_partitioned_table(
self, mocked_get_partition_keys_objects, hive_metastore_client
):
# arrange
db_name = "<db_name>"
table_name = "<table_name>"

mocked_get_partition_keys_objects.return_value = []
expected_value = []
# act
returned_value = hive_metastore_client.get_partition_keys(db_name, table_name)

# assert
assert returned_value == expected_value
mocked_get_partition_keys_objects.assert_called_once_with(
db_name=db_name, table_name=table_name
)