https://github.com/commoncrawl/cc-pyspark

Process Common Crawl data with Python and Spark

https://github.com/commoncrawl/cc-pyspark

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.2%) to scientific vocabulary

Keywords

common-crawl commoncrawl pyspark spark sparksql warc-files wat-files wet
Last synced: 5 months ago · JSON representation

Repository

Process Common Crawl data with Python and Spark

Basic Info
  • Host: GitHub
  • Owner: commoncrawl
  • License: mit
  • Language: Python
  • Default Branch: main
  • Homepage:
  • Size: 169 KB
Statistics
  • Stars: 432
  • Watchers: 21
  • Forks: 89
  • Open Issues: 5
  • Releases: 0
Topics
common-crawl commoncrawl pyspark spark sparksql warc-files wat-files wet
Created almost 9 years ago · Last pushed 9 months ago
Metadata Files
Readme License

README.md

Common Crawl Logo

Common Crawl PySpark Examples

This project provides examples how to process the Common Crawl dataset with Apache Spark and Python:

Further information about the examples and available options is shown via the command-line option --help.

Implementing a Custom Extractor

Extending the CCSparkJob isn't difficult and for many use cases it is sufficient to override a single method (process_record). Have a look at one of the examples, e.g. to count HTML tags.

Setup

To develop and test locally, you will need to install * Spark, see the detailed instructions, and * all required Python modules by running pip install -r requirements.txt * (optionally, and only if you want to query the columnar index) install S3 support libraries so that Spark can load the columnar index from S3

Compatibility and Requirements

Tested with with Spark 3.2.3, 3.3.2, 3.4.1, 3.5.5 in combination with Python 3.8, 3.9, 3.10, 3.12 and 3.13. See the branch python-2.7 if you want to run the job on Python 2.7 and older Spark versions.

Get Sample Data

To develop locally, you'll need at least three data files – one for each format used in at least one of the examples. They can be fetched from the following links: * warc.gz * wat.gz * wet.gz

Alternatively, running get-data.sh downloads the sample data. It also writes input files containing * sample input as file:// URLs * all input of one monthly crawl as paths relative to the data bucket base URL s3://commoncrawl/ resp. https://data.commoncrawl.org/ – see authenticated S3 access or access via HTTP for more information.

Note that the sample data is from an older crawl (CC-MAIN-2017-13 run in March 2017). If you want to use more recent data, please visit the Common Crawl site.

Process Common Crawl Data on Spark

CC-PySpark reads the list of input files from a manifest file. Typically, these are Common Crawl WARC, WAT or WET files, but it could be any other type of file, as long it is supported by the class implementing CCSparkJob. The files can be given as absolute URLs or as paths relative to a base URL (option --input_base_url). The URL cat point to a local file (file://), to a remote location (typically below s3://commoncrawl/ resp. https://data.commoncrawl.org/). For development and testing, you'd start with local files.

Running locally

First, point the environment variable SPARK_HOME to your Spark installation. Then submit a job via

$SPARK_HOME/bin/spark-submit ./server_count.py \ --num_output_partitions 1 --log_level WARN \ ./input/test_warc.txt servernames

This will count web server names sent in HTTP response headers for the sample WARC input and store the resulting counts in the SparkSQL table "servernames" in your warehouse location defined by spark.sql.warehouse.dir (usually in your working directory as ./spark-warehouse/servernames).

The output table can be accessed via SparkSQL, e.g.,

``` $SPARK_HOME/bin/pyspark

df = sqlContext.read.parquet("spark-warehouse/servernames") for row in df.sort(df.val.desc()).take(10): print(row) ... Row(key=u'Apache', val=9396) Row(key=u'nginx', val=4339) Row(key=u'Microsoft-IIS/7.5', val=3635) Row(key=u'(no server in HTTP header)', val=3188) Row(key=u'cloudflare-nginx', val=2743) Row(key=u'Microsoft-IIS/8.5', val=1459) Row(key=u'Microsoft-IIS/6.0', val=1324) Row(key=u'GSE', val=886) Row(key=u'Apache/2.2.15 (CentOS)', val=827) Row(key=u'Apache-Coyote/1.1', val=790) ```

But it's also possible to configure a different output format, for example CSV or JSON, see the command-line options.

See also * running the Spark shell and submitting Spark jobs * PySpark SQL API

Running in Spark cluster over large amounts of data

As the Common Crawl dataset lives in the Amazon Public Datasets program, you can access and process it on Amazon AWS (in the us-east-1 AWS region) without incurring any transfer costs. The only cost that you incur is the cost of the machines running your Spark cluster.

  1. spinning up the Spark cluster: AWS EMR contains a ready-to-use Spark installation but you'll find multiple descriptions on the web how to deploy Spark on a cheap cluster of AWS spot instances. See also launching Spark on a cluster.

  2. choose appropriate cluster-specific settings when submitting jobs and also check for relevant command-line options (e.g., --num_input_partitions or --num_output_partitions, see below)

  3. don't forget to deploy all dependencies in the cluster, see advanced dependency management

  4. also the the file sparkcc.py needs to be deployed or added as argument --py-files sparkcc.py to spark-submit. Note: some of the examples require further Python files as dependencies.

Command-line options

All examples show the available command-line options if called with the parameter --help or -h, e.g. $SPARK_HOME/bin/spark-submit ./server_count.py --help

Overwriting Spark configuration properties

There are many Spark configuration properties which allow to tune the job execution or output, see for example see tuning Spark or EMR Spark memory tuning.

It's possible to overwrite Spark properties when submitting the job:

$SPARK_HOME/bin/spark-submit \ --conf spark.sql.warehouse.dir=myWareHouseDir \ ... (other Spark options, flags, config properties) \ ./server_count.py \ ... (program-specific options)

Authenticated S3 Access or Access Via HTTP

Since April 2022 there are two ways to access of Common Crawl data: - using HTTP/HTTPS and the base URL https://data.commoncrawl.org/ or https://ds5q9oxwqwsfj.cloudfront.net/ - using the S3 API to read the bucket s3://commoncrawl/ requires authentication and makes an Amazon Web Services account mandatory.

The S3 API is strongly recommended as the most performant access scheme, if the data is processed in the AWS cloud and in the AWS us-east-1 region. In contrary, if reading the data from outside the AWS cloud, HTTP/HTTPS access is the preferred option.

Dependent on the chosen access scheme, the data bucket's base URL needs to be passed using the command-line option --input_base_url: - --input_base_url https://data.commoncrawl.org/ when using HTTP/HTTPS - --input_base_url s3://commoncrawl/ when using the S3 API.

This project uses boto3 to access WARC, WAT or WET files on s3://commoncrawl/ over the S3 API. The best way is to ensure that a S3 read-only IAM policy is attached to the the IAM role of the EC2 instances where Common Crawl data is processed, see the IAM user guide. If this is no option (or if the processing is not running on AWS), there are various other options to configure credentials in boto3.

Please also note that querying the columnar index requires S3 access.

Querying the columnar index

The example tools to query the columnar URL index may require additional configuration and setup steps.

Supported access schemes for the columnar index

Querying the columnar index using cc-pyspark requires authenticated S3 access. There is no support for HTTP/HTTPS access. Please see here for more information about supported data access schemes.

Installation of S3 Support Libraries

While WARC/WAT/WET files are read using boto3, accessing the columnar URL index (see option --query of CCIndexSparkJob) is done directly by the SparkSQL engine and requires that S3 support libraries are available. These libs are usually provided when the Spark job is run on a Hadoop cluster running on AWS (eg. EMR). However, they may not be provided for any Spark distribution and are usually absent when running Spark locally (not in a Hadoop cluster). In these situations, the easiest way is to add the libs as required packages by adding --packages org.apache.hadoop:hadoop-aws:3.2.1 to the arguments of spark-submit. This will make Spark manage the dependencies - the hadoop-aws package and transitive dependencies are downloaded as Maven dependencies. Note that the required version of hadoop-aws package depends on the Hadoop version bundled with your Spark installation, e.g., Spark 3.2.1 bundled with Hadoop 3.2 (spark-3.2.1-bin-hadoop3.2.tgz).

Please also note that: - the schema of the URL referencing the columnar index depends on the actual S3 file system implementation: it's s3:// on EMR but s3a:// when using s3a. - (since April 2022) only authenticated S3 access is possible. This requires that access to S3 is properly set up. For configuration details, see Authorizing access to EMRFS data in Amazon S3 or Hadoop-AWS: Authenticating with S3.

Below an example call to count words in 10 WARC records host under the .is top-level domain using the --packages option: $SPARK_HOME/bin/spark-submit \ --packages org.apache.hadoop:hadoop-aws:3.3.2 \ ./cc_index_word_count.py \ --input_base_url s3://commoncrawl/ \ --query "SELECT url, warc_filename, warc_record_offset, warc_record_length, content_charset FROM ccindex WHERE crawl = 'CC-MAIN-2020-24' AND subset = 'warc' AND url_host_tld = 'is' LIMIT 10" \ s3a://commoncrawl/cc-index/table/cc-main/warc/ \ myccindexwordcountoutput \ --num_output_partitions 1 \ --output_format json

Columnar index and schema merging

The schema of the columnar URL index has been extended over time by adding new columns. If you want to query one of the new columns (e.g., content_languages), the following Spark configuration option needs to be set: --conf spark.sql.parquet.mergeSchema=true

However, this option impacts the query performance, so use with care! Please also read cc-index-table about configuration options to improve the performance of Spark SQL queries.

Alternatively, it's possible configure the table schema explicitly: - download the latest table schema as JSON - and use it by adding the command-line argument --table_schema cc-index-schema-flat.json.

Using FastWARC to parse WARC files

FastWARC is a high-performance WARC parsing library for Python written in C++/Cython. The API is inspired in large parts by WARCIO, but does not aim at being a drop-in replacement.

Replacing FastWARC can speed up job execution by 25% if little custom computations are done and most of the time is spent for parsing WARC files.

To use FastWARC - the job class must inherit from CCFastWarcSparkJob instead of CCSparkJob. See ServerCountFastWarcJob for an example. - when running the job in a Spark cluster, sparkcc_fastwarc.py must be passed via --py-files in addition to sparkcc.py and further job-specific Python files. See also running in a Spark cluster.

Some differences between the warcio and FastWARC APIs are hidden from the user in methods implemented in CCSparkJob and CCFastWarcSparkJob respectively. These methods allow to access WARC or HTTP headers and the payload stream in a unique way, regardless of whether warcio or FastWARC are used.

However, it's recommended that you carefully verify that your custom job implementation works in combination with FastWARC. There are subtle differences between the warcio and FastWARC APIs, including the underlying classes (WARC/HTTP headers and stream implementations). In addition, FastWARC does not support for legacy ARC files and does not automatically decode HTTP content and transfer encodings (see Resiliparse HTTP Tools). While content and transfer encodings are already decoded in Common Crawl WARC files, this may not be the case for WARC files from other sources. See also WARC 1.1 specification, http/https response records.

Credits

Examples are originally ported from Stephen Merity's cc-mrjob with the following changes and upgrades: * based on Apache Spark (instead of mrjob) * boto3 supporting multi-part download of data from S3 * warcio a Python 2 and Python 3 compatible module for accessing WARC files

Further inspirations are taken from * cosr-back written by Sylvain Zimmer for Common Search. You should definitely take a look at it if you need a more sophisticated WARC processor (including an HTML parser for example). * Mark Litwintschik's blog post Analysing Petabytes of Websites

License

MIT License, as per LICENSE

Owner

  • Name: Common Crawl Foundation
  • Login: commoncrawl
  • Kind: organization
  • Email: info@commoncrawl.org

Common Crawl provides an archive of webpages going back to 2007.

GitHub Events

Total
  • Issues event: 5
  • Watch event: 39
  • Delete event: 6
  • Issue comment event: 13
  • Push event: 6
  • Pull request review comment event: 3
  • Pull request review event: 7
  • Pull request event: 5
  • Fork event: 4
  • Create event: 2
Last Year
  • Issues event: 5
  • Watch event: 39
  • Delete event: 6
  • Issue comment event: 13
  • Push event: 6
  • Pull request review comment event: 3
  • Pull request review event: 7
  • Pull request event: 5
  • Fork event: 4
  • Create event: 2

Committers

Last synced: 8 months ago

All Time
  • Total Commits: 111
  • Total Committers: 6
  • Avg Commits per committer: 18.5
  • Development Distribution Score (DDS): 0.099
Past Year
  • Commits: 16
  • Committers: 2
  • Avg Commits per committer: 8.0
  • Development Distribution Score (DDS): 0.438
Top Committers
Name Email Commits
Sebastian Nagel s****n@c****g 100
silentninja m****5@h****m 7
cronoik j****h@m****e 1
Jae Ro j****o@g****m 1
Alex Xue a****1@g****m 1
Praveen R p****i@i****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 28
  • Total pull requests: 26
  • Average time to close issues: 6 months
  • Average time to close pull requests: 8 months
  • Total issue authors: 15
  • Total pull request authors: 11
  • Average comments per issue: 2.04
  • Average comments per pull request: 1.04
  • Merged pull requests: 14
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 3
  • Pull requests: 6
  • Average time to close issues: 18 days
  • Average time to close pull requests: 25 days
  • Issue authors: 3
  • Pull request authors: 2
  • Average comments per issue: 1.0
  • Average comments per pull request: 1.0
  • Merged pull requests: 4
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • sebastian-nagel (13)
  • swetepete (2)
  • brand17 (1)
  • silentninja (1)
  • praveenr019 (1)
  • BrownXing (1)
  • calee88 (1)
  • chk2817 (1)
  • Thanchanokk-p (1)
  • gamtiq (1)
  • GenuineReader (1)
  • lgov (1)
  • aliebrahiiimi (1)
  • hussien (1)
  • jaehunro (1)
Pull Request Authors
  • sebastian-nagel (15)
  • silentninja (4)
  • jt55401 (2)
  • tylerkovacs (1)
  • lauraedelson (1)
  • rdowd003 (1)
  • praveenr019 (1)
  • cronoik (1)
  • Xue-Alex (1)
  • layedra (1)
  • jaehunro (1)
Top Labels
Issue Labels
enhancement (8) question (5) bug (1)
Pull Request Labels
enhancement (1)

Dependencies

requirements.txt pypi
  • beautifulsoup4 *
  • boto3 *
  • botocore *
  • idna *
  • lxml *
  • requests *
  • ujson *
  • warcio *