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