{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e0d8a956",
   "metadata": {},
   "source": [
    "## 1. Instalar e importar librerías"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "e6db0063",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: confluent-kafka in .\\.venv-1\\Lib\\site-packages (2.16.0)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install confluent-kafka"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90f29628",
   "metadata": {},
   "source": [
    "## PRODUCTOR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "a379b01b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from confluent_kafka import Producer\n",
    "import socket\n",
    "\n",
    "conf = {'bootstrap.servers':  \"10.22.87.215:9092\",\n",
    "        'client.id': socket.gethostname()}\n",
    "\n",
    "producer = Producer(conf)\n",
    "topic=\"prueba\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "e775e82c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enviado: Value: 20\n",
      "Enviado: Value: 21\n",
      "Enviado: Value: 22\n",
      "Enviado: Value: 23\n",
      "Enviado: Value: 24\n",
      "Enviado: Value: 25\n",
      "Enviado: Value: 26\n",
      "Enviado: Value: 27\n",
      "Enviado: Value: 28\n",
      "Enviado: Value: 29\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def confirmar(err, msg):\n",
    "    if err:\n",
    "        print(\"Error al enviar:\", err)\n",
    "    else:\n",
    "        print(\"Enviado:\", msg.value().decode(\"utf-8\"))\n",
    "\n",
    "for i in range(10):\n",
    "    producer.produce(\n",
    "        topic,\n",
    "        key=f\"key{i}\",\n",
    "        value=f\"Value: {20+i}\",\n",
    "        callback=confirmar\n",
    "    )\n",
    "\n",
    "producer.flush(15)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de499192",
   "metadata": {},
   "source": [
    "## CONSUMIDOR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "0658e6c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from confluent_kafka import Consumer\n",
    "\n",
    "conf = {\n",
    "    'bootstrap.servers': '10.22.87.215:9092',\n",
    "    'group.id': 'grupo_prueba_2026_01',\n",
    "    'auto.offset.reset': 'earliest'\n",
    "}\n",
    "\n",
    "consumer = Consumer(conf)\n",
    "consumer.subscribe(['prueba'])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "510a912e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Recibido: value\n",
      "Recibido: value\n",
      "Recibido: Value: 20\n",
      "Recibido: Value: 21\n",
      "Recibido: Value: 22\n",
      "Recibido: Value: 23\n",
      "Recibido: Value: 24\n",
      "Recibido: Value: 25\n",
      "Recibido: Value: 26\n",
      "Recibido: Value: 27\n",
      "Recibido: Value: 28\n",
      "Recibido: Value: 29\n",
      "Recibido: Value: 20\n",
      "Recibido: Value: 21\n",
      "Recibido: Value: 22\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    for i in range(15):\n",
    "        msg = consumer.poll(timeout=2.0)\n",
    "\n",
    "        if msg is None:\n",
    "            print(\"No hay mensajes\")\n",
    "            continue\n",
    "\n",
    "        if msg.error():\n",
    "            print(\"Error:\", msg.error())\n",
    "            continue\n",
    "\n",
    "        print(\"Recibido:\", msg.value().decode(\"utf-8\"))\n",
    "\n",
    "finally:\n",
    "    consumer.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ff7c8e5",
   "metadata": {},
   "source": [
    "## 2. Instalar e importar librerías"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "fa19f841",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting hdfs\n",
      "  Downloading hdfs-2.7.3.tar.gz (43 kB)\n",
      "  Installing build dependencies: started\n",
      "  Installing build dependencies: finished with status 'done'\n",
      "  Getting requirements to build wheel: started\n",
      "  Getting requirements to build wheel: finished with status 'done'\n",
      "  Preparing metadata (pyproject.toml): started\n",
      "  Preparing metadata (pyproject.toml): finished with status 'done'\n",
      "Collecting docopt (from hdfs)\n",
      "  Downloading docopt-0.6.2.tar.gz (25 kB)\n",
      "  Installing build dependencies: started\n",
      "  Installing build dependencies: finished with status 'done'\n",
      "  Getting requirements to build wheel: started\n",
      "  Getting requirements to build wheel: finished with status 'done'\n",
      "  Preparing metadata (pyproject.toml): started\n",
      "  Preparing metadata (pyproject.toml): finished with status 'done'\n",
      "Collecting requests>=2.7.0 (from hdfs)\n",
      "  Downloading requests-2.34.2-py3-none-any.whl.metadata (4.8 kB)\n",
      "Requirement already satisfied: six>=1.9.0 in .\\.venv-1\\Lib\\site-packages (from hdfs) (1.17.0)\n",
      "Collecting charset_normalizer<4,>=2 (from requests>=2.7.0->hdfs)\n",
      "  Downloading charset_normalizer-3.5.2-cp314-cp314-win_amd64.whl.metadata (47 kB)\n",
      "Collecting idna<4,>=2.5 (from requests>=2.7.0->hdfs)\n",
      "  Downloading idna-3.20-py3-none-any.whl.metadata (7.2 kB)\n",
      "Collecting urllib3<3,>=1.26 (from requests>=2.7.0->hdfs)\n",
      "  Downloading urllib3-2.8.0-py3-none-any.whl.metadata (7.4 kB)\n",
      "Collecting certifi>=2023.5.7 (from requests>=2.7.0->hdfs)\n",
      "  Downloading certifi-2026.10.18-py3-none-any.whl.metadata (2.5 kB)\n",
      "Downloading requests-2.34.2-py3-none-any.whl (73 kB)\n",
      "Downloading charset_normalizer-3.5.2-cp314-cp314-win_amd64.whl (210 kB)\n",
      "Downloading idna-3.20-py3-none-any.whl (69 kB)\n",
      "Downloading urllib3-2.8.0-py3-none-any.whl (135 kB)\n",
      "Downloading certifi-2026.10.18-py3-none-any.whl (134 kB)\n",
      "Building wheels for collected packages: hdfs, docopt\n",
      "  Building wheel for hdfs (pyproject.toml): started\n",
      "  Building wheel for hdfs (pyproject.toml): finished with status 'done'\n",
      "  Created wheel for hdfs: filename=hdfs-2.7.3-py3-none-any.whl size=34465 sha256=53a699f382b6ce5f846f100d08fb8d64d47aac212bda3a7e1135d86aa08afcb2\n",
      "  Stored in directory: c:\\users\\azugadiz\\appdata\\local\\pip\\cache\\wheels\\5a\\d5\\cb\\be014a35f99114a5cb33bc0e4567463beb432dce4e9766e7f1\n",
      "  Building wheel for docopt (pyproject.toml): started\n",
      "  Building wheel for docopt (pyproject.toml): finished with status 'done'\n",
      "  Created wheel for docopt: filename=docopt-0.6.2-py2.py3-none-any.whl size=13889 sha256=eb8a5e52b0ddac742e711cae0b848102d9da18b8292b3a58f2751e437779f8b4\n",
      "  Stored in directory: c:\\users\\azugadiz\\appdata\\local\\pip\\cache\\wheels\\79\\6f\\5f\\26c3d1a144117ef52122d67c32b9f3c297c3dec4fea00a31ac\n",
      "Successfully built hdfs docopt\n",
      "Installing collected packages: docopt, urllib3, idna, charset_normalizer, certifi, requests, hdfs\n",
      "\n",
      "   ----------------- ---------------------- 3/7 [charset_normalizer]\n",
      "   ---------------------------------------- 7/7 [hdfs]\n",
      "\n",
      "Successfully installed certifi-2026.10.18 charset_normalizer-3.5.2 docopt-0.6.2 hdfs-2.7.3 idna-3.20 requests-2.34.2 urllib3-2.8.0\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install hdfs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45a4e9a1",
   "metadata": {},
   "source": [
    "## HDFS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a35d27b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from hdfs import InsecureClient\n",
    "\n",
    "client = InsecureClient(\n",
    "    \"http://10.22.87.215:50070\",\n",
    "    user=\"VUESTRO_USUARIO\"\n",
    ")\n",
    "\n",
    "#crear carpeta\n",
    "client.makedirs('/user/VUESTRO_USUARIO/pruebaHDFS')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f732c88f",
   "metadata": {},
   "source": [
    "# Escribir en HDFS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de8aee57",
   "metadata": {},
   "outputs": [],
   "source": [
    "#escribir en HDFS\n",
    "import json\n",
    "\n",
    "datos = [\n",
    "    {\"key\": \"S01\", \"value\": 25},\n",
    "    {\"key\": \"S02\", \"value\": 28},\n",
    "    {\"key\": \"S03\", \"value\": 22}\n",
    "]\n",
    "\n",
    "with client.write(\n",
    "    '/user/VUESTRO_USUARIO/pruebaHDFS/datos.jsonl',\n",
    "    encoding='utf-8',\n",
    "    overwrite=True\n",
    ") as writer:\n",
    "    for dato in datos:\n",
    "        writer.write(json.dumps(dato) + '\\n')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcaf7c5f",
   "metadata": {},
   "source": [
    "# Leer del HDFS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adc5c71a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'key': 'S01', 'value': 25}\n",
      "{'key': 'S02', 'value': 28}\n",
      "{'key': 'S03', 'value': 22}\n"
     ]
    }
   ],
   "source": [
    "#leer en HDFS\n",
    "with client.read(\n",
    "    '/user/VUESTRO_USUARIO/pruebaHDFS/datos.jsonl',\n",
    "    encoding='utf-8'\n",
    ") as reader:\n",
    "    for linea in reader:\n",
    "        dato = json.loads(linea)\n",
    "        print(dato)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70f12ccc",
   "metadata": {},
   "source": [
    "# Listar directorios de HDFS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a91833df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['.Trash',\n",
       " '.hiveJars',\n",
       " '.scratchdir',\n",
       " '.sparkStaging',\n",
       " '.staging',\n",
       " 'PruebaHue',\n",
       " 'RETO',\n",
       " 'dt',\n",
       " 'files-view',\n",
       " 'hive',\n",
       " 'mapreduce',\n",
       " 'prueba',\n",
       " 'pruebaHDFS',\n",
       " 'sabana2.json',\n",
       " 'strangerCharacters.json',\n",
       " 'supermarket_sales+-+Sheet1.csv',\n",
       " 'texto.txt',\n",
       " 'transaction.json',\n",
       " 'user',\n",
       " 'usuario.json']"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "client.list('/user/VUESTRO_USUARIO/')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv-1 (3.14.7)",
   "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.14.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
