aboutsummaryrefslogtreecommitdiffstats
path: root/please
blob: e328410bf1f0f835ec4463ea71b95b362a5bf283 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python3
"""
Helper script for running Sandcrawler (journal pipeline) tasks in production.

This is basically a Makefile. Be sure to only use python3 standard library
modules, so there are no dependencies.
"""

import sys
import argparse
import subprocess
from datetime import datetime

HDFS_DIR = "hdfs:///user/bnewbold/sandcrawler"
HBASE_HOST = "wbgrp-svc263.us.archive.org"
ZOOKEEPER_HOSTS = "mtrcs-zk1.us.archive.org:2181"
GROBID_URI = "http://wbgrp-svc096.us.archive.org:8070"

def rebuild_python():
    print("Rebuilding python venv...")
    cmd = """cd mapreduce;
        export PIPENV_VENV_IN_PROJECT=1;
        pipenv install --deploy
        tar -czf venv-current.tar.gz -C .venv ."""
    subprocess.call(cmd, shell=True)

def rebuild_scalding():
    print("Rebuilding scalding jar...")
    cmd = """cd scalding; sbt assembly"""
    subprocess.call(cmd, shell=True)

def run_backfill(args):
    if args.rebuild:
        rebuild_python()
    print("Starting backfill job...")
    output = "{}/output-{}/{}-backfill".format(
        HDFS_DIR,
        args.env,
        datetime.strftime(datetime.now(), "%Y-%m-%d-%H%M.%S"))
    cmd = """cd mapreduce;
        pipenv run ./backfill_hbase_from_cdx.py \
            --hbase-host {hbase_host} \
            --hbase-table wbgrp-journal-extract-0-{env} \
            -r hadoop \
            -c mrjob.conf \
            --archive venv-current.tar.gz#venv \
            {input_cdx}
            """.format(hbase_host=HBASE_HOST, env=args.env,
                input_cdx=args.input_cdx)
    subprocess.call(cmd, shell=True)

def run_extract(args):
    if args.rebuild:
        rebuild_python()
    print("Starting extract job...")
    output = "{}/output-{}/{}-extract".format(
        HDFS_DIR,
        args.env,
        datetime.strftime(datetime.now(), "%Y-%m-%d-%H%M.%S"))
    cmd = """cd mapreduce;
        pipenv run ./extraction_cdx_grobid.py \
            --hbase-host {hbase_host} \
            --hbase-table wbgrp-journal-extract-0-{env} \
            --grobid-uri {grobid_uri} \
            -r hadoop \
            -c mrjob.conf \
            --archive venv-current.tar.gz#venv \
            --jobconf mapred.line.input.format.linespermap=8000 \
            --jobconf mapreduce.job.queuename=extraction \
            --jobconf mapred.task.timeout=3600000 \
            {input_cdx}
            """.format(hbase_host=HBASE_HOST, env=args.env,
                input_cdx=args.input_cdx,
                grobid_uri=GROBID_URI)
    subprocess.call(cmd, shell=True)

def run_rowcount(args):
    if args.rebuild:
        rebuild_scalding()
    print("Starting rowcount job...")
    output = "{}/output-{}/{}-rowcount".format(
        HDFS_DIR,
        args.env,
        datetime.strftime(datetime.now(), "%Y-%m-%d-%H%M.%S"))
    cmd = """hadoop jar \
        scalding/target/scala-2.11/sandcrawler-assembly-0.2.0-SNAPSHOT.jar \
        com.twitter.scalding.Tool sandcrawler.HBaseRowCountJob \
        --hdfs \
        --app.conf.path scalding/ia_cluster.conf \
        --hbase-table wbgrp-journal-extract-0-{env} \
        --zookeeper-hosts {zookeeper_hosts} \
        --output {output}""".format(
            output=output,
            zookeeper_hosts=ZOOKEEPER_HOSTS,
            env=args.env)
    subprocess.call(cmd, shell=True)

def run_statuscodecount(args):
    if args.rebuild:
        rebuild_scalding()
    print("Starting statuscodecount job...")
    output = "{}/output-{}/{}-statuscodecount".format(
        HDFS_DIR,
        args.env,
        datetime.strftime(datetime.now(), "%Y-%m-%d-%H%M.%S"))
    cmd = """hadoop jar \
        scalding/target/scala-2.11/sandcrawler-assembly-0.2.0-SNAPSHOT.jar \
        com.twitter.scalding.Tool sandcrawler.HBaseStatusCodeCountJob \
        --hdfs \
        --app.conf.path scalding/ia_cluster.conf \
        --hbase-table wbgrp-journal-extract-0-{env} \
        --zookeeper-hosts {zookeeper_hosts} \
        --output {output}""".format(
            output=output,
            zookeeper_hosts=ZOOKEEPER_HOSTS,
            env=args.env)
    subprocess.call(cmd, shell=True)

def run_statuscount(args):
    if args.rebuild:
        rebuild_scalding()
    print("Starting statuscount job...")
    output = "{}/output-{}/{}-statuscount".format(
        HDFS_DIR,
        args.env,
        datetime.strftime(datetime.now(), "%Y-%m-%d-%H%M.%S"))
    cmd = """hadoop jar \
        scalding/target/scala-2.11/sandcrawler-assembly-0.2.0-SNAPSHOT.jar \
        com.twitter.scalding.Tool sandcrawler.HBaseStatusCountJob \
        --hdfs \
        --app.conf.path scalding/ia_cluster.conf \
        --hbase-table wbgrp-journal-extract-0-{env} \
        --zookeeper-hosts {zookeeper_hosts} \
        --output {output}""".format(
            output=output,
            zookeeper_hosts=ZOOKEEPER_HOSTS,
            env=args.env)
    subprocess.call(cmd, shell=True)

def run_matchcrossref(args):
    if args.rebuild:
        rebuild_scalding()
    print("Starting matchcrossref job...")
    output = "{}/output-{}/{}-matchcrossref".format(
        HDFS_DIR,
        args.env,
        datetime.strftime(datetime.now(), "%Y-%m-%d-%H%M.%S"))
    # Notes: -D options must come after Tool but before class name
    # https://github.com/twitter/scalding/wiki/Frequently-asked-questions#how-do-i-pass-parameters-to-my-hadoop-job-number-of-reducers--memory-options--etc-
    cmd = """hadoop jar \
        scalding/target/scala-2.11/sandcrawler-assembly-0.2.0-SNAPSHOT.jar \
        com.twitter.scalding.Tool \
        -Dmapred.reduce.tasks={reducers} \
        sandcrawler.ScoreJob \
        --hdfs \
        --app.conf.path scalding/ia_cluster.conf \
        --hbase-table wbgrp-journal-extract-0-{env} \
        --zookeeper-hosts {zookeeper_hosts} \
        --crossref-input {crossref_input} \
        --output {output}""".format(
            output=output,
            zookeeper_hosts=ZOOKEEPER_HOSTS,
            env=args.env,
            reducers=args.reducers,
            crossref_input=args.crossref_input)
    subprocess.call(cmd, shell=True)

def run_grobidscorabledump(args):
    if args.rebuild:
        rebuild_scalding()
    print("Starting grobid-scorable-dump job...")
    output = "{}/output-{}/{}-grobidscorabledump".format(
        HDFS_DIR,
        args.env,
        datetime.strftime(datetime.now(), "%Y-%m-%d-%H%M.%S"))
    cmd = """hadoop jar \
        scalding/target/scala-2.11/sandcrawler-assembly-0.2.0-SNAPSHOT.jar \
        com.twitter.scalding.Tool sandcrawler.GrobidScorableDumpJob \
        --hdfs \
        --app.conf.path scalding/ia_cluster.conf \
        --hbase-table wbgrp-journal-extract-0-{env} \
        --zookeeper-hosts {zookeeper_hosts} \
        --output {output}""".format(
            output=output,
            zookeeper_hosts=ZOOKEEPER_HOSTS,
            env=args.env)
    subprocess.call(cmd, shell=True)

def run_colcount(args):
    if args.rebuild:
        rebuild_scalding()
    print("Starting colcount job...")
    output = "{}/output-{}/{}-colcount-{}".format(
        HDFS_DIR,
        args.env,
        datetime.strftime(datetime.now(), "%Y-%m-%d-%H%M.%S"),
        args.column.replace(':', '_'))
    cmd = """hadoop jar \
        scalding/target/scala-2.11/sandcrawler-assembly-0.2.0-SNAPSHOT.jar \
        com.twitter.scalding.Tool sandcrawler.HBaseColCountJob \
        --hdfs \
        --app.conf.path scalding/ia_cluster.conf \
        --hbase-table wbgrp-journal-extract-0-{env} \
        --zookeeper-hosts {zookeeper_hosts} \
        --column {column} \
        --output {output}""".format(
            column=args.column,
            output=output,
            zookeeper_hosts=ZOOKEEPER_HOSTS,
            env=args.env)
    subprocess.call(cmd, shell=True)

def main():
    parser = argparse.ArgumentParser()

    parser.add_argument('--prod',
        help="run against prod HBase table",
        action='store_true')
    parser.add_argument('--qa',
        help="run against qa HBase table",
        action='store_true')
    parser.add_argument('--rebuild',
        help="rebuild whatever artifact gets sent",
        action='store_true')
    subparsers = parser.add_subparsers()

    sub_backfill = subparsers.add_parser('backfill')
    sub_backfill.set_defaults(func=run_backfill)
    sub_backfill.add_argument('input_cdx',
        help="full HDFS path of CDX file to backfill")

    sub_extract = subparsers.add_parser('extract')
    sub_extract.set_defaults(func=run_extract)
    sub_extract.add_argument('input_cdx',
        help="full HDFS path of CDX file to extract")

    sub_rowcount = subparsers.add_parser('row-count')
    sub_rowcount.set_defaults(func=run_rowcount)

    sub_statuscount = subparsers.add_parser('status-count')
    sub_statuscount.set_defaults(func=run_statuscount)

    sub_statuscodecount = subparsers.add_parser('status-code-count')
    sub_statuscodecount.set_defaults(func=run_statuscodecount)

    sub_matchcrossref = subparsers.add_parser('match-crossref')
    sub_matchcrossref.set_defaults(func=run_matchcrossref)
    sub_matchcrossref.add_argument('crossref_input',
        help="full HDFS path of Crossref JSON dump")
    sub_matchcrossref.add_argument('--reducers',
        help="number of reducers to run",
        type=int, default=30)

    sub_grobidscorabledump = subparsers.add_parser('grobid-scorable-dump')
    sub_grobidscorabledump.set_defaults(func=run_grobidscorabledump)

    sub_colcount = subparsers.add_parser('col-count')
    sub_colcount.set_defaults(func=run_colcount)
    sub_colcount.add_argument('column',
        help="column name to use in count")

    args = parser.parse_args()
    if not args.__dict__.get("func"):
        print("tell me what to do! (try --help)")
        sys.exit(-1)
    if not (args.prod or args.qa) or (args.prod and args.qa):
        print("must pass one of --prod or --qa")
    if args.prod:
        args.env = "prod"
    if args.qa:
        args.env = "qa"

    args.func(args)

if __name__ == '__main__':
    main()