[{"data":1,"prerenderedAt":3726},["ShallowReactive",2],{"post-playing-with-ocr-libraries":3,"rel-playing-with-ocr-libraries":526,"sib-playing-with-ocr-libraries":3654},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"layout":11,"subtitle":12,"image":13,"optimized_image":13,"category":14,"tags":15,"author":18,"paginate":6,"body":19,"_type":520,"_id":521,"_source":522,"_file":523,"_stem":524,"_extension":525},"/posts/playing-with-ocr-libraries","posts",false,"","Playing with OCR libraries","How to recognize text from images with pytesseract library. From image to python string","2020-11-22T11:00:00.000Z","post","How to recognize text from images with pytesseract library","/assets/img/uploads/ocr.jpg","code",[16,17],"ocr","python","jaimedearcos",{"type":20,"children":21,"toc":515},"root",[22,39,46,69,102,107,176,181,223,229,477,489,495,509],{"type":23,"tag":24,"props":25,"children":26},"element","p",{},[27,30,37],{"type":28,"value":29},"text","A Few days ago I found a cool library for text recognition in images, ",{"type":23,"tag":31,"props":32,"children":34},"a",{"href":33},"https://pypi.org/project/pytesseract/",[35],{"type":28,"value":36},"pytesseract",{"type":28,"value":38},".\nWith this library and just a few lines of code you can implement OCR features.",{"type":23,"tag":40,"props":41,"children":43},"h2",{"id":42},"get-started",[44],{"type":28,"value":45},"Get started",{"type":23,"tag":24,"props":47,"children":48},{},[49,51,57,59,67],{"type":28,"value":50},"Install ",{"type":23,"tag":14,"props":52,"children":54},{"className":53},[],[55],{"type":28,"value":56},"teseract",{"type":28,"value":58}," in MacOs (",{"type":23,"tag":31,"props":60,"children":64},{"href":61,"rel":62},"https://tesseract-ocr.github.io/tessdoc/Home.html",[63],"nofollow",[65],{"type":28,"value":66},"for other platforms",{"type":28,"value":68},")",{"type":23,"tag":70,"props":71,"children":75},"pre",{"className":72,"code":73,"language":74,"meta":7,"style":7},"language-sh shiki shiki-themes github-dark github-light","brew install tesseract\n","sh",[76],{"type":23,"tag":14,"props":77,"children":78},{"__ignoreMap":7},[79],{"type":23,"tag":80,"props":81,"children":84},"span",{"class":82,"line":83},"line",1,[85,91,97],{"type":23,"tag":80,"props":86,"children":88},{"style":87},"--shiki-default:#B392F0;--shiki-light:#6F42C1",[89],{"type":28,"value":90},"brew",{"type":23,"tag":80,"props":92,"children":94},{"style":93},"--shiki-default:#9ECBFF;--shiki-light:#032F62",[95],{"type":28,"value":96}," install",{"type":23,"tag":80,"props":98,"children":99},{"style":93},[100],{"type":28,"value":101}," tesseract\n",{"type":23,"tag":24,"props":103,"children":104},{},[105],{"type":28,"value":106},"Create a new python environment & install requirements",{"type":23,"tag":70,"props":108,"children":112},{"className":109,"code":110,"language":111,"meta":7,"style":7},"language-bash shiki shiki-themes github-dark github-light","python -m venv en\nsource env/bin/activate\npip install -r requirements.txt\n","bash",[113],{"type":23,"tag":14,"props":114,"children":115},{"__ignoreMap":7},[116,139,153],{"type":23,"tag":80,"props":117,"children":118},{"class":82,"line":83},[119,123,129,134],{"type":23,"tag":80,"props":120,"children":121},{"style":87},[122],{"type":28,"value":17},{"type":23,"tag":80,"props":124,"children":126},{"style":125},"--shiki-default:#79B8FF;--shiki-light:#005CC5",[127],{"type":28,"value":128}," -m",{"type":23,"tag":80,"props":130,"children":131},{"style":93},[132],{"type":28,"value":133}," venv",{"type":23,"tag":80,"props":135,"children":136},{"style":93},[137],{"type":28,"value":138}," en\n",{"type":23,"tag":80,"props":140,"children":142},{"class":82,"line":141},2,[143,148],{"type":23,"tag":80,"props":144,"children":145},{"style":125},[146],{"type":28,"value":147},"source",{"type":23,"tag":80,"props":149,"children":150},{"style":93},[151],{"type":28,"value":152}," env/bin/activate\n",{"type":23,"tag":80,"props":154,"children":156},{"class":82,"line":155},3,[157,162,166,171],{"type":23,"tag":80,"props":158,"children":159},{"style":87},[160],{"type":28,"value":161},"pip",{"type":23,"tag":80,"props":163,"children":164},{"style":93},[165],{"type":28,"value":96},{"type":23,"tag":80,"props":167,"children":168},{"style":125},[169],{"type":28,"value":170}," -r",{"type":23,"tag":80,"props":172,"children":173},{"style":93},[174],{"type":28,"value":175}," requirements.txt\n",{"type":23,"tag":24,"props":177,"children":178},{},[179],{"type":28,"value":180},"requirements.txt file",{"type":23,"tag":70,"props":182,"children":186},{"className":183,"code":184,"language":185,"meta":7,"style":7},"language-txt shiki shiki-themes github-dark github-light","numpy==1.19.4\nopencv-python==4.4.0.46\nPillow==8.0.1\npytesseract==0.3.6\n","txt",[187],{"type":23,"tag":14,"props":188,"children":189},{"__ignoreMap":7},[190,198,206,214],{"type":23,"tag":80,"props":191,"children":192},{"class":82,"line":83},[193],{"type":23,"tag":80,"props":194,"children":195},{},[196],{"type":28,"value":197},"numpy==1.19.4\n",{"type":23,"tag":80,"props":199,"children":200},{"class":82,"line":141},[201],{"type":23,"tag":80,"props":202,"children":203},{},[204],{"type":28,"value":205},"opencv-python==4.4.0.46\n",{"type":23,"tag":80,"props":207,"children":208},{"class":82,"line":155},[209],{"type":23,"tag":80,"props":210,"children":211},{},[212],{"type":28,"value":213},"Pillow==8.0.1\n",{"type":23,"tag":80,"props":215,"children":217},{"class":82,"line":216},4,[218],{"type":23,"tag":80,"props":219,"children":220},{},[221],{"type":28,"value":222},"pytesseract==0.3.6\n",{"type":23,"tag":40,"props":224,"children":226},{"id":225},"create-your-first-ocr-script",[227],{"type":28,"value":228},"Create your first ocr script",{"type":23,"tag":70,"props":230,"children":233},{"className":231,"code":232,"language":17,"meta":7,"style":7},"language-python shiki shiki-themes github-dark github-light","from PIL import Image\nimport pytesseract\nimport argparse\nimport sys\n\noriginal_image = \"image.jpg\"\nimage = cv2.imread(original_image)\ngray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n\n# Convert to grayscale for better performance\nfilename = \"grayscale_image.png\"\ncv2.imwrite(filename, gray)\n\n# Load the image as a PIL/Pillow image and apply OCR\ntext = pytesseract.image_to_string(Image.open(filename))\nos.remove(filename)\nprint(text)\n",[234],{"type":23,"tag":14,"props":235,"children":236},{"__ignoreMap":7},[237,262,275,287,299,309,328,346,374,382,392,410,419,427,436,454,463],{"type":23,"tag":80,"props":238,"children":239},{"class":82,"line":83},[240,246,251,256],{"type":23,"tag":80,"props":241,"children":243},{"style":242},"--shiki-default:#F97583;--shiki-light:#D73A49",[244],{"type":28,"value":245},"from",{"type":23,"tag":80,"props":247,"children":248},{"style":125},[249],{"type":28,"value":250}," PIL",{"type":23,"tag":80,"props":252,"children":253},{"style":242},[254],{"type":28,"value":255}," import",{"type":23,"tag":80,"props":257,"children":259},{"style":258},"--shiki-default:#E1E4E8;--shiki-light:#24292E",[260],{"type":28,"value":261}," Image\n",{"type":23,"tag":80,"props":263,"children":264},{"class":82,"line":141},[265,270],{"type":23,"tag":80,"props":266,"children":267},{"style":242},[268],{"type":28,"value":269},"import",{"type":23,"tag":80,"props":271,"children":272},{"style":258},[273],{"type":28,"value":274}," pytesseract\n",{"type":23,"tag":80,"props":276,"children":277},{"class":82,"line":155},[278,282],{"type":23,"tag":80,"props":279,"children":280},{"style":242},[281],{"type":28,"value":269},{"type":23,"tag":80,"props":283,"children":284},{"style":258},[285],{"type":28,"value":286}," argparse\n",{"type":23,"tag":80,"props":288,"children":289},{"class":82,"line":216},[290,294],{"type":23,"tag":80,"props":291,"children":292},{"style":242},[293],{"type":28,"value":269},{"type":23,"tag":80,"props":295,"children":296},{"style":258},[297],{"type":28,"value":298}," sys\n",{"type":23,"tag":80,"props":300,"children":302},{"class":82,"line":301},5,[303],{"type":23,"tag":80,"props":304,"children":306},{"emptyLinePlaceholder":305},true,[307],{"type":28,"value":308},"\n",{"type":23,"tag":80,"props":310,"children":312},{"class":82,"line":311},6,[313,318,323],{"type":23,"tag":80,"props":314,"children":315},{"style":258},[316],{"type":28,"value":317},"original_image ",{"type":23,"tag":80,"props":319,"children":320},{"style":242},[321],{"type":28,"value":322},"=",{"type":23,"tag":80,"props":324,"children":325},{"style":93},[326],{"type":28,"value":327}," \"image.jpg\"\n",{"type":23,"tag":80,"props":329,"children":331},{"class":82,"line":330},7,[332,337,341],{"type":23,"tag":80,"props":333,"children":334},{"style":258},[335],{"type":28,"value":336},"image ",{"type":23,"tag":80,"props":338,"children":339},{"style":242},[340],{"type":28,"value":322},{"type":23,"tag":80,"props":342,"children":343},{"style":258},[344],{"type":28,"value":345}," cv2.imread(original_image)\n",{"type":23,"tag":80,"props":347,"children":349},{"class":82,"line":348},8,[350,355,359,364,369],{"type":23,"tag":80,"props":351,"children":352},{"style":258},[353],{"type":28,"value":354},"gray ",{"type":23,"tag":80,"props":356,"children":357},{"style":242},[358],{"type":28,"value":322},{"type":23,"tag":80,"props":360,"children":361},{"style":258},[362],{"type":28,"value":363}," cv2.cvtColor(image, cv2.",{"type":23,"tag":80,"props":365,"children":366},{"style":125},[367],{"type":28,"value":368},"COLOR_BGR2GRAY",{"type":23,"tag":80,"props":370,"children":371},{"style":258},[372],{"type":28,"value":373},")\n",{"type":23,"tag":80,"props":375,"children":377},{"class":82,"line":376},9,[378],{"type":23,"tag":80,"props":379,"children":380},{"emptyLinePlaceholder":305},[381],{"type":28,"value":308},{"type":23,"tag":80,"props":383,"children":385},{"class":82,"line":384},10,[386],{"type":23,"tag":80,"props":387,"children":389},{"style":388},"--shiki-default:#6A737D;--shiki-light:#6A737D",[390],{"type":28,"value":391},"# Convert to grayscale for better performance\n",{"type":23,"tag":80,"props":393,"children":395},{"class":82,"line":394},11,[396,401,405],{"type":23,"tag":80,"props":397,"children":398},{"style":258},[399],{"type":28,"value":400},"filename ",{"type":23,"tag":80,"props":402,"children":403},{"style":242},[404],{"type":28,"value":322},{"type":23,"tag":80,"props":406,"children":407},{"style":93},[408],{"type":28,"value":409}," \"grayscale_image.png\"\n",{"type":23,"tag":80,"props":411,"children":413},{"class":82,"line":412},12,[414],{"type":23,"tag":80,"props":415,"children":416},{"style":258},[417],{"type":28,"value":418},"cv2.imwrite(filename, gray)\n",{"type":23,"tag":80,"props":420,"children":422},{"class":82,"line":421},13,[423],{"type":23,"tag":80,"props":424,"children":425},{"emptyLinePlaceholder":305},[426],{"type":28,"value":308},{"type":23,"tag":80,"props":428,"children":430},{"class":82,"line":429},14,[431],{"type":23,"tag":80,"props":432,"children":433},{"style":388},[434],{"type":28,"value":435},"# Load the image as a PIL/Pillow image and apply OCR\n",{"type":23,"tag":80,"props":437,"children":439},{"class":82,"line":438},15,[440,445,449],{"type":23,"tag":80,"props":441,"children":442},{"style":258},[443],{"type":28,"value":444},"text ",{"type":23,"tag":80,"props":446,"children":447},{"style":242},[448],{"type":28,"value":322},{"type":23,"tag":80,"props":450,"children":451},{"style":258},[452],{"type":28,"value":453}," pytesseract.image_to_string(Image.open(filename))\n",{"type":23,"tag":80,"props":455,"children":457},{"class":82,"line":456},16,[458],{"type":23,"tag":80,"props":459,"children":460},{"style":258},[461],{"type":28,"value":462},"os.remove(filename)\n",{"type":23,"tag":80,"props":464,"children":466},{"class":82,"line":465},17,[467,472],{"type":23,"tag":80,"props":468,"children":469},{"style":125},[470],{"type":28,"value":471},"print",{"type":23,"tag":80,"props":473,"children":474},{"style":258},[475],{"type":28,"value":476},"(text)\n",{"type":23,"tag":24,"props":478,"children":479},{},[480,482],{"type":28,"value":481},"You can find the complete code of this example in ",{"type":23,"tag":31,"props":483,"children":486},{"href":484,"rel":485},"https://github.com/JaimeDeArcos/ocr-python",[63],[487],{"type":28,"value":488},"this GitHub repository",{"type":23,"tag":40,"props":490,"children":492},{"id":491},"references",[493],{"type":28,"value":494},"References",{"type":23,"tag":496,"props":497,"children":498},"ul",{},[499],{"type":23,"tag":500,"props":501,"children":502},"li",{},[503],{"type":23,"tag":31,"props":504,"children":506},{"href":33,"rel":505},[63],[507],{"type":28,"value":508},"PyTesseract pip documentation",{"type":23,"tag":510,"props":511,"children":512},"style",{},[513],{"type":28,"value":514},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}",{"title":7,"searchDepth":141,"depth":141,"links":516},[517,518,519],{"id":42,"depth":141,"text":45},{"id":225,"depth":141,"text":228},{"id":491,"depth":141,"text":494},"markdown","content:posts:playing-with-ocr-libraries.md","content","posts/playing-with-ocr-libraries.md","posts/playing-with-ocr-libraries","md",[527,1925,2834],{"_path":528,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":529,"description":530,"date":531,"layout":11,"subtitle":532,"image":533,"optimized_image":533,"category":14,"tags":534,"author":18,"paginate":6,"body":540,"_type":520,"_id":1922,"_source":522,"_file":1923,"_stem":1924,"_extension":525},"/posts/gpt-sql-integration","Implementa un analizador de consultas en lenguaje natural para bases de datos SQL con GPT","Aprende a implementar un sistema que convierte consultas en lenguaje natural en sentencias SQL usando OpenAI GPT, con un tutorial paso a paso y ejemplos prácticos.","2025-01-15T11:00:00.000Z","Transforma preguntas naturales en consultas SQL automatizadas","/assets/img/uploads/gpt-sql-integration.png",[14,535,536,537,538,539],"gpt","openai","sql","ai","nlp",{"type":20,"children":541,"toc":1911},[542,551,557,575,581,586,624,637,653,659,664,869,875,880,1211,1217,1222,1570,1576,1581,1640,1646,1651,1832,1838,1856,1862,1867,1874,1907],{"type":23,"tag":543,"props":544,"children":545},"blockquote",{},[546],{"type":23,"tag":24,"props":547,"children":548},{},[549],{"type":28,"value":550},"Convertir preguntas naturales en consultas SQL de forma automática simplifica la interacción con bases de datos, permitiendo que usuarios sin conocimientos técnicos extraigan datos relevantes. En este tutorial, aprenderás a implementar esta solución utilizando la API de OpenAI GPT.",{"type":23,"tag":40,"props":552,"children":554},{"id":553},"requisitos-previos",[555],{"type":28,"value":556},"Requisitos Previos",{"type":23,"tag":496,"props":558,"children":559},{},[560,565,570],{"type":23,"tag":500,"props":561,"children":562},{},[563],{"type":28,"value":564},"Cuenta y clave API de OpenAI",{"type":23,"tag":500,"props":566,"children":567},{},[568],{"type":28,"value":569},"Python instalado en tu entorno",{"type":23,"tag":500,"props":571,"children":572},{},[573],{"type":28,"value":574},"Base de datos SQL (ej.: PostgreSQL, MySQL)",{"type":23,"tag":40,"props":576,"children":578},{"id":577},"paso-1-configuración-del-entorno",[579],{"type":28,"value":580},"Paso 1: Configuración del entorno",{"type":23,"tag":24,"props":582,"children":583},{},[584],{"type":28,"value":585},"Primero, instala las dependencias necesarias:",{"type":23,"tag":70,"props":587,"children":589},{"className":109,"code":588,"language":111,"meta":7,"style":7},"pip install openai python-dotenv sqlalchemy psycopg2-binary\n",[590],{"type":23,"tag":14,"props":591,"children":592},{"__ignoreMap":7},[593],{"type":23,"tag":80,"props":594,"children":595},{"class":82,"line":83},[596,600,604,609,614,619],{"type":23,"tag":80,"props":597,"children":598},{"style":87},[599],{"type":28,"value":161},{"type":23,"tag":80,"props":601,"children":602},{"style":93},[603],{"type":28,"value":96},{"type":23,"tag":80,"props":605,"children":606},{"style":93},[607],{"type":28,"value":608}," openai",{"type":23,"tag":80,"props":610,"children":611},{"style":93},[612],{"type":28,"value":613}," python-dotenv",{"type":23,"tag":80,"props":615,"children":616},{"style":93},[617],{"type":28,"value":618}," sqlalchemy",{"type":23,"tag":80,"props":620,"children":621},{"style":93},[622],{"type":28,"value":623}," psycopg2-binary\n",{"type":23,"tag":24,"props":625,"children":626},{},[627,629,635],{"type":28,"value":628},"Guarda tu clave API en un archivo ",{"type":23,"tag":14,"props":630,"children":632},{"className":631},[],[633],{"type":28,"value":634},".env",{"type":28,"value":636},":",{"type":23,"tag":70,"props":638,"children":642},{"className":639,"code":640,"language":641,"meta":7,"style":7},"language-env shiki shiki-themes github-dark github-light","OPENAI_API_KEY='tu-api-key'\n","env",[643],{"type":23,"tag":14,"props":644,"children":645},{"__ignoreMap":7},[646],{"type":23,"tag":80,"props":647,"children":648},{"class":82,"line":83},[649],{"type":23,"tag":80,"props":650,"children":651},{},[652],{"type":28,"value":640},{"type":23,"tag":40,"props":654,"children":656},{"id":655},"paso-2-conexión-a-la-base-de-datos",[657],{"type":28,"value":658},"Paso 2: Conexión a la base de datos",{"type":23,"tag":24,"props":660,"children":661},{},[662],{"type":28,"value":663},"Utiliza SQLAlchemy para conectarte a tu base de datos:",{"type":23,"tag":70,"props":665,"children":667},{"className":231,"code":666,"language":17,"meta":7,"style":7},"from sqlalchemy import create_engine\nfrom sqlalchemy.orm import sessionmaker\nimport os\nfrom dotenv import load_dotenv\n\nload_dotenv()\n\nDATABASE_URL = os.getenv(\"DATABASE_URL\")\nengine = create_engine(DATABASE_URL)\nSession = sessionmaker(bind=engine)\nsession = Session()\n",[668],{"type":23,"tag":14,"props":669,"children":670},{"__ignoreMap":7},[671,692,713,725,746,753,761,768,795,820,852],{"type":23,"tag":80,"props":672,"children":673},{"class":82,"line":83},[674,678,683,687],{"type":23,"tag":80,"props":675,"children":676},{"style":242},[677],{"type":28,"value":245},{"type":23,"tag":80,"props":679,"children":680},{"style":258},[681],{"type":28,"value":682}," sqlalchemy ",{"type":23,"tag":80,"props":684,"children":685},{"style":242},[686],{"type":28,"value":269},{"type":23,"tag":80,"props":688,"children":689},{"style":258},[690],{"type":28,"value":691}," create_engine\n",{"type":23,"tag":80,"props":693,"children":694},{"class":82,"line":141},[695,699,704,708],{"type":23,"tag":80,"props":696,"children":697},{"style":242},[698],{"type":28,"value":245},{"type":23,"tag":80,"props":700,"children":701},{"style":258},[702],{"type":28,"value":703}," sqlalchemy.orm ",{"type":23,"tag":80,"props":705,"children":706},{"style":242},[707],{"type":28,"value":269},{"type":23,"tag":80,"props":709,"children":710},{"style":258},[711],{"type":28,"value":712}," sessionmaker\n",{"type":23,"tag":80,"props":714,"children":715},{"class":82,"line":155},[716,720],{"type":23,"tag":80,"props":717,"children":718},{"style":242},[719],{"type":28,"value":269},{"type":23,"tag":80,"props":721,"children":722},{"style":258},[723],{"type":28,"value":724}," os\n",{"type":23,"tag":80,"props":726,"children":727},{"class":82,"line":216},[728,732,737,741],{"type":23,"tag":80,"props":729,"children":730},{"style":242},[731],{"type":28,"value":245},{"type":23,"tag":80,"props":733,"children":734},{"style":258},[735],{"type":28,"value":736}," dotenv ",{"type":23,"tag":80,"props":738,"children":739},{"style":242},[740],{"type":28,"value":269},{"type":23,"tag":80,"props":742,"children":743},{"style":258},[744],{"type":28,"value":745}," load_dotenv\n",{"type":23,"tag":80,"props":747,"children":748},{"class":82,"line":301},[749],{"type":23,"tag":80,"props":750,"children":751},{"emptyLinePlaceholder":305},[752],{"type":28,"value":308},{"type":23,"tag":80,"props":754,"children":755},{"class":82,"line":311},[756],{"type":23,"tag":80,"props":757,"children":758},{"style":258},[759],{"type":28,"value":760},"load_dotenv()\n",{"type":23,"tag":80,"props":762,"children":763},{"class":82,"line":330},[764],{"type":23,"tag":80,"props":765,"children":766},{"emptyLinePlaceholder":305},[767],{"type":28,"value":308},{"type":23,"tag":80,"props":769,"children":770},{"class":82,"line":348},[771,776,781,786,791],{"type":23,"tag":80,"props":772,"children":773},{"style":125},[774],{"type":28,"value":775},"DATABASE_URL",{"type":23,"tag":80,"props":777,"children":778},{"style":242},[779],{"type":28,"value":780}," =",{"type":23,"tag":80,"props":782,"children":783},{"style":258},[784],{"type":28,"value":785}," os.getenv(",{"type":23,"tag":80,"props":787,"children":788},{"style":93},[789],{"type":28,"value":790},"\"DATABASE_URL\"",{"type":23,"tag":80,"props":792,"children":793},{"style":258},[794],{"type":28,"value":373},{"type":23,"tag":80,"props":796,"children":797},{"class":82,"line":376},[798,803,807,812,816],{"type":23,"tag":80,"props":799,"children":800},{"style":258},[801],{"type":28,"value":802},"engine ",{"type":23,"tag":80,"props":804,"children":805},{"style":242},[806],{"type":28,"value":322},{"type":23,"tag":80,"props":808,"children":809},{"style":258},[810],{"type":28,"value":811}," create_engine(",{"type":23,"tag":80,"props":813,"children":814},{"style":125},[815],{"type":28,"value":775},{"type":23,"tag":80,"props":817,"children":818},{"style":258},[819],{"type":28,"value":373},{"type":23,"tag":80,"props":821,"children":822},{"class":82,"line":384},[823,828,832,837,843,847],{"type":23,"tag":80,"props":824,"children":825},{"style":258},[826],{"type":28,"value":827},"Session ",{"type":23,"tag":80,"props":829,"children":830},{"style":242},[831],{"type":28,"value":322},{"type":23,"tag":80,"props":833,"children":834},{"style":258},[835],{"type":28,"value":836}," sessionmaker(",{"type":23,"tag":80,"props":838,"children":840},{"style":839},"--shiki-default:#FFAB70;--shiki-light:#E36209",[841],{"type":28,"value":842},"bind",{"type":23,"tag":80,"props":844,"children":845},{"style":242},[846],{"type":28,"value":322},{"type":23,"tag":80,"props":848,"children":849},{"style":258},[850],{"type":28,"value":851},"engine)\n",{"type":23,"tag":80,"props":853,"children":854},{"class":82,"line":394},[855,860,864],{"type":23,"tag":80,"props":856,"children":857},{"style":258},[858],{"type":28,"value":859},"session ",{"type":23,"tag":80,"props":861,"children":862},{"style":242},[863],{"type":28,"value":322},{"type":23,"tag":80,"props":865,"children":866},{"style":258},[867],{"type":28,"value":868}," Session()\n",{"type":23,"tag":40,"props":870,"children":872},{"id":871},"paso-3-obtener-esquema-de-la-base-de-datos",[873],{"type":28,"value":874},"Paso 3: Obtener esquema de la base de datos",{"type":23,"tag":24,"props":876,"children":877},{},[878],{"type":28,"value":879},"Genera una descripción del esquema que será utilizada por GPT:",{"type":23,"tag":70,"props":881,"children":883},{"className":231,"code":882,"language":17,"meta":7,"style":7},"from sqlalchemy import inspect\n\ndef get_database_schema(engine):\n    inspector = inspect(engine)\n    schema_description = \"\"\n    for table_name in inspector.get_table_names():\n        schema_description += f\"Table: {table_name}\\n\"\n        for column in inspector.get_columns(table_name):\n            schema_description += f\"- {column['name']} ({column['type']})\\n\"\n        schema_description += \"\\n\"\n    return schema_description\n\nschema_description = get_database_schema(engine)\nprint(schema_description)\n",[884],{"type":23,"tag":14,"props":885,"children":886},{"__ignoreMap":7},[887,907,914,932,949,966,989,1032,1054,1138,1162,1175,1182,1199],{"type":23,"tag":80,"props":888,"children":889},{"class":82,"line":83},[890,894,898,902],{"type":23,"tag":80,"props":891,"children":892},{"style":242},[893],{"type":28,"value":245},{"type":23,"tag":80,"props":895,"children":896},{"style":258},[897],{"type":28,"value":682},{"type":23,"tag":80,"props":899,"children":900},{"style":242},[901],{"type":28,"value":269},{"type":23,"tag":80,"props":903,"children":904},{"style":258},[905],{"type":28,"value":906}," inspect\n",{"type":23,"tag":80,"props":908,"children":909},{"class":82,"line":141},[910],{"type":23,"tag":80,"props":911,"children":912},{"emptyLinePlaceholder":305},[913],{"type":28,"value":308},{"type":23,"tag":80,"props":915,"children":916},{"class":82,"line":155},[917,922,927],{"type":23,"tag":80,"props":918,"children":919},{"style":242},[920],{"type":28,"value":921},"def",{"type":23,"tag":80,"props":923,"children":924},{"style":87},[925],{"type":28,"value":926}," get_database_schema",{"type":23,"tag":80,"props":928,"children":929},{"style":258},[930],{"type":28,"value":931},"(engine):\n",{"type":23,"tag":80,"props":933,"children":934},{"class":82,"line":216},[935,940,944],{"type":23,"tag":80,"props":936,"children":937},{"style":258},[938],{"type":28,"value":939},"    inspector ",{"type":23,"tag":80,"props":941,"children":942},{"style":242},[943],{"type":28,"value":322},{"type":23,"tag":80,"props":945,"children":946},{"style":258},[947],{"type":28,"value":948}," inspect(engine)\n",{"type":23,"tag":80,"props":950,"children":951},{"class":82,"line":301},[952,957,961],{"type":23,"tag":80,"props":953,"children":954},{"style":258},[955],{"type":28,"value":956},"    schema_description ",{"type":23,"tag":80,"props":958,"children":959},{"style":242},[960],{"type":28,"value":322},{"type":23,"tag":80,"props":962,"children":963},{"style":93},[964],{"type":28,"value":965}," \"\"\n",{"type":23,"tag":80,"props":967,"children":968},{"class":82,"line":311},[969,974,979,984],{"type":23,"tag":80,"props":970,"children":971},{"style":242},[972],{"type":28,"value":973},"    for",{"type":23,"tag":80,"props":975,"children":976},{"style":258},[977],{"type":28,"value":978}," table_name ",{"type":23,"tag":80,"props":980,"children":981},{"style":242},[982],{"type":28,"value":983},"in",{"type":23,"tag":80,"props":985,"children":986},{"style":258},[987],{"type":28,"value":988}," inspector.get_table_names():\n",{"type":23,"tag":80,"props":990,"children":991},{"class":82,"line":330},[992,997,1002,1007,1012,1017,1022,1027],{"type":23,"tag":80,"props":993,"children":994},{"style":258},[995],{"type":28,"value":996},"        schema_description ",{"type":23,"tag":80,"props":998,"children":999},{"style":242},[1000],{"type":28,"value":1001},"+=",{"type":23,"tag":80,"props":1003,"children":1004},{"style":242},[1005],{"type":28,"value":1006}," f",{"type":23,"tag":80,"props":1008,"children":1009},{"style":93},[1010],{"type":28,"value":1011},"\"Table: ",{"type":23,"tag":80,"props":1013,"children":1014},{"style":125},[1015],{"type":28,"value":1016},"{",{"type":23,"tag":80,"props":1018,"children":1019},{"style":258},[1020],{"type":28,"value":1021},"table_name",{"type":23,"tag":80,"props":1023,"children":1024},{"style":125},[1025],{"type":28,"value":1026},"}\\n",{"type":23,"tag":80,"props":1028,"children":1029},{"style":93},[1030],{"type":28,"value":1031},"\"\n",{"type":23,"tag":80,"props":1033,"children":1034},{"class":82,"line":348},[1035,1040,1045,1049],{"type":23,"tag":80,"props":1036,"children":1037},{"style":242},[1038],{"type":28,"value":1039},"        for",{"type":23,"tag":80,"props":1041,"children":1042},{"style":258},[1043],{"type":28,"value":1044}," column ",{"type":23,"tag":80,"props":1046,"children":1047},{"style":242},[1048],{"type":28,"value":983},{"type":23,"tag":80,"props":1050,"children":1051},{"style":258},[1052],{"type":28,"value":1053}," inspector.get_columns(table_name):\n",{"type":23,"tag":80,"props":1055,"children":1056},{"class":82,"line":376},[1057,1062,1066,1070,1075,1079,1084,1089,1094,1099,1104,1108,1112,1117,1121,1125,1129,1134],{"type":23,"tag":80,"props":1058,"children":1059},{"style":258},[1060],{"type":28,"value":1061},"            schema_description ",{"type":23,"tag":80,"props":1063,"children":1064},{"style":242},[1065],{"type":28,"value":1001},{"type":23,"tag":80,"props":1067,"children":1068},{"style":242},[1069],{"type":28,"value":1006},{"type":23,"tag":80,"props":1071,"children":1072},{"style":93},[1073],{"type":28,"value":1074},"\"- ",{"type":23,"tag":80,"props":1076,"children":1077},{"style":125},[1078],{"type":28,"value":1016},{"type":23,"tag":80,"props":1080,"children":1081},{"style":258},[1082],{"type":28,"value":1083},"column[",{"type":23,"tag":80,"props":1085,"children":1086},{"style":93},[1087],{"type":28,"value":1088},"'name'",{"type":23,"tag":80,"props":1090,"children":1091},{"style":258},[1092],{"type":28,"value":1093},"]",{"type":23,"tag":80,"props":1095,"children":1096},{"style":125},[1097],{"type":28,"value":1098},"}",{"type":23,"tag":80,"props":1100,"children":1101},{"style":93},[1102],{"type":28,"value":1103}," (",{"type":23,"tag":80,"props":1105,"children":1106},{"style":125},[1107],{"type":28,"value":1016},{"type":23,"tag":80,"props":1109,"children":1110},{"style":258},[1111],{"type":28,"value":1083},{"type":23,"tag":80,"props":1113,"children":1114},{"style":93},[1115],{"type":28,"value":1116},"'type'",{"type":23,"tag":80,"props":1118,"children":1119},{"style":258},[1120],{"type":28,"value":1093},{"type":23,"tag":80,"props":1122,"children":1123},{"style":125},[1124],{"type":28,"value":1098},{"type":23,"tag":80,"props":1126,"children":1127},{"style":93},[1128],{"type":28,"value":68},{"type":23,"tag":80,"props":1130,"children":1131},{"style":125},[1132],{"type":28,"value":1133},"\\n",{"type":23,"tag":80,"props":1135,"children":1136},{"style":93},[1137],{"type":28,"value":1031},{"type":23,"tag":80,"props":1139,"children":1140},{"class":82,"line":384},[1141,1145,1149,1154,1158],{"type":23,"tag":80,"props":1142,"children":1143},{"style":258},[1144],{"type":28,"value":996},{"type":23,"tag":80,"props":1146,"children":1147},{"style":242},[1148],{"type":28,"value":1001},{"type":23,"tag":80,"props":1150,"children":1151},{"style":93},[1152],{"type":28,"value":1153}," \"",{"type":23,"tag":80,"props":1155,"children":1156},{"style":125},[1157],{"type":28,"value":1133},{"type":23,"tag":80,"props":1159,"children":1160},{"style":93},[1161],{"type":28,"value":1031},{"type":23,"tag":80,"props":1163,"children":1164},{"class":82,"line":394},[1165,1170],{"type":23,"tag":80,"props":1166,"children":1167},{"style":242},[1168],{"type":28,"value":1169},"    return",{"type":23,"tag":80,"props":1171,"children":1172},{"style":258},[1173],{"type":28,"value":1174}," schema_description\n",{"type":23,"tag":80,"props":1176,"children":1177},{"class":82,"line":412},[1178],{"type":23,"tag":80,"props":1179,"children":1180},{"emptyLinePlaceholder":305},[1181],{"type":28,"value":308},{"type":23,"tag":80,"props":1183,"children":1184},{"class":82,"line":421},[1185,1190,1194],{"type":23,"tag":80,"props":1186,"children":1187},{"style":258},[1188],{"type":28,"value":1189},"schema_description ",{"type":23,"tag":80,"props":1191,"children":1192},{"style":242},[1193],{"type":28,"value":322},{"type":23,"tag":80,"props":1195,"children":1196},{"style":258},[1197],{"type":28,"value":1198}," get_database_schema(engine)\n",{"type":23,"tag":80,"props":1200,"children":1201},{"class":82,"line":429},[1202,1206],{"type":23,"tag":80,"props":1203,"children":1204},{"style":125},[1205],{"type":28,"value":471},{"type":23,"tag":80,"props":1207,"children":1208},{"style":258},[1209],{"type":28,"value":1210},"(schema_description)\n",{"type":23,"tag":40,"props":1212,"children":1214},{"id":1213},"paso-4-preparar-la-consulta-con-gpt",[1215],{"type":28,"value":1216},"Paso 4: Preparar la consulta con GPT",{"type":23,"tag":24,"props":1218,"children":1219},{},[1220],{"type":28,"value":1221},"Utiliza la API de OpenAI para transformar preguntas en consultas SQL, incluyendo el esquema de la base de datos:",{"type":23,"tag":70,"props":1223,"children":1225},{"className":231,"code":1224,"language":17,"meta":7,"style":7},"import openai\nimport os\n\nopenai.api_key = os.getenv(\"OPENAI_API_KEY\")\n\ndef generate_sql(question, schema):\n    response = openai.ChatCompletion.create(\n        model=\"gpt-4\",\n        messages=[\n            {\"role\": \"system\", \"content\": f\"Convierte preguntas en lenguaje natural a consultas SQL. Esquema de la base de datos:\\n{schema}\"},\n            {\"role\": \"user\", \"content\": question}\n        ]\n    )\n    sql_query = response.choices[0].message.content.strip()\n    return sql_query\n\nquery = generate_sql(\"¿Cuántos usuarios se registraron el último mes?\", schema_description)\nprint(query)\n",[1226],{"type":23,"tag":14,"props":1227,"children":1228},{"__ignoreMap":7},[1229,1241,1252,1259,1284,1291,1308,1325,1347,1364,1435,1468,1476,1484,1511,1523,1530,1557],{"type":23,"tag":80,"props":1230,"children":1231},{"class":82,"line":83},[1232,1236],{"type":23,"tag":80,"props":1233,"children":1234},{"style":242},[1235],{"type":28,"value":269},{"type":23,"tag":80,"props":1237,"children":1238},{"style":258},[1239],{"type":28,"value":1240}," openai\n",{"type":23,"tag":80,"props":1242,"children":1243},{"class":82,"line":141},[1244,1248],{"type":23,"tag":80,"props":1245,"children":1246},{"style":242},[1247],{"type":28,"value":269},{"type":23,"tag":80,"props":1249,"children":1250},{"style":258},[1251],{"type":28,"value":724},{"type":23,"tag":80,"props":1253,"children":1254},{"class":82,"line":155},[1255],{"type":23,"tag":80,"props":1256,"children":1257},{"emptyLinePlaceholder":305},[1258],{"type":28,"value":308},{"type":23,"tag":80,"props":1260,"children":1261},{"class":82,"line":216},[1262,1267,1271,1275,1280],{"type":23,"tag":80,"props":1263,"children":1264},{"style":258},[1265],{"type":28,"value":1266},"openai.api_key ",{"type":23,"tag":80,"props":1268,"children":1269},{"style":242},[1270],{"type":28,"value":322},{"type":23,"tag":80,"props":1272,"children":1273},{"style":258},[1274],{"type":28,"value":785},{"type":23,"tag":80,"props":1276,"children":1277},{"style":93},[1278],{"type":28,"value":1279},"\"OPENAI_API_KEY\"",{"type":23,"tag":80,"props":1281,"children":1282},{"style":258},[1283],{"type":28,"value":373},{"type":23,"tag":80,"props":1285,"children":1286},{"class":82,"line":301},[1287],{"type":23,"tag":80,"props":1288,"children":1289},{"emptyLinePlaceholder":305},[1290],{"type":28,"value":308},{"type":23,"tag":80,"props":1292,"children":1293},{"class":82,"line":311},[1294,1298,1303],{"type":23,"tag":80,"props":1295,"children":1296},{"style":242},[1297],{"type":28,"value":921},{"type":23,"tag":80,"props":1299,"children":1300},{"style":87},[1301],{"type":28,"value":1302}," generate_sql",{"type":23,"tag":80,"props":1304,"children":1305},{"style":258},[1306],{"type":28,"value":1307},"(question, schema):\n",{"type":23,"tag":80,"props":1309,"children":1310},{"class":82,"line":330},[1311,1316,1320],{"type":23,"tag":80,"props":1312,"children":1313},{"style":258},[1314],{"type":28,"value":1315},"    response ",{"type":23,"tag":80,"props":1317,"children":1318},{"style":242},[1319],{"type":28,"value":322},{"type":23,"tag":80,"props":1321,"children":1322},{"style":258},[1323],{"type":28,"value":1324}," openai.ChatCompletion.create(\n",{"type":23,"tag":80,"props":1326,"children":1327},{"class":82,"line":348},[1328,1333,1337,1342],{"type":23,"tag":80,"props":1329,"children":1330},{"style":839},[1331],{"type":28,"value":1332},"        model",{"type":23,"tag":80,"props":1334,"children":1335},{"style":242},[1336],{"type":28,"value":322},{"type":23,"tag":80,"props":1338,"children":1339},{"style":93},[1340],{"type":28,"value":1341},"\"gpt-4\"",{"type":23,"tag":80,"props":1343,"children":1344},{"style":258},[1345],{"type":28,"value":1346},",\n",{"type":23,"tag":80,"props":1348,"children":1349},{"class":82,"line":376},[1350,1355,1359],{"type":23,"tag":80,"props":1351,"children":1352},{"style":839},[1353],{"type":28,"value":1354},"        messages",{"type":23,"tag":80,"props":1356,"children":1357},{"style":242},[1358],{"type":28,"value":322},{"type":23,"tag":80,"props":1360,"children":1361},{"style":258},[1362],{"type":28,"value":1363},"[\n",{"type":23,"tag":80,"props":1365,"children":1366},{"class":82,"line":384},[1367,1372,1377,1382,1387,1392,1397,1401,1406,1411,1416,1421,1425,1430],{"type":23,"tag":80,"props":1368,"children":1369},{"style":258},[1370],{"type":28,"value":1371},"            {",{"type":23,"tag":80,"props":1373,"children":1374},{"style":93},[1375],{"type":28,"value":1376},"\"role\"",{"type":23,"tag":80,"props":1378,"children":1379},{"style":258},[1380],{"type":28,"value":1381},": ",{"type":23,"tag":80,"props":1383,"children":1384},{"style":93},[1385],{"type":28,"value":1386},"\"system\"",{"type":23,"tag":80,"props":1388,"children":1389},{"style":258},[1390],{"type":28,"value":1391},", ",{"type":23,"tag":80,"props":1393,"children":1394},{"style":93},[1395],{"type":28,"value":1396},"\"content\"",{"type":23,"tag":80,"props":1398,"children":1399},{"style":258},[1400],{"type":28,"value":1381},{"type":23,"tag":80,"props":1402,"children":1403},{"style":242},[1404],{"type":28,"value":1405},"f",{"type":23,"tag":80,"props":1407,"children":1408},{"style":93},[1409],{"type":28,"value":1410},"\"Convierte preguntas en lenguaje natural a consultas SQL. Esquema de la base de datos:",{"type":23,"tag":80,"props":1412,"children":1413},{"style":125},[1414],{"type":28,"value":1415},"\\n{",{"type":23,"tag":80,"props":1417,"children":1418},{"style":258},[1419],{"type":28,"value":1420},"schema",{"type":23,"tag":80,"props":1422,"children":1423},{"style":125},[1424],{"type":28,"value":1098},{"type":23,"tag":80,"props":1426,"children":1427},{"style":93},[1428],{"type":28,"value":1429},"\"",{"type":23,"tag":80,"props":1431,"children":1432},{"style":258},[1433],{"type":28,"value":1434},"},\n",{"type":23,"tag":80,"props":1436,"children":1437},{"class":82,"line":394},[1438,1442,1446,1450,1455,1459,1463],{"type":23,"tag":80,"props":1439,"children":1440},{"style":258},[1441],{"type":28,"value":1371},{"type":23,"tag":80,"props":1443,"children":1444},{"style":93},[1445],{"type":28,"value":1376},{"type":23,"tag":80,"props":1447,"children":1448},{"style":258},[1449],{"type":28,"value":1381},{"type":23,"tag":80,"props":1451,"children":1452},{"style":93},[1453],{"type":28,"value":1454},"\"user\"",{"type":23,"tag":80,"props":1456,"children":1457},{"style":258},[1458],{"type":28,"value":1391},{"type":23,"tag":80,"props":1460,"children":1461},{"style":93},[1462],{"type":28,"value":1396},{"type":23,"tag":80,"props":1464,"children":1465},{"style":258},[1466],{"type":28,"value":1467},": question}\n",{"type":23,"tag":80,"props":1469,"children":1470},{"class":82,"line":412},[1471],{"type":23,"tag":80,"props":1472,"children":1473},{"style":258},[1474],{"type":28,"value":1475},"        ]\n",{"type":23,"tag":80,"props":1477,"children":1478},{"class":82,"line":421},[1479],{"type":23,"tag":80,"props":1480,"children":1481},{"style":258},[1482],{"type":28,"value":1483},"    )\n",{"type":23,"tag":80,"props":1485,"children":1486},{"class":82,"line":429},[1487,1492,1496,1501,1506],{"type":23,"tag":80,"props":1488,"children":1489},{"style":258},[1490],{"type":28,"value":1491},"    sql_query ",{"type":23,"tag":80,"props":1493,"children":1494},{"style":242},[1495],{"type":28,"value":322},{"type":23,"tag":80,"props":1497,"children":1498},{"style":258},[1499],{"type":28,"value":1500}," response.choices[",{"type":23,"tag":80,"props":1502,"children":1503},{"style":125},[1504],{"type":28,"value":1505},"0",{"type":23,"tag":80,"props":1507,"children":1508},{"style":258},[1509],{"type":28,"value":1510},"].message.content.strip()\n",{"type":23,"tag":80,"props":1512,"children":1513},{"class":82,"line":438},[1514,1518],{"type":23,"tag":80,"props":1515,"children":1516},{"style":242},[1517],{"type":28,"value":1169},{"type":23,"tag":80,"props":1519,"children":1520},{"style":258},[1521],{"type":28,"value":1522}," sql_query\n",{"type":23,"tag":80,"props":1524,"children":1525},{"class":82,"line":456},[1526],{"type":23,"tag":80,"props":1527,"children":1528},{"emptyLinePlaceholder":305},[1529],{"type":28,"value":308},{"type":23,"tag":80,"props":1531,"children":1532},{"class":82,"line":465},[1533,1538,1542,1547,1552],{"type":23,"tag":80,"props":1534,"children":1535},{"style":258},[1536],{"type":28,"value":1537},"query ",{"type":23,"tag":80,"props":1539,"children":1540},{"style":242},[1541],{"type":28,"value":322},{"type":23,"tag":80,"props":1543,"children":1544},{"style":258},[1545],{"type":28,"value":1546}," generate_sql(",{"type":23,"tag":80,"props":1548,"children":1549},{"style":93},[1550],{"type":28,"value":1551},"\"¿Cuántos usuarios se registraron el último mes?\"",{"type":23,"tag":80,"props":1553,"children":1554},{"style":258},[1555],{"type":28,"value":1556},", schema_description)\n",{"type":23,"tag":80,"props":1558,"children":1560},{"class":82,"line":1559},18,[1561,1565],{"type":23,"tag":80,"props":1562,"children":1563},{"style":125},[1564],{"type":28,"value":471},{"type":23,"tag":80,"props":1566,"children":1567},{"style":258},[1568],{"type":28,"value":1569},"(query)\n",{"type":23,"tag":40,"props":1571,"children":1573},{"id":1572},"paso-5-ejecutar-consultas-generadas",[1574],{"type":28,"value":1575},"Paso 5: Ejecutar consultas generadas",{"type":23,"tag":24,"props":1577,"children":1578},{},[1579],{"type":28,"value":1580},"Ejecuta la consulta generada y obtiene resultados:",{"type":23,"tag":70,"props":1582,"children":1584},{"className":231,"code":1583,"language":17,"meta":7,"style":7},"result = session.execute(query)\nfor row in result:\n    print(row)\n",[1585],{"type":23,"tag":14,"props":1586,"children":1587},{"__ignoreMap":7},[1588,1605,1627],{"type":23,"tag":80,"props":1589,"children":1590},{"class":82,"line":83},[1591,1596,1600],{"type":23,"tag":80,"props":1592,"children":1593},{"style":258},[1594],{"type":28,"value":1595},"result ",{"type":23,"tag":80,"props":1597,"children":1598},{"style":242},[1599],{"type":28,"value":322},{"type":23,"tag":80,"props":1601,"children":1602},{"style":258},[1603],{"type":28,"value":1604}," session.execute(query)\n",{"type":23,"tag":80,"props":1606,"children":1607},{"class":82,"line":141},[1608,1613,1618,1622],{"type":23,"tag":80,"props":1609,"children":1610},{"style":242},[1611],{"type":28,"value":1612},"for",{"type":23,"tag":80,"props":1614,"children":1615},{"style":258},[1616],{"type":28,"value":1617}," row ",{"type":23,"tag":80,"props":1619,"children":1620},{"style":242},[1621],{"type":28,"value":983},{"type":23,"tag":80,"props":1623,"children":1624},{"style":258},[1625],{"type":28,"value":1626}," result:\n",{"type":23,"tag":80,"props":1628,"children":1629},{"class":82,"line":155},[1630,1635],{"type":23,"tag":80,"props":1631,"children":1632},{"style":125},[1633],{"type":28,"value":1634},"    print",{"type":23,"tag":80,"props":1636,"children":1637},{"style":258},[1638],{"type":28,"value":1639},"(row)\n",{"type":23,"tag":40,"props":1641,"children":1643},{"id":1642},"paso-6-manejo-de-errores-y-seguridad",[1644],{"type":28,"value":1645},"Paso 6: Manejo de errores y seguridad",{"type":23,"tag":24,"props":1647,"children":1648},{},[1649],{"type":28,"value":1650},"Para evitar problemas con consultas inseguras o errores, siempre valida las consultas generadas por GPT antes de ejecutarlas directamente:",{"type":23,"tag":70,"props":1652,"children":1654},{"className":231,"code":1653,"language":17,"meta":7,"style":7},"def execute_safe_query(sql_query):\n    try:\n        result = session.execute(sql_query)\n        return result.fetchall()\n    except Exception as e:\n        print(f\"Error al ejecutar consulta: {e}\")\n        return None\n\nresults = execute_safe_query(query)\nprint(results)\n",[1655],{"type":23,"tag":14,"props":1656,"children":1657},{"__ignoreMap":7},[1658,1675,1688,1705,1718,1741,1784,1796,1803,1820],{"type":23,"tag":80,"props":1659,"children":1660},{"class":82,"line":83},[1661,1665,1670],{"type":23,"tag":80,"props":1662,"children":1663},{"style":242},[1664],{"type":28,"value":921},{"type":23,"tag":80,"props":1666,"children":1667},{"style":87},[1668],{"type":28,"value":1669}," execute_safe_query",{"type":23,"tag":80,"props":1671,"children":1672},{"style":258},[1673],{"type":28,"value":1674},"(sql_query):\n",{"type":23,"tag":80,"props":1676,"children":1677},{"class":82,"line":141},[1678,1683],{"type":23,"tag":80,"props":1679,"children":1680},{"style":242},[1681],{"type":28,"value":1682},"    try",{"type":23,"tag":80,"props":1684,"children":1685},{"style":258},[1686],{"type":28,"value":1687},":\n",{"type":23,"tag":80,"props":1689,"children":1690},{"class":82,"line":155},[1691,1696,1700],{"type":23,"tag":80,"props":1692,"children":1693},{"style":258},[1694],{"type":28,"value":1695},"        result ",{"type":23,"tag":80,"props":1697,"children":1698},{"style":242},[1699],{"type":28,"value":322},{"type":23,"tag":80,"props":1701,"children":1702},{"style":258},[1703],{"type":28,"value":1704}," session.execute(sql_query)\n",{"type":23,"tag":80,"props":1706,"children":1707},{"class":82,"line":216},[1708,1713],{"type":23,"tag":80,"props":1709,"children":1710},{"style":242},[1711],{"type":28,"value":1712},"        return",{"type":23,"tag":80,"props":1714,"children":1715},{"style":258},[1716],{"type":28,"value":1717}," result.fetchall()\n",{"type":23,"tag":80,"props":1719,"children":1720},{"class":82,"line":301},[1721,1726,1731,1736],{"type":23,"tag":80,"props":1722,"children":1723},{"style":242},[1724],{"type":28,"value":1725},"    except",{"type":23,"tag":80,"props":1727,"children":1728},{"style":125},[1729],{"type":28,"value":1730}," Exception",{"type":23,"tag":80,"props":1732,"children":1733},{"style":242},[1734],{"type":28,"value":1735}," as",{"type":23,"tag":80,"props":1737,"children":1738},{"style":258},[1739],{"type":28,"value":1740}," e:\n",{"type":23,"tag":80,"props":1742,"children":1743},{"class":82,"line":311},[1744,1749,1754,1758,1763,1767,1772,1776,1780],{"type":23,"tag":80,"props":1745,"children":1746},{"style":125},[1747],{"type":28,"value":1748},"        print",{"type":23,"tag":80,"props":1750,"children":1751},{"style":258},[1752],{"type":28,"value":1753},"(",{"type":23,"tag":80,"props":1755,"children":1756},{"style":242},[1757],{"type":28,"value":1405},{"type":23,"tag":80,"props":1759,"children":1760},{"style":93},[1761],{"type":28,"value":1762},"\"Error al ejecutar consulta: ",{"type":23,"tag":80,"props":1764,"children":1765},{"style":125},[1766],{"type":28,"value":1016},{"type":23,"tag":80,"props":1768,"children":1769},{"style":258},[1770],{"type":28,"value":1771},"e",{"type":23,"tag":80,"props":1773,"children":1774},{"style":125},[1775],{"type":28,"value":1098},{"type":23,"tag":80,"props":1777,"children":1778},{"style":93},[1779],{"type":28,"value":1429},{"type":23,"tag":80,"props":1781,"children":1782},{"style":258},[1783],{"type":28,"value":373},{"type":23,"tag":80,"props":1785,"children":1786},{"class":82,"line":330},[1787,1791],{"type":23,"tag":80,"props":1788,"children":1789},{"style":242},[1790],{"type":28,"value":1712},{"type":23,"tag":80,"props":1792,"children":1793},{"style":125},[1794],{"type":28,"value":1795}," None\n",{"type":23,"tag":80,"props":1797,"children":1798},{"class":82,"line":348},[1799],{"type":23,"tag":80,"props":1800,"children":1801},{"emptyLinePlaceholder":305},[1802],{"type":28,"value":308},{"type":23,"tag":80,"props":1804,"children":1805},{"class":82,"line":376},[1806,1811,1815],{"type":23,"tag":80,"props":1807,"children":1808},{"style":258},[1809],{"type":28,"value":1810},"results ",{"type":23,"tag":80,"props":1812,"children":1813},{"style":242},[1814],{"type":28,"value":322},{"type":23,"tag":80,"props":1816,"children":1817},{"style":258},[1818],{"type":28,"value":1819}," execute_safe_query(query)\n",{"type":23,"tag":80,"props":1821,"children":1822},{"class":82,"line":384},[1823,1827],{"type":23,"tag":80,"props":1824,"children":1825},{"style":125},[1826],{"type":28,"value":471},{"type":23,"tag":80,"props":1828,"children":1829},{"style":258},[1830],{"type":28,"value":1831},"(results)\n",{"type":23,"tag":40,"props":1833,"children":1835},{"id":1834},"buenas-prácticas",[1836],{"type":28,"value":1837},"Buenas prácticas",{"type":23,"tag":496,"props":1839,"children":1840},{},[1841,1846,1851],{"type":23,"tag":500,"props":1842,"children":1843},{},[1844],{"type":28,"value":1845},"Siempre revisa o limita el acceso de los usuarios a consultas críticas o sensibles.",{"type":23,"tag":500,"props":1847,"children":1848},{},[1849],{"type":28,"value":1850},"Implementa validaciones adicionales para asegurar la integridad y seguridad de tu base de datos.",{"type":23,"tag":500,"props":1852,"children":1853},{},[1854],{"type":28,"value":1855},"Utiliza tokens y modelos específicos de OpenAI optimizados para tareas SQL.",{"type":23,"tag":40,"props":1857,"children":1859},{"id":1858},"conclusión",[1860],{"type":28,"value":1861},"Conclusión",{"type":23,"tag":24,"props":1863,"children":1864},{},[1865],{"type":28,"value":1866},"Utilizar GPT para transformar consultas en lenguaje natural a SQL facilita enormemente la accesibilidad a los datos. Implementando este sistema, podrás ofrecer una experiencia de usuario más amigable y eficiente.",{"type":23,"tag":1868,"props":1869,"children":1871},"h1",{"id":1870},"recursos-adicionales",[1872],{"type":28,"value":1873},"Recursos adicionales",{"type":23,"tag":496,"props":1875,"children":1876},{},[1877,1887,1897],{"type":23,"tag":500,"props":1878,"children":1879},{},[1880],{"type":23,"tag":31,"props":1881,"children":1884},{"href":1882,"rel":1883},"https://platform.openai.com/docs/api-reference",[63],[1885],{"type":28,"value":1886},"Documentación API OpenAI",{"type":23,"tag":500,"props":1888,"children":1889},{},[1890],{"type":23,"tag":31,"props":1891,"children":1894},{"href":1892,"rel":1893},"https://docs.sqlalchemy.org/en/20/",[63],[1895],{"type":28,"value":1896},"SQLAlchemy ORM Documentation",{"type":23,"tag":500,"props":1898,"children":1899},{},[1900],{"type":23,"tag":31,"props":1901,"children":1904},{"href":1902,"rel":1903},"https://pypi.org/project/python-dotenv/",[63],[1905],{"type":28,"value":1906},"Python Dotenv",{"type":23,"tag":510,"props":1908,"children":1909},{},[1910],{"type":28,"value":514},{"title":7,"searchDepth":141,"depth":141,"links":1912},[1913,1914,1915,1916,1917,1918,1919,1920,1921],{"id":553,"depth":141,"text":556},{"id":577,"depth":141,"text":580},{"id":655,"depth":141,"text":658},{"id":871,"depth":141,"text":874},{"id":1213,"depth":141,"text":1216},{"id":1572,"depth":141,"text":1575},{"id":1642,"depth":141,"text":1645},{"id":1834,"depth":141,"text":1837},{"id":1858,"depth":141,"text":1861},"content:posts:gpt-sql-integration.md","posts/gpt-sql-integration.md","posts/gpt-sql-integration",{"_path":1926,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":1927,"description":1928,"date":1929,"layout":11,"subtitle":1930,"image":1931,"optimized_image":1931,"category":14,"tags":1932,"author":18,"paginate":6,"body":1937,"_type":520,"_id":2831,"_source":522,"_file":2832,"_stem":2833,"_extension":525},"/posts/java-security-best-practices","Java Security Best Practices: Preventing Vulnerabilities and Threats","Explore essential security strategies for Java applications, addressing common vulnerabilities identified by OWASP Top 10 with practical examples.","2024-08-15T11:00:00.000Z","Practical Strategies to Safeguard Your Java Applications","/assets/img/uploads/java-security-best-practices.png",[14,1933,1934,1935,1936],"java","security","owasp","cybersecurity",{"type":20,"children":1938,"toc":2819},[1939,1947,1953,1958,2012,2017,2023,2028,2151,2156,2162,2167,2269,2275,2280,2403,2408,2414,2419,2444,2449,2455,2460,2476,2481,2487,2492,2575,2580,2586,2591,2711,2716,2722,2727,2760,2765,2771,2776,2782,2815],{"type":23,"tag":543,"props":1940,"children":1941},{},[1942],{"type":23,"tag":24,"props":1943,"children":1944},{},[1945],{"type":28,"value":1946},"Security is critical in software development. Java applications, like all software, face numerous potential vulnerabilities. In this article, we will explore essential security best practices, referencing the OWASP Top 10, and providing practical Java examples to help you protect your applications effectively.",{"type":23,"tag":40,"props":1948,"children":1950},{"id":1949},"understanding-owasp-top-10",[1951],{"type":28,"value":1952},"Understanding OWASP Top 10",{"type":23,"tag":24,"props":1954,"children":1955},{},[1956],{"type":28,"value":1957},"The OWASP Top 10 outlines the most critical web application security risks:",{"type":23,"tag":1959,"props":1960,"children":1961},"ol",{},[1962,1967,1972,1977,1982,1987,1992,1997,2002,2007],{"type":23,"tag":500,"props":1963,"children":1964},{},[1965],{"type":28,"value":1966},"Injection",{"type":23,"tag":500,"props":1968,"children":1969},{},[1970],{"type":28,"value":1971},"Broken Authentication",{"type":23,"tag":500,"props":1973,"children":1974},{},[1975],{"type":28,"value":1976},"Sensitive Data Exposure",{"type":23,"tag":500,"props":1978,"children":1979},{},[1980],{"type":28,"value":1981},"XML External Entities (XXE)",{"type":23,"tag":500,"props":1983,"children":1984},{},[1985],{"type":28,"value":1986},"Broken Access Control",{"type":23,"tag":500,"props":1988,"children":1989},{},[1990],{"type":28,"value":1991},"Security Misconfiguration",{"type":23,"tag":500,"props":1993,"children":1994},{},[1995],{"type":28,"value":1996},"Cross-Site Scripting (XSS)",{"type":23,"tag":500,"props":1998,"children":1999},{},[2000],{"type":28,"value":2001},"Insecure Deserialization",{"type":23,"tag":500,"props":2003,"children":2004},{},[2005],{"type":28,"value":2006},"Using Components with Known Vulnerabilities",{"type":23,"tag":500,"props":2008,"children":2009},{},[2010],{"type":28,"value":2011},"Insufficient Logging and Monitoring",{"type":23,"tag":24,"props":2013,"children":2014},{},[2015],{"type":28,"value":2016},"Let's delve into practical strategies and Java implementations for some of these threats.",{"type":23,"tag":40,"props":2018,"children":2020},{"id":2019},"_1-injection-prevention",[2021],{"type":28,"value":2022},"1. Injection Prevention",{"type":23,"tag":24,"props":2024,"children":2025},{},[2026],{"type":28,"value":2027},"Use Prepared Statements and parameterized queries:",{"type":23,"tag":70,"props":2029,"children":2032},{"className":2030,"code":2031,"language":1933,"meta":7,"style":7},"language-java shiki shiki-themes github-dark github-light","PreparedStatement stmt = connection.prepareStatement(\"SELECT * FROM users WHERE username=? AND password=?\");\nstmt.setString(1, username);\nstmt.setString(2, password);\nResultSet rs = stmt.executeQuery();\n",[2033],{"type":23,"tag":14,"props":2034,"children":2035},{"__ignoreMap":7},[2036,2072,2099,2124],{"type":23,"tag":80,"props":2037,"children":2038},{"class":82,"line":83},[2039,2044,2048,2053,2058,2062,2067],{"type":23,"tag":80,"props":2040,"children":2041},{"style":258},[2042],{"type":28,"value":2043},"PreparedStatement stmt ",{"type":23,"tag":80,"props":2045,"children":2046},{"style":242},[2047],{"type":28,"value":322},{"type":23,"tag":80,"props":2049,"children":2050},{"style":258},[2051],{"type":28,"value":2052}," connection.",{"type":23,"tag":80,"props":2054,"children":2055},{"style":87},[2056],{"type":28,"value":2057},"prepareStatement",{"type":23,"tag":80,"props":2059,"children":2060},{"style":258},[2061],{"type":28,"value":1753},{"type":23,"tag":80,"props":2063,"children":2064},{"style":93},[2065],{"type":28,"value":2066},"\"SELECT * FROM users WHERE username=? AND password=?\"",{"type":23,"tag":80,"props":2068,"children":2069},{"style":258},[2070],{"type":28,"value":2071},");\n",{"type":23,"tag":80,"props":2073,"children":2074},{"class":82,"line":141},[2075,2080,2085,2089,2094],{"type":23,"tag":80,"props":2076,"children":2077},{"style":258},[2078],{"type":28,"value":2079},"stmt.",{"type":23,"tag":80,"props":2081,"children":2082},{"style":87},[2083],{"type":28,"value":2084},"setString",{"type":23,"tag":80,"props":2086,"children":2087},{"style":258},[2088],{"type":28,"value":1753},{"type":23,"tag":80,"props":2090,"children":2091},{"style":125},[2092],{"type":28,"value":2093},"1",{"type":23,"tag":80,"props":2095,"children":2096},{"style":258},[2097],{"type":28,"value":2098},", username);\n",{"type":23,"tag":80,"props":2100,"children":2101},{"class":82,"line":155},[2102,2106,2110,2114,2119],{"type":23,"tag":80,"props":2103,"children":2104},{"style":258},[2105],{"type":28,"value":2079},{"type":23,"tag":80,"props":2107,"children":2108},{"style":87},[2109],{"type":28,"value":2084},{"type":23,"tag":80,"props":2111,"children":2112},{"style":258},[2113],{"type":28,"value":1753},{"type":23,"tag":80,"props":2115,"children":2116},{"style":125},[2117],{"type":28,"value":2118},"2",{"type":23,"tag":80,"props":2120,"children":2121},{"style":258},[2122],{"type":28,"value":2123},", password);\n",{"type":23,"tag":80,"props":2125,"children":2126},{"class":82,"line":216},[2127,2132,2136,2141,2146],{"type":23,"tag":80,"props":2128,"children":2129},{"style":258},[2130],{"type":28,"value":2131},"ResultSet rs ",{"type":23,"tag":80,"props":2133,"children":2134},{"style":242},[2135],{"type":28,"value":322},{"type":23,"tag":80,"props":2137,"children":2138},{"style":258},[2139],{"type":28,"value":2140}," stmt.",{"type":23,"tag":80,"props":2142,"children":2143},{"style":87},[2144],{"type":28,"value":2145},"executeQuery",{"type":23,"tag":80,"props":2147,"children":2148},{"style":258},[2149],{"type":28,"value":2150},"();\n",{"type":23,"tag":24,"props":2152,"children":2153},{},[2154],{"type":28,"value":2155},"This approach prevents SQL injection by ensuring that input data is treated as data, not executable code.",{"type":23,"tag":40,"props":2157,"children":2159},{"id":2158},"_2-secure-authentication",[2160],{"type":28,"value":2161},"2. Secure Authentication",{"type":23,"tag":24,"props":2163,"children":2164},{},[2165],{"type":28,"value":2166},"Implement robust authentication using secure password storage (bcrypt or Argon2):",{"type":23,"tag":70,"props":2168,"children":2170},{"className":2030,"code":2169,"language":1933,"meta":7,"style":7},"import org.springframework.security.crypto.bcrypt.BCrypt;\n\nString hashedPassword = BCrypt.hashpw(password, BCrypt.gensalt());\nif (BCrypt.checkpw(password, hashedPassword)) {\n    // authentication successful\n}\n",[2171],{"type":23,"tag":14,"props":2172,"children":2173},{"__ignoreMap":7},[2174,2186,2193,2230,2253,2261],{"type":23,"tag":80,"props":2175,"children":2176},{"class":82,"line":83},[2177,2181],{"type":23,"tag":80,"props":2178,"children":2179},{"style":242},[2180],{"type":28,"value":269},{"type":23,"tag":80,"props":2182,"children":2183},{"style":258},[2184],{"type":28,"value":2185}," org.springframework.security.crypto.bcrypt.BCrypt;\n",{"type":23,"tag":80,"props":2187,"children":2188},{"class":82,"line":141},[2189],{"type":23,"tag":80,"props":2190,"children":2191},{"emptyLinePlaceholder":305},[2192],{"type":28,"value":308},{"type":23,"tag":80,"props":2194,"children":2195},{"class":82,"line":155},[2196,2201,2205,2210,2215,2220,2225],{"type":23,"tag":80,"props":2197,"children":2198},{"style":258},[2199],{"type":28,"value":2200},"String hashedPassword ",{"type":23,"tag":80,"props":2202,"children":2203},{"style":242},[2204],{"type":28,"value":322},{"type":23,"tag":80,"props":2206,"children":2207},{"style":258},[2208],{"type":28,"value":2209}," BCrypt.",{"type":23,"tag":80,"props":2211,"children":2212},{"style":87},[2213],{"type":28,"value":2214},"hashpw",{"type":23,"tag":80,"props":2216,"children":2217},{"style":258},[2218],{"type":28,"value":2219},"(password, BCrypt.",{"type":23,"tag":80,"props":2221,"children":2222},{"style":87},[2223],{"type":28,"value":2224},"gensalt",{"type":23,"tag":80,"props":2226,"children":2227},{"style":258},[2228],{"type":28,"value":2229},"());\n",{"type":23,"tag":80,"props":2231,"children":2232},{"class":82,"line":216},[2233,2238,2243,2248],{"type":23,"tag":80,"props":2234,"children":2235},{"style":242},[2236],{"type":28,"value":2237},"if",{"type":23,"tag":80,"props":2239,"children":2240},{"style":258},[2241],{"type":28,"value":2242}," (BCrypt.",{"type":23,"tag":80,"props":2244,"children":2245},{"style":87},[2246],{"type":28,"value":2247},"checkpw",{"type":23,"tag":80,"props":2249,"children":2250},{"style":258},[2251],{"type":28,"value":2252},"(password, hashedPassword)) {\n",{"type":23,"tag":80,"props":2254,"children":2255},{"class":82,"line":301},[2256],{"type":23,"tag":80,"props":2257,"children":2258},{"style":388},[2259],{"type":28,"value":2260},"    // authentication successful\n",{"type":23,"tag":80,"props":2262,"children":2263},{"class":82,"line":311},[2264],{"type":23,"tag":80,"props":2265,"children":2266},{"style":258},[2267],{"type":28,"value":2268},"}\n",{"type":23,"tag":40,"props":2270,"children":2272},{"id":2271},"_3-sensitive-data-exposure",[2273],{"type":28,"value":2274},"3. Sensitive Data Exposure",{"type":23,"tag":24,"props":2276,"children":2277},{},[2278],{"type":28,"value":2279},"Use encryption for sensitive data storage:",{"type":23,"tag":70,"props":2281,"children":2283},{"className":2030,"code":2282,"language":1933,"meta":7,"style":7},"Cipher cipher = Cipher.getInstance(\"AES/GCM/NoPadding\");\nSecretKey key = generateSecretKey();\ncipher.init(Cipher.ENCRYPT_MODE, key);\nbyte[] encryptedData = cipher.doFinal(plainData.getBytes(StandardCharsets.UTF_8));\n",[2284],{"type":23,"tag":14,"props":2285,"children":2286},{"__ignoreMap":7},[2287,2322,2343,2361],{"type":23,"tag":80,"props":2288,"children":2289},{"class":82,"line":83},[2290,2295,2299,2304,2309,2313,2318],{"type":23,"tag":80,"props":2291,"children":2292},{"style":258},[2293],{"type":28,"value":2294},"Cipher cipher ",{"type":23,"tag":80,"props":2296,"children":2297},{"style":242},[2298],{"type":28,"value":322},{"type":23,"tag":80,"props":2300,"children":2301},{"style":258},[2302],{"type":28,"value":2303}," Cipher.",{"type":23,"tag":80,"props":2305,"children":2306},{"style":87},[2307],{"type":28,"value":2308},"getInstance",{"type":23,"tag":80,"props":2310,"children":2311},{"style":258},[2312],{"type":28,"value":1753},{"type":23,"tag":80,"props":2314,"children":2315},{"style":93},[2316],{"type":28,"value":2317},"\"AES/GCM/NoPadding\"",{"type":23,"tag":80,"props":2319,"children":2320},{"style":258},[2321],{"type":28,"value":2071},{"type":23,"tag":80,"props":2323,"children":2324},{"class":82,"line":141},[2325,2330,2334,2339],{"type":23,"tag":80,"props":2326,"children":2327},{"style":258},[2328],{"type":28,"value":2329},"SecretKey key ",{"type":23,"tag":80,"props":2331,"children":2332},{"style":242},[2333],{"type":28,"value":322},{"type":23,"tag":80,"props":2335,"children":2336},{"style":87},[2337],{"type":28,"value":2338}," generateSecretKey",{"type":23,"tag":80,"props":2340,"children":2341},{"style":258},[2342],{"type":28,"value":2150},{"type":23,"tag":80,"props":2344,"children":2345},{"class":82,"line":155},[2346,2351,2356],{"type":23,"tag":80,"props":2347,"children":2348},{"style":258},[2349],{"type":28,"value":2350},"cipher.",{"type":23,"tag":80,"props":2352,"children":2353},{"style":87},[2354],{"type":28,"value":2355},"init",{"type":23,"tag":80,"props":2357,"children":2358},{"style":258},[2359],{"type":28,"value":2360},"(Cipher.ENCRYPT_MODE, key);\n",{"type":23,"tag":80,"props":2362,"children":2363},{"class":82,"line":216},[2364,2369,2374,2378,2383,2388,2393,2398],{"type":23,"tag":80,"props":2365,"children":2366},{"style":242},[2367],{"type":28,"value":2368},"byte",{"type":23,"tag":80,"props":2370,"children":2371},{"style":258},[2372],{"type":28,"value":2373},"[] encryptedData ",{"type":23,"tag":80,"props":2375,"children":2376},{"style":242},[2377],{"type":28,"value":322},{"type":23,"tag":80,"props":2379,"children":2380},{"style":258},[2381],{"type":28,"value":2382}," cipher.",{"type":23,"tag":80,"props":2384,"children":2385},{"style":87},[2386],{"type":28,"value":2387},"doFinal",{"type":23,"tag":80,"props":2389,"children":2390},{"style":258},[2391],{"type":28,"value":2392},"(plainData.",{"type":23,"tag":80,"props":2394,"children":2395},{"style":87},[2396],{"type":28,"value":2397},"getBytes",{"type":23,"tag":80,"props":2399,"children":2400},{"style":258},[2401],{"type":28,"value":2402},"(StandardCharsets.UTF_8));\n",{"type":23,"tag":24,"props":2404,"children":2405},{},[2406],{"type":28,"value":2407},"Always encrypt data at rest and in transit (use HTTPS).",{"type":23,"tag":40,"props":2409,"children":2411},{"id":2410},"_4-security-misconfiguration",[2412],{"type":28,"value":2413},"4. Security Misconfiguration",{"type":23,"tag":24,"props":2415,"children":2416},{},[2417],{"type":28,"value":2418},"Adopt secure defaults, disable debugging, and avoid detailed error messages:",{"type":23,"tag":70,"props":2420,"children":2424},{"className":2421,"code":2422,"language":2423,"meta":7,"style":7},"language-properties shiki shiki-themes github-dark github-light","server.error.include-stacktrace=never\nserver.error.include-message=never\n","properties",[2425],{"type":23,"tag":14,"props":2426,"children":2427},{"__ignoreMap":7},[2428,2436],{"type":23,"tag":80,"props":2429,"children":2430},{"class":82,"line":83},[2431],{"type":23,"tag":80,"props":2432,"children":2433},{},[2434],{"type":28,"value":2435},"server.error.include-stacktrace=never\n",{"type":23,"tag":80,"props":2437,"children":2438},{"class":82,"line":141},[2439],{"type":23,"tag":80,"props":2440,"children":2441},{},[2442],{"type":28,"value":2443},"server.error.include-message=never\n",{"type":23,"tag":24,"props":2445,"children":2446},{},[2447],{"type":28,"value":2448},"Review and secure your Java application configuration regularly.",{"type":23,"tag":40,"props":2450,"children":2452},{"id":2451},"_5-cross-site-scripting-xss",[2453],{"type":28,"value":2454},"5. Cross-Site Scripting (XSS)",{"type":23,"tag":24,"props":2456,"children":2457},{},[2458],{"type":28,"value":2459},"Utilize proper encoding or frameworks that prevent XSS by default, such as Thymeleaf:",{"type":23,"tag":70,"props":2461,"children":2465},{"className":2462,"code":2463,"language":2464,"meta":7,"style":7},"language-html shiki shiki-themes github-dark github-light","\u003Cp th:text=\"${userInput}\">\u003C/p>\n","html",[2466],{"type":23,"tag":14,"props":2467,"children":2468},{"__ignoreMap":7},[2469],{"type":23,"tag":80,"props":2470,"children":2471},{"class":82,"line":83},[2472],{"type":23,"tag":80,"props":2473,"children":2474},{},[2475],{"type":28,"value":2463},{"type":23,"tag":24,"props":2477,"children":2478},{},[2479],{"type":28,"value":2480},"Avoid directly inserting user inputs into HTML without proper sanitization or encoding.",{"type":23,"tag":40,"props":2482,"children":2484},{"id":2483},"_6-insecure-deserialization",[2485],{"type":28,"value":2486},"6. Insecure Deserialization",{"type":23,"tag":24,"props":2488,"children":2489},{},[2490],{"type":28,"value":2491},"Limit deserialization to trusted sources only:",{"type":23,"tag":70,"props":2493,"children":2495},{"className":2030,"code":2494,"language":1933,"meta":7,"style":7},"ObjectInputStream ois = new ObjectInputStream(new FileInputStream(\"trusted_data.ser\"));\nMyObject obj = (MyObject) ois.readObject();\n",[2496],{"type":23,"tag":14,"props":2497,"children":2498},{"__ignoreMap":7},[2499,2549],{"type":23,"tag":80,"props":2500,"children":2501},{"class":82,"line":83},[2502,2507,2511,2516,2521,2525,2530,2535,2539,2544],{"type":23,"tag":80,"props":2503,"children":2504},{"style":258},[2505],{"type":28,"value":2506},"ObjectInputStream ois ",{"type":23,"tag":80,"props":2508,"children":2509},{"style":242},[2510],{"type":28,"value":322},{"type":23,"tag":80,"props":2512,"children":2513},{"style":242},[2514],{"type":28,"value":2515}," new",{"type":23,"tag":80,"props":2517,"children":2518},{"style":87},[2519],{"type":28,"value":2520}," ObjectInputStream",{"type":23,"tag":80,"props":2522,"children":2523},{"style":258},[2524],{"type":28,"value":1753},{"type":23,"tag":80,"props":2526,"children":2527},{"style":242},[2528],{"type":28,"value":2529},"new",{"type":23,"tag":80,"props":2531,"children":2532},{"style":87},[2533],{"type":28,"value":2534}," FileInputStream",{"type":23,"tag":80,"props":2536,"children":2537},{"style":258},[2538],{"type":28,"value":1753},{"type":23,"tag":80,"props":2540,"children":2541},{"style":93},[2542],{"type":28,"value":2543},"\"trusted_data.ser\"",{"type":23,"tag":80,"props":2545,"children":2546},{"style":258},[2547],{"type":28,"value":2548},"));\n",{"type":23,"tag":80,"props":2550,"children":2551},{"class":82,"line":141},[2552,2557,2561,2566,2571],{"type":23,"tag":80,"props":2553,"children":2554},{"style":258},[2555],{"type":28,"value":2556},"MyObject obj ",{"type":23,"tag":80,"props":2558,"children":2559},{"style":242},[2560],{"type":28,"value":322},{"type":23,"tag":80,"props":2562,"children":2563},{"style":258},[2564],{"type":28,"value":2565}," (MyObject) ois.",{"type":23,"tag":80,"props":2567,"children":2568},{"style":87},[2569],{"type":28,"value":2570},"readObject",{"type":23,"tag":80,"props":2572,"children":2573},{"style":258},[2574],{"type":28,"value":2150},{"type":23,"tag":24,"props":2576,"children":2577},{},[2578],{"type":28,"value":2579},"Always validate deserialized data.",{"type":23,"tag":40,"props":2581,"children":2583},{"id":2582},"_7-component-vulnerabilities",[2584],{"type":28,"value":2585},"7. Component Vulnerabilities",{"type":23,"tag":24,"props":2587,"children":2588},{},[2589],{"type":28,"value":2590},"Regularly update libraries and dependencies, and use tools like OWASP Dependency-Check to identify vulnerabilities:",{"type":23,"tag":70,"props":2592,"children":2596},{"className":2593,"code":2594,"language":2595,"meta":7,"style":7},"language-xml shiki shiki-themes github-dark github-light","\u003Cplugin>\n    \u003CgroupId>org.owasp\u003C/groupId>\n    \u003CartifactId>dependency-check-maven\u003C/artifactId>\n    \u003Cversion>9.0.9\u003C/version>\n\u003C/plugin>\n","xml",[2597],{"type":23,"tag":14,"props":2598,"children":2599},{"__ignoreMap":7},[2600,2619,2645,2670,2695],{"type":23,"tag":80,"props":2601,"children":2602},{"class":82,"line":83},[2603,2608,2614],{"type":23,"tag":80,"props":2604,"children":2605},{"style":258},[2606],{"type":28,"value":2607},"\u003C",{"type":23,"tag":80,"props":2609,"children":2611},{"style":2610},"--shiki-default:#85E89D;--shiki-light:#22863A",[2612],{"type":28,"value":2613},"plugin",{"type":23,"tag":80,"props":2615,"children":2616},{"style":258},[2617],{"type":28,"value":2618},">\n",{"type":23,"tag":80,"props":2620,"children":2621},{"class":82,"line":141},[2622,2627,2632,2637,2641],{"type":23,"tag":80,"props":2623,"children":2624},{"style":258},[2625],{"type":28,"value":2626},"    \u003C",{"type":23,"tag":80,"props":2628,"children":2629},{"style":2610},[2630],{"type":28,"value":2631},"groupId",{"type":23,"tag":80,"props":2633,"children":2634},{"style":258},[2635],{"type":28,"value":2636},">org.owasp\u003C/",{"type":23,"tag":80,"props":2638,"children":2639},{"style":2610},[2640],{"type":28,"value":2631},{"type":23,"tag":80,"props":2642,"children":2643},{"style":258},[2644],{"type":28,"value":2618},{"type":23,"tag":80,"props":2646,"children":2647},{"class":82,"line":155},[2648,2652,2657,2662,2666],{"type":23,"tag":80,"props":2649,"children":2650},{"style":258},[2651],{"type":28,"value":2626},{"type":23,"tag":80,"props":2653,"children":2654},{"style":2610},[2655],{"type":28,"value":2656},"artifactId",{"type":23,"tag":80,"props":2658,"children":2659},{"style":258},[2660],{"type":28,"value":2661},">dependency-check-maven\u003C/",{"type":23,"tag":80,"props":2663,"children":2664},{"style":2610},[2665],{"type":28,"value":2656},{"type":23,"tag":80,"props":2667,"children":2668},{"style":258},[2669],{"type":28,"value":2618},{"type":23,"tag":80,"props":2671,"children":2672},{"class":82,"line":216},[2673,2677,2682,2687,2691],{"type":23,"tag":80,"props":2674,"children":2675},{"style":258},[2676],{"type":28,"value":2626},{"type":23,"tag":80,"props":2678,"children":2679},{"style":2610},[2680],{"type":28,"value":2681},"version",{"type":23,"tag":80,"props":2683,"children":2684},{"style":258},[2685],{"type":28,"value":2686},">9.0.9\u003C/",{"type":23,"tag":80,"props":2688,"children":2689},{"style":2610},[2690],{"type":28,"value":2681},{"type":23,"tag":80,"props":2692,"children":2693},{"style":258},[2694],{"type":28,"value":2618},{"type":23,"tag":80,"props":2696,"children":2697},{"class":82,"line":301},[2698,2703,2707],{"type":23,"tag":80,"props":2699,"children":2700},{"style":258},[2701],{"type":28,"value":2702},"\u003C/",{"type":23,"tag":80,"props":2704,"children":2705},{"style":2610},[2706],{"type":28,"value":2613},{"type":23,"tag":80,"props":2708,"children":2709},{"style":258},[2710],{"type":28,"value":2618},{"type":23,"tag":24,"props":2712,"children":2713},{},[2714],{"type":28,"value":2715},"Execute dependency checks regularly in your CI/CD pipeline.",{"type":23,"tag":40,"props":2717,"children":2719},{"id":2718},"_8-logging-and-monitoring",[2720],{"type":28,"value":2721},"8. Logging and Monitoring",{"type":23,"tag":24,"props":2723,"children":2724},{},[2725],{"type":28,"value":2726},"Implement comprehensive logging and monitoring to detect and respond to security incidents:",{"type":23,"tag":70,"props":2728,"children":2730},{"className":2030,"code":2729,"language":1933,"meta":7,"style":7},"logger.warn(\"Failed login attempt for user: {}\", username);\n",[2731],{"type":23,"tag":14,"props":2732,"children":2733},{"__ignoreMap":7},[2734],{"type":23,"tag":80,"props":2735,"children":2736},{"class":82,"line":83},[2737,2742,2747,2751,2756],{"type":23,"tag":80,"props":2738,"children":2739},{"style":258},[2740],{"type":28,"value":2741},"logger.",{"type":23,"tag":80,"props":2743,"children":2744},{"style":87},[2745],{"type":28,"value":2746},"warn",{"type":23,"tag":80,"props":2748,"children":2749},{"style":258},[2750],{"type":28,"value":1753},{"type":23,"tag":80,"props":2752,"children":2753},{"style":93},[2754],{"type":28,"value":2755},"\"Failed login attempt for user: {}\"",{"type":23,"tag":80,"props":2757,"children":2758},{"style":258},[2759],{"type":28,"value":2098},{"type":23,"tag":24,"props":2761,"children":2762},{},[2763],{"type":28,"value":2764},"Aggregate logs centrally, and set up alerts for suspicious activities.",{"type":23,"tag":40,"props":2766,"children":2768},{"id":2767},"conclusion",[2769],{"type":28,"value":2770},"Conclusion",{"type":23,"tag":24,"props":2772,"children":2773},{},[2774],{"type":28,"value":2775},"Adopting these best practices can greatly enhance the security posture of your Java applications. Stay proactive by regularly reviewing security measures, updating dependencies, and monitoring your systems. Security is a continuous process, not a one-time setup.",{"type":23,"tag":1868,"props":2777,"children":2779},{"id":2778},"further-reading",[2780],{"type":28,"value":2781},"Further Reading",{"type":23,"tag":496,"props":2783,"children":2784},{},[2785,2795,2805],{"type":23,"tag":500,"props":2786,"children":2787},{},[2788],{"type":23,"tag":31,"props":2789,"children":2792},{"href":2790,"rel":2791},"https://owasp.org/www-project-top-ten/",[63],[2793],{"type":28,"value":2794},"OWASP Top 10",{"type":23,"tag":500,"props":2796,"children":2797},{},[2798],{"type":23,"tag":31,"props":2799,"children":2802},{"href":2800,"rel":2801},"https://spring.io/projects/spring-security",[63],[2803],{"type":28,"value":2804},"Spring Security",{"type":23,"tag":500,"props":2806,"children":2807},{},[2808],{"type":23,"tag":31,"props":2809,"children":2812},{"href":2810,"rel":2811},"https://owasp.org/www-project-dependency-check/",[63],[2813],{"type":28,"value":2814},"OWASP Dependency Check",{"type":23,"tag":510,"props":2816,"children":2817},{},[2818],{"type":28,"value":514},{"title":7,"searchDepth":141,"depth":141,"links":2820},[2821,2822,2823,2824,2825,2826,2827,2828,2829,2830],{"id":1949,"depth":141,"text":1952},{"id":2019,"depth":141,"text":2022},{"id":2158,"depth":141,"text":2161},{"id":2271,"depth":141,"text":2274},{"id":2410,"depth":141,"text":2413},{"id":2451,"depth":141,"text":2454},{"id":2483,"depth":141,"text":2486},{"id":2582,"depth":141,"text":2585},{"id":2718,"depth":141,"text":2721},{"id":2767,"depth":141,"text":2770},"content:posts:java-security-best-practices.md","posts/java-security-best-practices.md","posts/java-security-best-practices",{"_path":2835,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":2836,"description":2837,"date":2838,"layout":11,"subtitle":2839,"image":2840,"optimized_image":2840,"category":14,"tags":2841,"author":18,"paginate":6,"body":2845,"_type":520,"_id":3651,"_source":522,"_file":3652,"_stem":3653,"_extension":525},"/posts/circuit-breaking-resilience","Circuit Breaking in Distributed Systems","An expert guide to understanding and implementing circuit breakers in distributed systems, with practical Java examples using Resilience4j and best practices.","2024-07-15T11:00:00.000Z","Enhancing Resilience with Circuit Breaker Patterns","/assets/img/uploads/circuit-breaking-resilience.png",[14,1933,2842,2843,2844],"resilience","circuit-breaker","distributed-systems",{"type":20,"children":2846,"toc":3639},[2847,2855,2861,2866,2900,2905,2911,2916,2934,2940,2947,2952,3062,3068,3272,3278,3283,3450,3456,3461,3555,3560,3566,3589,3593,3598,3602,3635],{"type":23,"tag":543,"props":2848,"children":2849},{},[2850],{"type":23,"tag":24,"props":2851,"children":2852},{},[2853],{"type":28,"value":2854},"In modern distributed systems, failures are inevitable. The circuit breaker pattern helps maintain system stability by preventing failures from cascading across service boundaries. In this post, we'll dive deep into circuit breakers, explore their importance, and demonstrate practical implementation using Resilience4j in Java.",{"type":23,"tag":40,"props":2856,"children":2858},{"id":2857},"understanding-the-circuit-breaker-pattern",[2859],{"type":28,"value":2860},"Understanding the Circuit Breaker Pattern",{"type":23,"tag":24,"props":2862,"children":2863},{},[2864],{"type":28,"value":2865},"The circuit breaker pattern prevents a service from making repeated calls to an external service that is experiencing issues. It has three main states:",{"type":23,"tag":496,"props":2867,"children":2868},{},[2869,2880,2890],{"type":23,"tag":500,"props":2870,"children":2871},{},[2872,2878],{"type":23,"tag":2873,"props":2874,"children":2875},"strong",{},[2876],{"type":28,"value":2877},"Closed:",{"type":28,"value":2879}," All requests pass through normally.",{"type":23,"tag":500,"props":2881,"children":2882},{},[2883,2888],{"type":23,"tag":2873,"props":2884,"children":2885},{},[2886],{"type":28,"value":2887},"Open:",{"type":28,"value":2889}," Requests fail fast, bypassing the problematic service.",{"type":23,"tag":500,"props":2891,"children":2892},{},[2893,2898],{"type":23,"tag":2873,"props":2894,"children":2895},{},[2896],{"type":28,"value":2897},"Half-Open:",{"type":28,"value":2899}," A limited number of requests test if the external service has recovered.",{"type":23,"tag":24,"props":2901,"children":2902},{},[2903],{"type":28,"value":2904},"This mechanism helps improve the resilience and responsiveness of your systems.",{"type":23,"tag":40,"props":2906,"children":2908},{"id":2907},"when-to-use-circuit-breakers",[2909],{"type":28,"value":2910},"When to Use Circuit Breakers",{"type":23,"tag":24,"props":2912,"children":2913},{},[2914],{"type":28,"value":2915},"Circuit breakers are essential when:",{"type":23,"tag":496,"props":2917,"children":2918},{},[2919,2924,2929],{"type":23,"tag":500,"props":2920,"children":2921},{},[2922],{"type":28,"value":2923},"Your system relies on remote calls (microservices architecture, external APIs).",{"type":23,"tag":500,"props":2925,"children":2926},{},[2927],{"type":28,"value":2928},"You need to prevent failure propagation.",{"type":23,"tag":500,"props":2930,"children":2931},{},[2932],{"type":28,"value":2933},"You aim to improve user experience by quickly responding to failures.",{"type":23,"tag":40,"props":2935,"children":2937},{"id":2936},"implementing-circuit-breakers-with-resilience4j",[2938],{"type":28,"value":2939},"Implementing Circuit Breakers with Resilience4j",{"type":23,"tag":2941,"props":2942,"children":2944},"h3",{"id":2943},"step-1-adding-dependencies",[2945],{"type":28,"value":2946},"Step 1: Adding Dependencies",{"type":23,"tag":24,"props":2948,"children":2949},{},[2950],{"type":28,"value":2951},"Add Resilience4j dependencies to your project (Maven example):",{"type":23,"tag":70,"props":2953,"children":2955},{"className":2593,"code":2954,"language":2595,"meta":7,"style":7},"\u003Cdependency>\n    \u003CgroupId>io.github.resilience4j\u003C/groupId>\n    \u003CartifactId>resilience4j-circuitbreaker\u003C/artifactId>\n    \u003Cversion>2.0.2\u003C/version>\n\u003C/dependency>\n",[2956],{"type":23,"tag":14,"props":2957,"children":2958},{"__ignoreMap":7},[2959,2975,2999,3023,3047],{"type":23,"tag":80,"props":2960,"children":2961},{"class":82,"line":83},[2962,2966,2971],{"type":23,"tag":80,"props":2963,"children":2964},{"style":258},[2965],{"type":28,"value":2607},{"type":23,"tag":80,"props":2967,"children":2968},{"style":2610},[2969],{"type":28,"value":2970},"dependency",{"type":23,"tag":80,"props":2972,"children":2973},{"style":258},[2974],{"type":28,"value":2618},{"type":23,"tag":80,"props":2976,"children":2977},{"class":82,"line":141},[2978,2982,2986,2991,2995],{"type":23,"tag":80,"props":2979,"children":2980},{"style":258},[2981],{"type":28,"value":2626},{"type":23,"tag":80,"props":2983,"children":2984},{"style":2610},[2985],{"type":28,"value":2631},{"type":23,"tag":80,"props":2987,"children":2988},{"style":258},[2989],{"type":28,"value":2990},">io.github.resilience4j\u003C/",{"type":23,"tag":80,"props":2992,"children":2993},{"style":2610},[2994],{"type":28,"value":2631},{"type":23,"tag":80,"props":2996,"children":2997},{"style":258},[2998],{"type":28,"value":2618},{"type":23,"tag":80,"props":3000,"children":3001},{"class":82,"line":155},[3002,3006,3010,3015,3019],{"type":23,"tag":80,"props":3003,"children":3004},{"style":258},[3005],{"type":28,"value":2626},{"type":23,"tag":80,"props":3007,"children":3008},{"style":2610},[3009],{"type":28,"value":2656},{"type":23,"tag":80,"props":3011,"children":3012},{"style":258},[3013],{"type":28,"value":3014},">resilience4j-circuitbreaker\u003C/",{"type":23,"tag":80,"props":3016,"children":3017},{"style":2610},[3018],{"type":28,"value":2656},{"type":23,"tag":80,"props":3020,"children":3021},{"style":258},[3022],{"type":28,"value":2618},{"type":23,"tag":80,"props":3024,"children":3025},{"class":82,"line":216},[3026,3030,3034,3039,3043],{"type":23,"tag":80,"props":3027,"children":3028},{"style":258},[3029],{"type":28,"value":2626},{"type":23,"tag":80,"props":3031,"children":3032},{"style":2610},[3033],{"type":28,"value":2681},{"type":23,"tag":80,"props":3035,"children":3036},{"style":258},[3037],{"type":28,"value":3038},">2.0.2\u003C/",{"type":23,"tag":80,"props":3040,"children":3041},{"style":2610},[3042],{"type":28,"value":2681},{"type":23,"tag":80,"props":3044,"children":3045},{"style":258},[3046],{"type":28,"value":2618},{"type":23,"tag":80,"props":3048,"children":3049},{"class":82,"line":301},[3050,3054,3058],{"type":23,"tag":80,"props":3051,"children":3052},{"style":258},[3053],{"type":28,"value":2702},{"type":23,"tag":80,"props":3055,"children":3056},{"style":2610},[3057],{"type":28,"value":2970},{"type":23,"tag":80,"props":3059,"children":3060},{"style":258},[3061],{"type":28,"value":2618},{"type":23,"tag":2941,"props":3063,"children":3065},{"id":3064},"step-2-configuring-circuit-breaker",[3066],{"type":28,"value":3067},"Step 2: Configuring Circuit Breaker",{"type":23,"tag":70,"props":3069,"children":3071},{"className":2030,"code":3070,"language":1933,"meta":7,"style":7},"CircuitBreakerConfig config = CircuitBreakerConfig.custom()\n    .failureRateThreshold(50)\n    .waitDurationInOpenState(Duration.ofMillis(1000))\n    .permittedNumberOfCallsInHalfOpenState(2)\n    .minimumNumberOfCalls(5)\n    .build();\n\nCircuitBreaker circuitBreaker = CircuitBreaker.of(\"myService\", config);\n",[3072],{"type":23,"tag":14,"props":3073,"children":3074},{"__ignoreMap":7},[3075,3102,3128,3164,3188,3213,3229,3236],{"type":23,"tag":80,"props":3076,"children":3077},{"class":82,"line":83},[3078,3083,3087,3092,3097],{"type":23,"tag":80,"props":3079,"children":3080},{"style":258},[3081],{"type":28,"value":3082},"CircuitBreakerConfig config ",{"type":23,"tag":80,"props":3084,"children":3085},{"style":242},[3086],{"type":28,"value":322},{"type":23,"tag":80,"props":3088,"children":3089},{"style":258},[3090],{"type":28,"value":3091}," CircuitBreakerConfig.",{"type":23,"tag":80,"props":3093,"children":3094},{"style":87},[3095],{"type":28,"value":3096},"custom",{"type":23,"tag":80,"props":3098,"children":3099},{"style":258},[3100],{"type":28,"value":3101},"()\n",{"type":23,"tag":80,"props":3103,"children":3104},{"class":82,"line":141},[3105,3110,3115,3119,3124],{"type":23,"tag":80,"props":3106,"children":3107},{"style":258},[3108],{"type":28,"value":3109},"    .",{"type":23,"tag":80,"props":3111,"children":3112},{"style":87},[3113],{"type":28,"value":3114},"failureRateThreshold",{"type":23,"tag":80,"props":3116,"children":3117},{"style":258},[3118],{"type":28,"value":1753},{"type":23,"tag":80,"props":3120,"children":3121},{"style":125},[3122],{"type":28,"value":3123},"50",{"type":23,"tag":80,"props":3125,"children":3126},{"style":258},[3127],{"type":28,"value":373},{"type":23,"tag":80,"props":3129,"children":3130},{"class":82,"line":155},[3131,3135,3140,3145,3150,3154,3159],{"type":23,"tag":80,"props":3132,"children":3133},{"style":258},[3134],{"type":28,"value":3109},{"type":23,"tag":80,"props":3136,"children":3137},{"style":87},[3138],{"type":28,"value":3139},"waitDurationInOpenState",{"type":23,"tag":80,"props":3141,"children":3142},{"style":258},[3143],{"type":28,"value":3144},"(Duration.",{"type":23,"tag":80,"props":3146,"children":3147},{"style":87},[3148],{"type":28,"value":3149},"ofMillis",{"type":23,"tag":80,"props":3151,"children":3152},{"style":258},[3153],{"type":28,"value":1753},{"type":23,"tag":80,"props":3155,"children":3156},{"style":125},[3157],{"type":28,"value":3158},"1000",{"type":23,"tag":80,"props":3160,"children":3161},{"style":258},[3162],{"type":28,"value":3163},"))\n",{"type":23,"tag":80,"props":3165,"children":3166},{"class":82,"line":216},[3167,3171,3176,3180,3184],{"type":23,"tag":80,"props":3168,"children":3169},{"style":258},[3170],{"type":28,"value":3109},{"type":23,"tag":80,"props":3172,"children":3173},{"style":87},[3174],{"type":28,"value":3175},"permittedNumberOfCallsInHalfOpenState",{"type":23,"tag":80,"props":3177,"children":3178},{"style":258},[3179],{"type":28,"value":1753},{"type":23,"tag":80,"props":3181,"children":3182},{"style":125},[3183],{"type":28,"value":2118},{"type":23,"tag":80,"props":3185,"children":3186},{"style":258},[3187],{"type":28,"value":373},{"type":23,"tag":80,"props":3189,"children":3190},{"class":82,"line":301},[3191,3195,3200,3204,3209],{"type":23,"tag":80,"props":3192,"children":3193},{"style":258},[3194],{"type":28,"value":3109},{"type":23,"tag":80,"props":3196,"children":3197},{"style":87},[3198],{"type":28,"value":3199},"minimumNumberOfCalls",{"type":23,"tag":80,"props":3201,"children":3202},{"style":258},[3203],{"type":28,"value":1753},{"type":23,"tag":80,"props":3205,"children":3206},{"style":125},[3207],{"type":28,"value":3208},"5",{"type":23,"tag":80,"props":3210,"children":3211},{"style":258},[3212],{"type":28,"value":373},{"type":23,"tag":80,"props":3214,"children":3215},{"class":82,"line":311},[3216,3220,3225],{"type":23,"tag":80,"props":3217,"children":3218},{"style":258},[3219],{"type":28,"value":3109},{"type":23,"tag":80,"props":3221,"children":3222},{"style":87},[3223],{"type":28,"value":3224},"build",{"type":23,"tag":80,"props":3226,"children":3227},{"style":258},[3228],{"type":28,"value":2150},{"type":23,"tag":80,"props":3230,"children":3231},{"class":82,"line":330},[3232],{"type":23,"tag":80,"props":3233,"children":3234},{"emptyLinePlaceholder":305},[3235],{"type":28,"value":308},{"type":23,"tag":80,"props":3237,"children":3238},{"class":82,"line":348},[3239,3244,3248,3253,3258,3262,3267],{"type":23,"tag":80,"props":3240,"children":3241},{"style":258},[3242],{"type":28,"value":3243},"CircuitBreaker circuitBreaker ",{"type":23,"tag":80,"props":3245,"children":3246},{"style":242},[3247],{"type":28,"value":322},{"type":23,"tag":80,"props":3249,"children":3250},{"style":258},[3251],{"type":28,"value":3252}," CircuitBreaker.",{"type":23,"tag":80,"props":3254,"children":3255},{"style":87},[3256],{"type":28,"value":3257},"of",{"type":23,"tag":80,"props":3259,"children":3260},{"style":258},[3261],{"type":28,"value":1753},{"type":23,"tag":80,"props":3263,"children":3264},{"style":93},[3265],{"type":28,"value":3266},"\"myService\"",{"type":23,"tag":80,"props":3268,"children":3269},{"style":258},[3270],{"type":28,"value":3271},", config);\n",{"type":23,"tag":2941,"props":3273,"children":3275},{"id":3274},"step-3-using-circuit-breaker",[3276],{"type":28,"value":3277},"Step 3: Using Circuit Breaker",{"type":23,"tag":24,"props":3279,"children":3280},{},[3281],{"type":28,"value":3282},"Wrap service calls using the circuit breaker:",{"type":23,"tag":70,"props":3284,"children":3286},{"className":2030,"code":3285,"language":1933,"meta":7,"style":7},"Supplier\u003CString> decoratedSupplier = CircuitBreaker.decorateSupplier(circuitBreaker, () -> myService.remoteCall());\n\nTry.ofSupplier(decoratedSupplier)\n    .recover(throwable -> \"Fallback response\")\n    .onSuccess(result -> System.out.println(\"Response: \" + result));\n",[3287],{"type":23,"tag":14,"props":3288,"children":3289},{"__ignoreMap":7},[3290,3345,3352,3370,3400],{"type":23,"tag":80,"props":3291,"children":3292},{"class":82,"line":83},[3293,3298,3303,3308,3312,3316,3321,3326,3331,3336,3341],{"type":23,"tag":80,"props":3294,"children":3295},{"style":258},[3296],{"type":28,"value":3297},"Supplier\u003C",{"type":23,"tag":80,"props":3299,"children":3300},{"style":242},[3301],{"type":28,"value":3302},"String",{"type":23,"tag":80,"props":3304,"children":3305},{"style":258},[3306],{"type":28,"value":3307},"> decoratedSupplier ",{"type":23,"tag":80,"props":3309,"children":3310},{"style":242},[3311],{"type":28,"value":322},{"type":23,"tag":80,"props":3313,"children":3314},{"style":258},[3315],{"type":28,"value":3252},{"type":23,"tag":80,"props":3317,"children":3318},{"style":87},[3319],{"type":28,"value":3320},"decorateSupplier",{"type":23,"tag":80,"props":3322,"children":3323},{"style":258},[3324],{"type":28,"value":3325},"(circuitBreaker, () ",{"type":23,"tag":80,"props":3327,"children":3328},{"style":242},[3329],{"type":28,"value":3330},"->",{"type":23,"tag":80,"props":3332,"children":3333},{"style":258},[3334],{"type":28,"value":3335}," myService.",{"type":23,"tag":80,"props":3337,"children":3338},{"style":87},[3339],{"type":28,"value":3340},"remoteCall",{"type":23,"tag":80,"props":3342,"children":3343},{"style":258},[3344],{"type":28,"value":2229},{"type":23,"tag":80,"props":3346,"children":3347},{"class":82,"line":141},[3348],{"type":23,"tag":80,"props":3349,"children":3350},{"emptyLinePlaceholder":305},[3351],{"type":28,"value":308},{"type":23,"tag":80,"props":3353,"children":3354},{"class":82,"line":155},[3355,3360,3365],{"type":23,"tag":80,"props":3356,"children":3357},{"style":258},[3358],{"type":28,"value":3359},"Try.",{"type":23,"tag":80,"props":3361,"children":3362},{"style":87},[3363],{"type":28,"value":3364},"ofSupplier",{"type":23,"tag":80,"props":3366,"children":3367},{"style":258},[3368],{"type":28,"value":3369},"(decoratedSupplier)\n",{"type":23,"tag":80,"props":3371,"children":3372},{"class":82,"line":216},[3373,3377,3382,3387,3391,3396],{"type":23,"tag":80,"props":3374,"children":3375},{"style":258},[3376],{"type":28,"value":3109},{"type":23,"tag":80,"props":3378,"children":3379},{"style":87},[3380],{"type":28,"value":3381},"recover",{"type":23,"tag":80,"props":3383,"children":3384},{"style":258},[3385],{"type":28,"value":3386},"(throwable ",{"type":23,"tag":80,"props":3388,"children":3389},{"style":242},[3390],{"type":28,"value":3330},{"type":23,"tag":80,"props":3392,"children":3393},{"style":93},[3394],{"type":28,"value":3395}," \"Fallback response\"",{"type":23,"tag":80,"props":3397,"children":3398},{"style":258},[3399],{"type":28,"value":373},{"type":23,"tag":80,"props":3401,"children":3402},{"class":82,"line":301},[3403,3407,3412,3417,3421,3426,3431,3435,3440,3445],{"type":23,"tag":80,"props":3404,"children":3405},{"style":258},[3406],{"type":28,"value":3109},{"type":23,"tag":80,"props":3408,"children":3409},{"style":87},[3410],{"type":28,"value":3411},"onSuccess",{"type":23,"tag":80,"props":3413,"children":3414},{"style":258},[3415],{"type":28,"value":3416},"(result ",{"type":23,"tag":80,"props":3418,"children":3419},{"style":242},[3420],{"type":28,"value":3330},{"type":23,"tag":80,"props":3422,"children":3423},{"style":258},[3424],{"type":28,"value":3425}," System.out.",{"type":23,"tag":80,"props":3427,"children":3428},{"style":87},[3429],{"type":28,"value":3430},"println",{"type":23,"tag":80,"props":3432,"children":3433},{"style":258},[3434],{"type":28,"value":1753},{"type":23,"tag":80,"props":3436,"children":3437},{"style":93},[3438],{"type":28,"value":3439},"\"Response: \"",{"type":23,"tag":80,"props":3441,"children":3442},{"style":242},[3443],{"type":28,"value":3444}," +",{"type":23,"tag":80,"props":3446,"children":3447},{"style":258},[3448],{"type":28,"value":3449}," result));\n",{"type":23,"tag":40,"props":3451,"children":3453},{"id":3452},"monitoring-and-metrics",[3454],{"type":28,"value":3455},"Monitoring and Metrics",{"type":23,"tag":24,"props":3457,"children":3458},{},[3459],{"type":28,"value":3460},"Resilience4j provides built-in support for metrics and monitoring:",{"type":23,"tag":70,"props":3462,"children":3464},{"className":2030,"code":3463,"language":1933,"meta":7,"style":7},"CircuitBreakerRegistry registry = CircuitBreakerRegistry.ofDefaults();\nregistry.getEventPublisher()\n    .onStateTransition(event -> System.out.println(event.getStateTransition()));\n",[3465],{"type":23,"tag":14,"props":3466,"children":3467},{"__ignoreMap":7},[3468,3494,3511],{"type":23,"tag":80,"props":3469,"children":3470},{"class":82,"line":83},[3471,3476,3480,3485,3490],{"type":23,"tag":80,"props":3472,"children":3473},{"style":258},[3474],{"type":28,"value":3475},"CircuitBreakerRegistry registry ",{"type":23,"tag":80,"props":3477,"children":3478},{"style":242},[3479],{"type":28,"value":322},{"type":23,"tag":80,"props":3481,"children":3482},{"style":258},[3483],{"type":28,"value":3484}," CircuitBreakerRegistry.",{"type":23,"tag":80,"props":3486,"children":3487},{"style":87},[3488],{"type":28,"value":3489},"ofDefaults",{"type":23,"tag":80,"props":3491,"children":3492},{"style":258},[3493],{"type":28,"value":2150},{"type":23,"tag":80,"props":3495,"children":3496},{"class":82,"line":141},[3497,3502,3507],{"type":23,"tag":80,"props":3498,"children":3499},{"style":258},[3500],{"type":28,"value":3501},"registry.",{"type":23,"tag":80,"props":3503,"children":3504},{"style":87},[3505],{"type":28,"value":3506},"getEventPublisher",{"type":23,"tag":80,"props":3508,"children":3509},{"style":258},[3510],{"type":28,"value":3101},{"type":23,"tag":80,"props":3512,"children":3513},{"class":82,"line":155},[3514,3518,3523,3528,3532,3536,3540,3545,3550],{"type":23,"tag":80,"props":3515,"children":3516},{"style":258},[3517],{"type":28,"value":3109},{"type":23,"tag":80,"props":3519,"children":3520},{"style":87},[3521],{"type":28,"value":3522},"onStateTransition",{"type":23,"tag":80,"props":3524,"children":3525},{"style":258},[3526],{"type":28,"value":3527},"(event ",{"type":23,"tag":80,"props":3529,"children":3530},{"style":242},[3531],{"type":28,"value":3330},{"type":23,"tag":80,"props":3533,"children":3534},{"style":258},[3535],{"type":28,"value":3425},{"type":23,"tag":80,"props":3537,"children":3538},{"style":87},[3539],{"type":28,"value":3430},{"type":23,"tag":80,"props":3541,"children":3542},{"style":258},[3543],{"type":28,"value":3544},"(event.",{"type":23,"tag":80,"props":3546,"children":3547},{"style":87},[3548],{"type":28,"value":3549},"getStateTransition",{"type":23,"tag":80,"props":3551,"children":3552},{"style":258},[3553],{"type":28,"value":3554},"()));\n",{"type":23,"tag":24,"props":3556,"children":3557},{},[3558],{"type":28,"value":3559},"Integrate with Prometheus, Grafana, or other observability tools for comprehensive monitoring.",{"type":23,"tag":40,"props":3561,"children":3563},{"id":3562},"best-practices-for-circuit-breaking",[3564],{"type":28,"value":3565},"Best Practices for Circuit Breaking",{"type":23,"tag":496,"props":3567,"children":3568},{},[3569,3574,3579,3584],{"type":23,"tag":500,"props":3570,"children":3571},{},[3572],{"type":28,"value":3573},"Configure sensible thresholds to avoid premature circuit opening.",{"type":23,"tag":500,"props":3575,"children":3576},{},[3577],{"type":28,"value":3578},"Always define fallback mechanisms.",{"type":23,"tag":500,"props":3580,"children":3581},{},[3582],{"type":28,"value":3583},"Regularly monitor circuit breaker states and metrics.",{"type":23,"tag":500,"props":3585,"children":3586},{},[3587],{"type":28,"value":3588},"Test thoroughly to ensure reliability under failure conditions.",{"type":23,"tag":40,"props":3590,"children":3591},{"id":2767},[3592],{"type":28,"value":2770},{"type":23,"tag":24,"props":3594,"children":3595},{},[3596],{"type":28,"value":3597},"Implementing circuit breakers using Resilience4j significantly enhances the resilience of your Java-based distributed systems. This proactive approach ensures your systems gracefully handle failures, maintaining service availability and user satisfaction.",{"type":23,"tag":1868,"props":3599,"children":3600},{"id":2778},[3601],{"type":28,"value":2781},{"type":23,"tag":496,"props":3603,"children":3604},{},[3605,3615,3625],{"type":23,"tag":500,"props":3606,"children":3607},{},[3608],{"type":23,"tag":31,"props":3609,"children":3612},{"href":3610,"rel":3611},"https://resilience4j.readme.io/docs",[63],[3613],{"type":28,"value":3614},"Resilience4j Official Documentation",{"type":23,"tag":500,"props":3616,"children":3617},{},[3618],{"type":23,"tag":31,"props":3619,"children":3622},{"href":3620,"rel":3621},"https://martinfowler.com/bliki/CircuitBreaker.html",[63],[3623],{"type":28,"value":3624},"Circuit Breaker Pattern by Martin Fowler",{"type":23,"tag":500,"props":3626,"children":3627},{},[3628],{"type":23,"tag":31,"props":3629,"children":3632},{"href":3630,"rel":3631},"https://microservices.io/patterns/reliability/circuit-breaker.html",[63],[3633],{"type":28,"value":3634},"Microservices Patterns by Chris Richardson",{"type":23,"tag":510,"props":3636,"children":3637},{},[3638],{"type":28,"value":514},{"title":7,"searchDepth":141,"depth":141,"links":3640},[3641,3642,3643,3648,3649,3650],{"id":2857,"depth":141,"text":2860},{"id":2907,"depth":141,"text":2910},{"id":2936,"depth":141,"text":2939,"children":3644},[3645,3646,3647],{"id":2943,"depth":155,"text":2946},{"id":3064,"depth":155,"text":3067},{"id":3274,"depth":155,"text":3277},{"id":3452,"depth":141,"text":3455},{"id":3562,"depth":141,"text":3565},{"id":2767,"depth":141,"text":2770},"content:posts:circuit-breaking-resilience.md","posts/circuit-breaking-resilience.md","posts/circuit-breaking-resilience",[3655,3656,3657,3658,3662,3666,3670,3674,3678,3682,3686,3690,3694,3698,3702,3706,3710,3714,3717,3721,3725],{"_path":528,"title":529,"date":531},{"_path":1926,"title":1927,"date":1929},{"_path":2835,"title":2836,"date":2838},{"_path":3659,"title":3660,"date":3661},"/posts/maven-and-github-actions","Automating Java Builds with Maven and GitHub Actions CI/CD","2024-06-15T11:00:00.000Z",{"_path":3663,"title":3664,"date":3665},"/posts/pattern-matching-sealed-classes-java21","A Guide to Pattern Matching and Sealed Classes in Java 21","2024-05-15T11:00:00.000Z",{"_path":3667,"title":3668,"date":3669},"/posts/async-java-completable-vs-reactive","Asynchronous Programming in Java: CompletableFuture vs Reactive Streams","2024-04-15T11:00:00.000Z",{"_path":3671,"title":3672,"date":3673},"/posts/mastering-concurrency","Mastering Concurrency in Java: Threads, Executors, and Virtual Threads","2024-03-15T11:00:00.000Z",{"_path":3675,"title":3676,"date":3677},"/posts/microservices-mistakes","Common Mistakes When Developing Microservices","2024-02-15T11:00:00.000Z",{"_path":3679,"title":3680,"date":3681},"/posts/tdd-testing","Test-Driven Development (TDD) in Java with JUnit and Mockito","2024-01-15T11:00:00.000Z",{"_path":3683,"title":3684,"date":3685},"/posts/single-table-design","Single Table Design","2023-12-27T11:00:00.000Z",{"_path":3687,"title":3688,"date":3689},"/posts/java-17","Exploring Java 17's New Features","2023-10-08T11:00:00.000Z",{"_path":3691,"title":3692,"date":3693},"/posts/hexagonal-architecture","Introduction to Hexagonal Architecture","2023-10-02 18:00:00",{"_path":3695,"title":3696,"date":3697},"/posts/terraform","Unleashing the Power of Infrastructure as Code (IaC)","2023-09-24 18:00:00",{"_path":3699,"title":3700,"date":3701},"/posts/python-virtual-environments","Python Virtual Environments (venv)","2023-09-17 22:00:00",{"_path":3703,"title":3704,"date":3705},"/posts/api-first-principles","API First principles","2023-09-01 22:15:18",{"_path":3707,"title":3708,"date":3709},"/posts/how-to-configure-ssh-key-based-authentication","How To Configure SSH Key-Based Authentication","2021-02-21 03:46:12",{"_path":3711,"title":3712,"date":3713},"/posts/algorithms-binary-search-tree","Algorithms - Binary Search Tree in kotlin","2020-12-06T11:00:00.000Z",{"_path":3715,"title":3716,"date":3713},"/posts/how-to-create-a-twitter-bot","How to create a Twitter bot",{"_path":3718,"title":3719,"date":3720},"/posts/manage-aws-infrastructure-with-terraform","Bootstrap complete Java application infrastructure in AWS with Terraform","2020-12-02T11:00:00.000Z",{"_path":3722,"title":3723,"date":3724},"/posts/algorithms-working-with-trees","Algorithms - Binary tree traversals in kotlin","2020-11-24T11:00:00.000Z",{"_path":4,"title":8,"date":10},1790510066009]