<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Miguel Ángel Lobato on Medium]]></title>
        <description><![CDATA[Stories by Miguel Ángel Lobato on Medium]]></description>
        <link>https://medium.com/@miguellobato84?source=rss-646640507fb3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*LaIVKdFDHXrUGFdhZsSVOA.jpeg</url>
            <title>Stories by Miguel Ángel Lobato on Medium</title>
            <link>https://medium.com/@miguellobato84?source=rss-646640507fb3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 22:49:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@miguellobato84/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[About reading raw json files in spark]]></title>
            <link>https://medium.com/@miguellobato84/about-reading-raw-json-files-in-spark-ba2bd453ae83?source=rss-646640507fb3------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba2bd453ae83</guid>
            <category><![CDATA[delta-lake]]></category>
            <category><![CDATA[spark]]></category>
            <category><![CDATA[json]]></category>
            <category><![CDATA[schema]]></category>
            <dc:creator><![CDATA[Miguel Ángel Lobato]]></dc:creator>
            <pubDate>Fri, 13 May 2022 18:44:47 GMT</pubDate>
            <atom:updated>2022-05-13T18:46:59.948Z</atom:updated>
            <content:encoded><![CDATA[<p>A while ago, I wrote an article here at medium with an approach to read raw json files in spark and an approach to handle schema changes. <a href="https://medium.com/@miguellobato84/improving-delta-lake-schema-evolution-2cce8db2f0f5">It can be found here</a>.</p><p>In my case, a spark job is adding latest changes to a Delta Lake table. The source are plain json files, so schema changes is something that happens more often I would like to.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7hxhbfOSVLske2JONQYrAA.jpeg" /></figure><p>After some time of the code in a production environment, some workarounds has to be included to reduce the merging schema problems:</p><ol><li>Reading json files as raw text and performing some replacements, for example if the value of a property is an empty string I replace it with a null value. I do this replacement because the client is sending unknown fields as a empty string.</li><li>After performing the replacements, the schema is calculated with Spark.</li><li>Readed schema is compared and merged with the target schema as explained in the previous post.</li><li>Now, we read again the json files with the calculated schema.</li></ol><p>An example of this code can be found here:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f11da560ff1cfb035b6011718eb8d7f4/href">https://medium.com/media/f11da560ff1cfb035b6011718eb8d7f4/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba2bd453ae83" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Improving delta lake schema evolution]]></title>
            <link>https://medium.com/@miguellobato84/improving-delta-lake-schema-evolution-2cce8db2f0f5?source=rss-646640507fb3------2</link>
            <guid isPermaLink="false">https://medium.com/p/2cce8db2f0f5</guid>
            <dc:creator><![CDATA[Miguel Ángel Lobato]]></dc:creator>
            <pubDate>Mon, 13 Sep 2021 08:08:51 GMT</pubDate>
            <atom:updated>2022-05-13T18:30:35.717Z</atom:updated>
            <content:encoded><![CDATA[<p><strong><em>Important note: in Delta Lake 1.1.0 and following releases, part of this problem is solved (but not completely).</em></strong></p><blockquote>From <a href="https://github.com/delta-io/delta/releases/tag/v1.1.0">Delta Lake 1.1.0</a> CHANGELOG:</blockquote><blockquote><em>When applying the MERGE operation on a target table having a column typed as an array of nested structs, the nested columns between the source and target data are now resolved by name and not by position in the struct. This ensures structs in arrays have a consistent behavior with structs outside arrays.</em></blockquote><p>Hi there, this is my first post at medium (please subscribe! more will follow) and I will show my way to solve a very common problem related to schema evolution when working with spark and <a href="https://delta.io/">delta lake</a>.</p><p>In my case, I’m, a Kafka Connect component is writing schemaless json files in a “raw” AWS S3 bucket. That strategy is simple as you don’t have to deal with the schema before writing to Kafka, but you have to deal in the consolidation step.</p><p>Delta Lake comes with some <a href="https://docs.delta.io/latest/delta-update.html#automatic-schema-evolution">automatic schema evolution functionality</a>, but in some cases it’s impossible to merge both schemas, so it raises the following error:</p><blockquote>org.apache.spark.sql.AnalysisException: Failed to merge fields ‘a2’ and ‘a2’. Failed to merge fields ‘a3’ and ‘a3’. Failed to merge incompatible data types StringType and StructType(StructField(a4,StringType,true), StructField(a5,StringType,true))<br> at org.apache.spark.sql.delta.schema.SchemaUtils$.$anonfun$mergeSchemas$1(SchemaUtils.scala:847)<br> …</blockquote><p>This error will appear when delta is unable to perform a merge schema operation. Some cases, as the exposed in the example, are impossible to solve but to force casting the struct to string type.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UxhBnhixnU7AsKOKg52Hig.jpeg" /></figure><h3><strong>My solution</strong></h3><p>The solution I’m proposing is to calculate a json reading schema by comparing the new one with the existing one.</p><p>This is achieved in two steps, being the first one some schema transformation (that is, converting array&lt;struct&gt; to array&lt;string&gt;). This step (in theory) won’t be necessary with newer Delta Lake versions (I didn’t tested it).</p><p>The second one will perform the schema merge to return a <strong>new schema</strong>, that will be used while reading the json files.</p><p>These are the main scenarios that this method can handle:</p><ul><li>If there is a new field (event inside structs), no changes to the schema are done as Delta Lake will merge it properly.</li><li>If the field exists in both schemas, the target data type will be returned. Spark will try to read the json with the target data type, and if its not possible it will store the field in a “corrupt_record” field).</li><li>If the field is a struct, the method will recursively process the inner fields.</li></ul><p>This is the function I’m using, but it can be found <a href="https://github.com/miguellobato84/spark-delta-schema-evolution">at my github</a> with some examples.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1839a01adf37ed7a5c5e47e65c7ae104/href">https://medium.com/media/1839a01adf37ed7a5c5e47e65c7ae104/href</a></iframe><h3>Possible drawbacks and improvements</h3><p>The main drawback is casting array&lt;struct&gt; to array&lt;string&gt;, losing the schema in the way. This should be corrected with Delta Lake 1.1.0.</p><p>The another drawback is that some kind of alerting system has to be monitoring if there are new rows with the “corrupt_record” field, so this case should be attended individually. Spark will leave as NULL the field.</p><p>Full source code can be found in <a href="https://github.com/miguellobato84/spark-delta-schema-evolution">my github repository</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2cce8db2f0f5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>