Created
April 30, 2013 06:03
-
-
Save Stiivi/5486843 to your computer and use it in GitHub Desktop.
Brewery2 Example: using kernel function added_keys and added_rows. The added_rows will be called with two flavours: one as (sql, sql) the other will be retried with (rows, sql).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from brewery2 import k, open_store, FieldList | |
DATA_TARGET = [ | |
[1, "Janko", "Bratislava"], | |
[2, "Marienka", "Bratislava"], | |
[3, "Jaga", "Zvolen"] | |
] | |
DATA_SRC = [ | |
[1, "Janko", "Bratislava"], | |
[2, "Marienka", "Bratislava"], | |
[3, "Jaga", "Bratislava"], | |
[4, "Vlk", "Zvolen"] | |
] | |
# Open database store | |
store = open_store("sql", "sqlite:///") | |
# Create some example tables ... | |
src = store.create("src_customers", FieldList(("id", "integer"), | |
("name", "string"), | |
("city", "string"))) | |
src.append_from_iterable(DATA_SRC) | |
target = store.create("dim_customers", FieldList(("id", "integer"), | |
("name", "string"), | |
("city", "string"))) | |
target.append_from_iterable(DATA_TARGET) | |
# Some fun stuff | |
def try_added_keys(): | |
"""Show all added keys""" | |
diff = k.added_keys(src,target,"id") | |
print("difference:") | |
for row in diff: | |
print(row) | |
def try_added_rows_composable(): | |
"""Show all added rows using one nested SQL statement - because we can compose within one store""" | |
diff = k.added_rows(src,target,"id") | |
print("difference:") | |
for row in diff: | |
print(row) | |
def try_added_rows_noncomposable(): | |
"""Show all added rows using an iterator - we can not compose, because data are in two stores""" | |
another_store = open_store("sql", "sqlite:///") | |
target = another_store.create("dim_customers", FieldList(("id", "integer"), | |
("name", "string"), | |
("city", "string"))) | |
target.append_from_iterable(DATA_TARGET) | |
diff = k.added_rows(src,target,"id") | |
print("difference:") | |
for row in diff: | |
print(row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment