Mercurial > repos > bcclaywell > argo_navis
comparison venv/bin/dynamodb_dump @ 0:d67268158946 draft
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
author | bcclaywell |
---|---|
date | Mon, 12 Oct 2015 17:43:33 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d67268158946 |
---|---|
1 #!/Users/bclaywel/workspace/argo-navis/venv/bin/python2.7 | |
2 | |
3 import argparse | |
4 import errno | |
5 import os | |
6 | |
7 import boto | |
8 from boto.compat import json | |
9 | |
10 | |
11 DESCRIPTION = """Dump the contents of one or more DynamoDB tables to the local filesystem. | |
12 | |
13 Each table is dumped into two files: | |
14 - {table_name}.metadata stores the table's name, schema and provisioned | |
15 throughput. | |
16 - {table_name}.data stores the table's actual contents. | |
17 | |
18 Both files are created in the current directory. To write them somewhere else, | |
19 use the --out-dir parameter (the target directory will be created if needed). | |
20 """ | |
21 | |
22 | |
23 def dump_table(table, out_dir): | |
24 metadata_file = os.path.join(out_dir, "%s.metadata" % table.name) | |
25 data_file = os.path.join(out_dir, "%s.data" % table.name) | |
26 | |
27 with open(metadata_file, "w") as metadata_fd: | |
28 json.dump( | |
29 { | |
30 "name": table.name, | |
31 "schema": table.schema.dict, | |
32 "read_units": table.read_units, | |
33 "write_units": table.write_units, | |
34 }, | |
35 metadata_fd | |
36 ) | |
37 | |
38 with open(data_file, "w") as data_fd: | |
39 for item in table.scan(): | |
40 # JSON can't serialize sets -- convert those to lists. | |
41 data = {} | |
42 for k, v in item.iteritems(): | |
43 if isinstance(v, (set, frozenset)): | |
44 data[k] = list(v) | |
45 else: | |
46 data[k] = v | |
47 | |
48 data_fd.write(json.dumps(data)) | |
49 data_fd.write("\n") | |
50 | |
51 | |
52 def dynamodb_dump(tables, out_dir): | |
53 try: | |
54 os.makedirs(out_dir) | |
55 except OSError as e: | |
56 # We don't care if the dir already exists. | |
57 if e.errno != errno.EEXIST: | |
58 raise | |
59 | |
60 conn = boto.connect_dynamodb() | |
61 for t in tables: | |
62 dump_table(conn.get_table(t), out_dir) | |
63 | |
64 | |
65 if __name__ == "__main__": | |
66 parser = argparse.ArgumentParser( | |
67 prog="dynamodb_dump", | |
68 description=DESCRIPTION | |
69 ) | |
70 parser.add_argument("--out-dir", default=".") | |
71 parser.add_argument("tables", metavar="TABLES", nargs="+") | |
72 | |
73 namespace = parser.parse_args() | |
74 | |
75 dynamodb_dump(namespace.tables, namespace.out_dir) |