|
| 1 | +# iterate over monthly crawls and store |
| 2 | +# the joined data as a partition of the result table |
| 3 | + |
| 4 | +import logging |
| 5 | +import re |
| 6 | +import sys |
| 7 | + |
| 8 | +from pyathena import connect |
| 9 | + |
| 10 | +logging.basicConfig( |
| 11 | + level="INFO", format="%(asctime)s %(levelname)s %(name)s: %(message)s" |
| 12 | +) |
| 13 | + |
| 14 | +join_template = """ |
| 15 | +CREATE TABLE {db}._tmp_overlap |
| 16 | +WITH (external_location = '{s3_location}/crawl={crawl}/', |
| 17 | + partitioned_by = ARRAY['subset'], |
| 18 | + format = 'PARQUET', |
| 19 | + parquet_compression = 'GZIP') |
| 20 | +AS SELECT |
| 21 | + cc.url_surtkey AS url_surtkey, |
| 22 | + cc.url_host_tld AS url_host_tld, |
| 23 | + cc.url_host_registered_domain AS url_host_registered_domain, |
| 24 | + cc.url_host_name AS url_host_name, |
| 25 | + cc.url AS url, |
| 26 | + cc.fetch_status AS fetch_status, |
| 27 | + cc.fetch_time AS fetch_time, |
| 28 | + cc.warc_filename AS warc_filename, |
| 29 | + cc.warc_record_offset AS warc_record_offset, |
| 30 | + cc.warc_record_length AS warc_record_length, |
| 31 | + cc.fetch_redirect AS fetch_redirect, |
| 32 | + cc.content_mime_detected AS content_mime_detected, |
| 33 | + cc.content_languages AS content_languages, |
| 34 | + cc.subset AS subset |
| 35 | +FROM ccindex.ccindex AS cc |
| 36 | + RIGHT OUTER JOIN {db}.{url_table} AS {tid} |
| 37 | + ON cc.url = {tid}.url |
| 38 | +WHERE cc.crawl = '{crawl}' |
| 39 | +""" |
| 40 | + |
| 41 | +drop_tmp_table = "DROP TABLE `{db}._tmp_overlap`;" |
| 42 | + |
| 43 | +# list of crawls |
| 44 | +# Note: in order to get a list of released crawls: |
| 45 | +# - query Athena |
| 46 | +# SHOW PARTITIONS ccindex |
| 47 | +# - see |
| 48 | +# https://commoncrawl.s3.amazonaws.com/crawl-data/index.html |
| 49 | +crawls = [ |
| 50 | + "CC-MAIN-2013-20", |
| 51 | + "CC-MAIN-2013-48", |
| 52 | + # |
| 53 | + "CC-MAIN-2014-10", |
| 54 | + "CC-MAIN-2014-15", |
| 55 | + "CC-MAIN-2014-23", |
| 56 | + "CC-MAIN-2014-35", |
| 57 | + "CC-MAIN-2014-41", |
| 58 | + "CC-MAIN-2014-42", |
| 59 | + "CC-MAIN-2014-49", |
| 60 | + "CC-MAIN-2014-52", |
| 61 | + # |
| 62 | + "CC-MAIN-2015-06", |
| 63 | + "CC-MAIN-2015-11", |
| 64 | + "CC-MAIN-2015-14", |
| 65 | + "CC-MAIN-2015-18", |
| 66 | + "CC-MAIN-2015-22", |
| 67 | + "CC-MAIN-2015-27", |
| 68 | + "CC-MAIN-2015-32", |
| 69 | + "CC-MAIN-2015-35", |
| 70 | + "CC-MAIN-2015-40", |
| 71 | + "CC-MAIN-2015-48", |
| 72 | + # |
| 73 | + "CC-MAIN-2016-07", |
| 74 | + "CC-MAIN-2016-18", |
| 75 | + "CC-MAIN-2016-22", |
| 76 | + "CC-MAIN-2016-26", |
| 77 | + "CC-MAIN-2016-30", |
| 78 | + "CC-MAIN-2016-36", |
| 79 | + "CC-MAIN-2016-40", |
| 80 | + "CC-MAIN-2016-44", |
| 81 | + "CC-MAIN-2016-50", |
| 82 | + # |
| 83 | + "CC-MAIN-2017-04", |
| 84 | + "CC-MAIN-2017-09", |
| 85 | + "CC-MAIN-2017-13", |
| 86 | + "CC-MAIN-2017-17", |
| 87 | + "CC-MAIN-2017-22", |
| 88 | + "CC-MAIN-2017-26", |
| 89 | + "CC-MAIN-2017-30", |
| 90 | + "CC-MAIN-2017-34", |
| 91 | + "CC-MAIN-2017-39", |
| 92 | + "CC-MAIN-2017-43", |
| 93 | + "CC-MAIN-2017-47", |
| 94 | + "CC-MAIN-2017-51", |
| 95 | + # |
| 96 | + "CC-MAIN-2018-05", |
| 97 | + "CC-MAIN-2018-09", |
| 98 | + "CC-MAIN-2018-13", |
| 99 | + "CC-MAIN-2018-17", |
| 100 | + "CC-MAIN-2018-22", |
| 101 | + "CC-MAIN-2018-26", |
| 102 | + "CC-MAIN-2018-30", |
| 103 | + "CC-MAIN-2018-34", |
| 104 | + "CC-MAIN-2018-39", |
| 105 | + "CC-MAIN-2018-43", |
| 106 | + "CC-MAIN-2018-47", |
| 107 | + "CC-MAIN-2018-51", |
| 108 | + # |
| 109 | + "CC-MAIN-2019-04", |
| 110 | + "CC-MAIN-2019-09", |
| 111 | + "CC-MAIN-2019-13", |
| 112 | + "CC-MAIN-2019-18", |
| 113 | + "CC-MAIN-2019-22", |
| 114 | + "CC-MAIN-2019-26", |
| 115 | + "CC-MAIN-2019-30", |
| 116 | + "CC-MAIN-2019-35", |
| 117 | + "CC-MAIN-2019-39", |
| 118 | + "CC-MAIN-2019-43", |
| 119 | + "CC-MAIN-2019-47", |
| 120 | + "CC-MAIN-2019-51", |
| 121 | + # |
| 122 | + "CC-MAIN-2020-05", |
| 123 | + "CC-MAIN-2020-10", |
| 124 | + "CC-MAIN-2020-16", |
| 125 | + "CC-MAIN-2020-24", |
| 126 | + "CC-MAIN-2020-29", |
| 127 | + "CC-MAIN-2020-34", |
| 128 | + "CC-MAIN-2020-40", |
| 129 | + "CC-MAIN-2020-45", |
| 130 | + "CC-MAIN-2020-50", |
| 131 | + # |
| 132 | + "CC-MAIN-2021-04", |
| 133 | + "CC-MAIN-2021-10", |
| 134 | + "CC-MAIN-2021-17", |
| 135 | + "CC-MAIN-2021-21", |
| 136 | + "CC-MAIN-2021-25", |
| 137 | + "CC-MAIN-2021-31", |
| 138 | + "CC-MAIN-2021-39", |
| 139 | + "CC-MAIN-2021-43", |
| 140 | + "CC-MAIN-2021-49", |
| 141 | + # |
| 142 | +] |
| 143 | + |
| 144 | + |
| 145 | +s3_location = sys.argv[1] |
| 146 | +s3_location = s3_location.rstrip("/") # no trailing slash! |
| 147 | + |
| 148 | +url_table = sys.argv[2] |
| 149 | + |
| 150 | +crawl_selector = re.compile(sys.argv[3], re.IGNORECASE) |
| 151 | + |
| 152 | + |
| 153 | +crawls = filter(lambda c: crawl_selector.match(c), crawls) |
| 154 | + |
| 155 | + |
| 156 | +cursor = connect( |
| 157 | + s3_staging_dir="{}/staging".format(s3_location), region_name="us-east-1" |
| 158 | +).cursor() |
| 159 | + |
| 160 | +for crawl in crawls: |
| 161 | + query = join_template.format( |
| 162 | + crawl=crawl, |
| 163 | + s3_location=f"{s3_location}/cc-{url_table}", |
| 164 | + db="bigscience", |
| 165 | + url_table=url_table, |
| 166 | + tid="bs", |
| 167 | + ) |
| 168 | + logging.info("Athena query: %s", query) |
| 169 | + |
| 170 | + cursor.execute(query) |
| 171 | + logging.info("Athena query ID %s: %s", cursor.query_id, cursor.result_set.state) |
| 172 | + logging.info( |
| 173 | + " data_scanned_in_bytes: %d", cursor.result_set.data_scanned_in_bytes |
| 174 | + ) |
| 175 | + logging.info( |
| 176 | + " total_execution_time_in_millis: %d", |
| 177 | + cursor.result_set.total_execution_time_in_millis, |
| 178 | + ) |
| 179 | + |
| 180 | + cursor.execute(drop_tmp_table.format(db="bigscience")) |
| 181 | + logging.info("Drop temporary table: %s", cursor.result_set.state) |
0 commit comments