{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Wikipedia Page Titles\n", "- populate `page_titles` table from single dump file `enwiki-20190420-pages-articles-multistream.xml.bz2`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# basic defaults, including study dates, common SQL exclusions and parquet files for anonymized data\n", "%run -i 'data-defaults.py'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "+-------+--------------------+\n", "|page_id| page_title|\n", "+-------+--------------------+\n", "| 12| Anarchism|\n", "| 25| Autism|\n", "| 39| Albedo|\n", "| 290| A|\n", "| 303| Alabama|\n", "| 305| Achilles|\n", "| 307| Abraham Lincoln|\n", "| 308| Aristotle|\n", "| 309|An American in Paris|\n", "| 316|Academy Award for...|\n", "| 324| Academy Awards|\n", "| 330| Actrius|\n", "| 332| Animalia (book)|\n", "| 334|International Ato...|\n", "| 336| Altruism|\n", "| 339| Ayn Rand|\n", "| 340| Alain Connes|\n", "| 344| Allan Dwan|\n", "| 358| Algeria|\n", "| 359|List of Atlas Shr...|\n", "+-------+--------------------+\n", "only showing top 20 rows\n", "\n" ] } ], "source": [ "WIKIPEDIA_XML_DUMP = 'enwiki-20190420-pages-articles-multistream.xml.bz2'\n", "\n", "def page_title(entity):\n", " page_title = entity.title\n", " return Row(page_id=entity.id, page_title=entity.title)\n", "\n", "wikipedia = sqlContext.read.format('com.databricks.spark.xml').options(rowTag='page').load(WIKIPEDIA_XML_DUMP)\n", "articles = wikipedia\\\n", " .filter(\"ns = '0'\")\\\n", " .filter(\"redirect._title is null\") \\\n", " .filter(\"revision.text._VALUE is not null\") \\\n", " .filter(\"length(revision.text._VALUE) > 0\")\n", "page_titles = sqlContext.createDataFrame(articles.rdd.map(lambda entity: page_title(entity)))\n", "page_titles.show()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "DataFrame[]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# save page titles data to a table for later use\n", "page_titles.registerTempTable(\"temp_page_titles\")\n", "sqlContext.sql(\"DROP TABLE IF EXISTS ryanmax.population_page_titles_20190420\")\n", "sqlContext.sql(\"CREATE TABLE ryanmax.population_page_titles_20190420 AS SELECT * FROM temp_page_titles\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "+--------+-----------+\n", "|page_ids|page_titles|\n", "+--------+-----------+\n", "| 5847824| 5847824|\n", "+--------+-----------+\n", "\n" ] } ], "source": [ "# sanity check: 1:1 ratio of page_id to page_title\n", "q = \"\"\"\n", "select count(distinct page_id) as page_ids, count(distinct page_title) as page_titles \n", " from ryanmax.population_page_titles_20190420\n", "\"\"\"\n", "sqlContext.sql(q).show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "PySpark large: spark-xml jar and local venv path ", "language": "python", "name": "spark-ryanmax" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.3" } }, "nbformat": 4, "nbformat_minor": 2 }