Last active
December 9, 2020 19:52
-
-
Save cvalaas/9a1c0b8b3a0fa28551e19b25595de368 to your computer and use it in GitHub Desktop.
sample spanner code
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
spanner_client = spanner.Client() | |
instance = spanner_client.instance("tfgen-spanid-20201027205143024") # <--spanner instance ID here. this is stage's. this should be a variable | |
database = instance.database("workday") # <-- this should probably also be a variable | |
TABLE_NAME = "workers" | |
############################# | |
# this part should be in a separate function, probably | |
import csv | |
workday_fieldnames = "" | |
worker_data = [] | |
with open('workday_infile.csv', newline='') as csvfile: | |
workday_workers_csv = csv.DictReader(csvfile) | |
workday_fieldnames = workday_workers_csv.fieldnames | |
for row in reader: | |
worker_data.append(row) | |
############################## | |
all_workers = spanner.KeySet(all_=True) | |
with database.batch() as batch: | |
batch.delete(TABLE_NAME, all_workers) | |
batch.insert( | |
table=TABLE_NAME, | |
columns=tuple(workday_fieldnames), | |
values=worker_data, | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment