as10128 commented on issue #751:
URL: https://github.com/apache/iceberg-python/issues/751#issuecomment-2123742680
@ndrluis I wrote a test code, I used minio and hive catalog, you need to
modify the aws_access_key, aws_secret_key, s3_endpoint, hive_thrift_uri,
warehouse_uri parameters.The results are annotated in the code.
Python 3.10.14
MacOS x86
```
from pyiceberg.catalog import load_catalog
from pyiceberg.schema import Schema
from pyiceberg.types import (
DecimalType,
LongType,
NestedField,
)
from pyiceberg.exceptions import NoSuchTableError
import pyarrow as pa
from decimal import Decimal
aws_access_key = "aws_access_key"
aws_secret_key = "aws_secret_key"
s3_endpoint = "http://s3_ip:9000"
hive_thrift_uri = "thrift://hive_metastore_ip:9083"
warehouse_uri = "s3a://xxxx/xxxx"
catalog = load_catalog("default", **{
"uri": f"{hive_thrift_uri}",
"s3.endpoint": f"{s3_endpoint}",
"py-io-impl": "pyiceberg.io.pyarrow.PyArrowFileIO",
"s3.access-key-id": f"{aws_access_key}",
"s3.secret-access-key": f"{aws_secret_key}",
})
try:
catalog.drop_table(identifier='cw.decimal_type_table')
except NoSuchTableError:
pass
data = pa.Table.from_pylist(
[
{"intertype": 1, "decimaltype1": Decimal(52.30), "decimaltype2":
Decimal(52.30)},
{"intertype": 2, "decimaltype1": Decimal(52.30), "decimaltype2":
Decimal(52.30)},
{"intertype": 3, "decimaltype1": Decimal(52.30), "decimaltype2":
Decimal(52.30)},
],
)
arrow_schema = pa.schema(
[
pa.field("intertype", pa.int64(), nullable=False),
pa.field("decimaltype1", pa.decimal128(32, 16), nullable=True),
pa.field("decimaltype2", pa.decimal128(32, 2), nullable=True),
]
)
iceberg_schema = Schema(
NestedField(field_id=1, name="intertype", field_type=LongType(),
required=True),
NestedField(field_id=2, name="decimaltype1", field_type=DecimalType(32,
16), required=False),
NestedField(field_id=3, name="decimaltype2", field_type=DecimalType(32,
2), required=False),
identifier_field_ids=[1]
)
if __name__ == '__main__':
create_table_instance =
catalog.create_table(identifier='cw.decimal_type_table', schema=iceberg_schema)
print(create_table_instance)
# decimal_type_table(
# 1: intertype: required long,
# 2: decimaltype1: optional decimal(32, 16),
# 3: decimaltype2: optional decimal(32, 2)
# ),
# partition by: [],
# sort order: [],
# snapshot: null
assert (create_table_instance.schema().columns[1] ==
NestedField(field_id=2, name="decimaltype1",
field_type=DecimalType(32, 16), required=False))
# insert data
df = pa.Table.from_pylist(data, schema=arrow_schema)
create_table_instance.append(df)
load_table_instance = catalog.load_table('cw.decimal_type_table')
print(load_table_instance)
# decimal_type_table(
# 1: intertype: required long,
# 2: decimaltype1: optional decimal(32, 2),
# 3: decimaltype2: optional decimal(32, 2)
# ),
# partition by: [],
# sort order: [],
# snapshot: Operation.APPEND: id = 4966508564674566147, schema_id = 0
assert (load_table_instance.schema().columns[1] ==
NestedField(field_id=2, name="decimaltype1",
field_type=DecimalType(32, 16), required=False))
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]