Nhập môn Data Quality với Great Expectations
Davina Moossazadeh
Data Scientist
expectation = gx.expectations.ExpectTableColumnCountToEqual(
value=10
)
suite = gx.ExpectationSuite(
name="my_suite"
)
# Thêm Expectation vào Suite
suite.add_expectation(
expectation=expectation
)
# Tạo một Expectation Suite khác
another_suite = gx.ExpectationSuite(name="my_other_suite")
# Thêm cùng Expectation vào Suite mới
another_suite.add_expectation(expectation=expectation)
Expectations không thể thuộc nhiều Suite cùng lúc:
RuntimeError: Cannot add Expectation because it already belongs to an
ExpectationSuite. If you want to update an existing Expectation, please call
Expectation.save(). If you are copying this Expectation to a new ExpectationSuite,
please copy it first (the core expectations and some others support
copy(expectation)) and set `Expectation.id = None`.
DNT_CURLLY_TAG_2
If you are copying this Expectation to a new ExpectationSuite, please copy it first
(the core expectations and some others support copy(expectation)) and set
`Expectation.id = None`.
Sao chép Expectation, đặt .id là None, rồi thêm vào Suite mới mà không lỗi:
expectation_copy = expectation.copy()expectation_copy.id = None
another_suite.add_expectation(
expectation=expectation_copy
)
print(
expectation_copy in another_suite.expectations
)
True
Thêm
.add_expectation()
suite.add_expectation(
expectation=expectation
)
Xóa
.delete_expectation()
suite.delete_expectation(
expectation=expectation
)
Cập nhật thuộc tính .value và lưu thay đổi:
expectation = gx.expectations.ExpectTableColumnCountToEqual( value=10 )expectation.value = 11expectation.save()
Đảm bảo Expectation thuộc một Suite, nếu không sẽ gặp lỗi:
RuntimeError: Expectation must be added to ExpectationSuite before it can be saved.
suite = gx.ExpectationSuite(name="my_suite")
validation_definition = gx.ValidationDefinition(
data=batch_definition, suite=suite, name="my_validation_definition"
)
# Định nghĩa Expectation col_name_expectation = gx.expectations.ExpectColumnToExist(column="GHI")# Thêm Expectation vào Suite suite.add_expectation(expectation=col_name_expectation)# Chạy Validation Definition gắn với Suite validation_results = validation_definition.run()
Lưu thay đổi ở Suite trước khi chạy Validation Definition để tránh lỗi:
validation_results = validation_definition.run()
ResourceFreshnessAggregateError: ExpectationSuite 'my_suite' has changed since it
has last been saved. Please update with `<SUITE_OBJECT>.save()`, then try your
action again.
Dùng phương thức .save() để lưu Suite và chạy Validation Definition không lỗi:
suite.save()validation_results = validation_definition.run()print(validation_results.success)
False
Sao chép Expectation:
expectation_copy = expectation.copy()
expectation_copy.id = None
Kiểm tra Expectation có trong Suite không:
expectation in suite.expectations
Xóa Expectation:
suite.delete_expectation(expectation)
Cập nhật giá trị Expectation:
expectation.value = new_value
Lưu thay đổi của Expectation:
expectation.save()
Lưu thay đổi của Expectation Suite:
suite.save()
Nhập môn Data Quality với Great Expectations