การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น
Davina Moossazadeh
Data Scientist
expectation = gx.expectations.ExpectTableColumnCountToEqual(
value=10
)
suite = gx.ExpectationSuite(
name="my_suite"
)
# Add Expectation to Suite
suite.add_expectation(
expectation=expectation
)
# Create another Expectation Suite
another_suite = gx.ExpectationSuite(name="my_other_suite")
# Add the same Expectation to the new Suite
another_suite.add_expectation(expectation=expectation)
Expectation ไม่สามารถ อยู่ใน Suite หลายอันพร้อมกันได้:
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`.
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`.
คัดลอก Expectation ตั้งค่า .id เป็น None แล้วเพิ่มลงใน Suite ใหม่ได้เลยโดยไม่เกิดข้อผิดพลาด:
expectation_copy = expectation.copy()expectation_copy.id = None
another_suite.add_expectation(
expectation=expectation_copy
)
print(
expectation_copy in another_suite.expectations
)
True
เพิ่ม
.add_expectation()
suite.add_expectation(
expectation=expectation
)
ลบ
.delete_expectation()
suite.delete_expectation(
expectation=expectation
)
อัปเดต attribute .value แล้วบันทึกการเปลี่ยนแปลง:
expectation = gx.expectations.ExpectTableColumnCountToEqual( value=10 )expectation.value = 11expectation.save()
ต้องให้ Expectation อยู่ใน Suite ก่อน มิฉะนั้นจะเกิดข้อผิดพลาด:
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"
)
# Define Expectation col_name_expectation = gx.expectations.ExpectColumnToExist(column="GHI")# Add Expectation to Suite suite.add_expectation(expectation=col_name_expectation)# Run Validation Definition associated with the Suite validation_results = validation_definition.run()
บันทึก Suite ก่อนรัน Validation Definition เพื่อหลีกเลี่ยงข้อผิดพลาด:
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.
ใช้เมธอด .save() เพื่อบันทึก Suite แล้วรัน Validation Definition ได้โดยไม่มีข้อผิดพลาด:
suite.save()validation_results = validation_definition.run()print(validation_results.success)
False
คัดลอก Expectation:
expectation_copy = expectation.copy()
expectation_copy.id = None
ตรวจสอบว่า Expectation อยู่ใน Suite หรือไม่:
expectation in suite.expectations
ลบ Expectation:
suite.delete_expectation(expectation)
อัปเดตค่า Expectation:
expectation.value = new_value
บันทึกการเปลี่ยนแปลงของ Expectation:
expectation.save()
บันทึกการเปลี่ยนแปลงของ Expectation Suite:
suite.save()
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น