{ "cells": [ { "cell_type": "code", "execution_count": 5, "id": "e3cf6fb7-e4e7-4cd7-baa7-03209328631c", "metadata": {}, "outputs": [], "source": [ "from pyspark.sql import SparkSession\n", "import pyspark.sql.functions as F\n", "\n", "spark = SparkSession.builder.appName(\"some_name\").getOrCreate()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "f0f1fb08-2373-48b2-99d9-1424a4ceb0c0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "+---------+\n", "|log_level|\n", "+---------+\n", "|INFO |\n", "|DEBUG |\n", "|ERROR |\n", "|INFO |\n", "|ERROR |\n", "|DEBUG |\n", "|ERROR |\n", "+---------+\n", "\n" ] } ], "source": [ "parts = F.split(F.col(\"value\"), \" \", 2)\n", "df_logs = spark.read.text(\"logs.txt\").select(parts[0].alias(\"log_level\"))\n", "df_logs.show(truncate=False)" ] }, { "cell_type": "code", "execution_count": 37, "id": "6d784b52-9369-4051-8084-a7f705c001fa", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_error_logs = df_logs.filter(F.col(\"log_level\") == \"ERROR\").count()\n", "count_error" ] }, { "cell_type": "code", "execution_count": 38, "id": "592dc010-5700-4cc1-b0bf-8738c3b1567e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.42857142857142855" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "total = df_logs.count()\n", "count_error / total" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "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.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }