<?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 Samuel So on Medium]]></title>
        <description><![CDATA[Stories by Samuel So on Medium]]></description>
        <link>https://medium.com/@samsso?source=rss-df48d979d070------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*eIM8baPHPd8l-aHdjsa8Pw.jpeg</url>
            <title>Stories by Samuel So on Medium</title>
            <link>https://medium.com/@samsso?source=rss-df48d979d070------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 25 Nov 2026 01:24:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@samsso/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[Migrating Data from Legacy Systems Using Akka Streams and Alpakka]]></title>
            <link>https://build.diligent.com/migrating-data-from-legacy-systems-using-akka-streams-and-alpakka-90db4ba2eab4?source=rss-df48d979d070------2</link>
            <guid isPermaLink="false">https://medium.com/p/90db4ba2eab4</guid>
            <category><![CDATA[alpakka]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[akka-streams]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[scala]]></category>
            <dc:creator><![CDATA[Samuel So]]></dc:creator>
            <pubDate>Tue, 02 Jun 2020 12:56:00 GMT</pubDate>
            <atom:updated>2020-06-02T12:56:00.996Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/910/1*qpeoIAKeHRuoejhWFqU_pA.jpeg" /></figure><p>Migrating data from old applications can be a pain. This is especially true for legacy applications with a lot of data. Although data extraction is straightforward using database backup programs or an application’s built-in data export feature, shaping the extracted data into the format you need can be difficult, especially for large data sets. The extracted data may be incomplete, or may not be packaged in a structure required by your migration workflow. To address these challenges we created a utility that makes the data migration process as efficient and painless as possible. This blog describes how we did this using <a href="https://doc.akka.io/docs/akka/current/stream/index.html">Akka Streams</a> and <a href="https://doc.akka.io/docs/alpakka/current/index.html">Alpakka</a>.</p><h3>Use Case</h3><p>Our legacy product (which we’ll call Source App) is an on-premise Java EE application that contains a large amount of data in a SQL database. Our goal is to extract that information and transfer it over to our cloud-based solution (which we’ll call Target App). The product’s main feature is to analyze business data and detect fraud and anomalies on a scheduled basis. Analysis data is modelled as <em>Analytic Tables </em>which belong to one or more <em>Analytic Projects. </em>Also belonging to <em>Analytic Projects</em> are <em>Analytic Scripts</em> which contain sets of SQL-like operations to be executed on <em>Analytic Tables</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/323/1*r-tq_FSJfMBo8v04xds4ww.png" /><figcaption>An Analytic Project contains Analytic Tables and Analytic Scripts</figcaption></figure><p>These objects are stored in the Source App database in a single denormalized table — audititems, with every item having a reference to its parent item, which itself is in this same table. Each table record has metadata columns describing the item’s ID, name, type, parent ID, etc. The relationship of these objects and their metadata are the things we’re interested in transferring into its successor product (Target App).</p><figure><img alt="A sample of Analytic objects from App A’s audititems table." src="https://cdn-images-1.medium.com/max/521/1*NUObA-WIcs_fIsdtDacSHA.png" /><figcaption>Source App’s <em>audititems</em> table</figcaption></figure><p>To import this data, we will use Target App’s import feature which can ingest data from compressed files with data laid out in a predefined structure. This structure organizes data per <em>Analytic Project </em>with its name and contents derived from Source App&#39;s database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*5wpE58mRHm_u1ay20B0JFg.png" /><figcaption>Example folder structure for Target App’s import feature</figcaption></figure><p>Source App has an archive feature available that can be used to extract data, but its output format does not match what’s needed for Target App’s import feature. In order to migrate data from the old application, customers usually engage our consulting team to conduct a migration process which involves manually archiving every <em>Analytic Project</em> and placing the archive outputs in folders representing the relationship of the extracted items. This is a time-consuming process and can take up a large portion of time in the consulting engagement that could otherwise be spent on adding more value to the customer.</p><p>What we wanted was a standalone data extraction and transformation utility that can be used to get data out from Source App to be loaded into Target App without a lot of manual work. This tool should make the extraction and packaging of old application data much easier and reduce the overall time needed to complete the migration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*4-RiIioOqaD2dhzy-RVjCg.png" /></figure><h3>Options Considered</h3><p>First we considered modifying the existing archive feature from Source App to export data in the format we need for the migration process. However, development and testing can be challenging for a legacy application — software frameworks and libraries become outdated and existing functionality can be hard to extend. The process to deploy this new feature is also time consuming as the upgrade involves running installer software directly on the server, often requiring additional planning and access rights.</p><p>Another option we considered was to extract the data directly using database backup programs or dump utilities and format the data with custom shell/SQL scripts. This would be an efficient way to extract the needed metadata, but a fair amount of work would be needed to derive the relationship of the extracted objects (some of which are stored as XML data that must first be parsed) and place them in the correct paths in the required structure. Also, much of this work is specific to the customer and may not be easily reusable.</p><h3>A Streaming Solution</h3><p>To cut out all the custom work, we decided on building a simple standalone streaming solution utilizing <a href="https://doc.akka.io/docs/alpakka/current/index.html">Alpakka</a> to connect to the legacy application database with the <a href="https://doc.akka.io/docs/alpakka/current/slick.html">Slick JDBC connector</a> — <a href="http://scala-slick.org/"><em>Slick</em></a><em> is a database access library for Scala.</em> We’ll use <a href="https://doc.akka.io/docs/akka/current/stream/stream-flows-and-basics.html">basic linear stages</a> from the <a href="https://doc.akka.io/docs/akka/current/stream/index.html">Akka Streams</a> library to process elements and construct the destination output files that we need.</p><p>Each step in the migration process is expressed by a processing stage. These are put together to compose a processing pipeline capable of extracting and transforming large data sets into files of the expected format, which are then compressed. By using the database connectors from Alpakka and the fluent DSL provided by Akka Streams, we get many of the benefits of streaming including ease of use, reusability, and composability. Best of all, we do not need to worry about running out of memory processing large data sets as we would with a non-streaming solution. By using Akka’s high-level, explicit API we enjoy these benefits without having to manage the complexity of streaming ourselves. Aside from the aforementioned benefits of going with this approach, this was a good opportunity to work with a fun and new way of programming a solution that doesn’t involve working with large objects in memory.</p><p>Here are the steps — <em>to be described in detail with code later</em>:</p><ol><li>Establish a connection to Source App’s database using Alpakka’s Slick connector for PostgresSQL.</li><li>Create a <a href="https://doc.akka.io/api/akka/current/akka/stream/scaladsl/Source.html">Source</a> with <a href="https://doc.akka.io/docs/alpakka/current/slick.html#using-a-slick-source">Slick.source</a> from the results of a database query to extract the records we’re interested in as AuditItem elements from Source App.</li><li>Use Akka Stream’s linear stage processing to transform elements into our desired data type Project (Source App’s <em>Analytic Project</em>).</li><li>Create a <a href="https://doc.akka.io/api/akka/current/akka/stream/scaladsl/Flow.html">Flow</a> that creates directories from the paths constructed from Project elements and writes the project content out into the directories using Akka Streams’ <a href="https://doc.akka.io/docs/akka/current/stream/operators/FileIO/toPath.html">FileIO</a>.</li><li>Compress the entire output folder.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/661/1*DBkamWeFbg0y-omcoqx2-A.png" /></figure><h3>The Code</h3><p>The steps to our solution can be expressed simply with a single statement of code that ties everything together. We’ll go over the implementation by covering each of these steps in detail next.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a2cedac9ccc964fca533922b8e2353e0/href">https://medium.com/media/a2cedac9ccc964fca533922b8e2353e0/href</a></iframe><h4>Establish a Connection</h4><p>Before we can run the code shown above, we need to first configure a database session. In our standalone tool written in Scala, we create a database session with an instance of <a href="https://doc.akka.io/docs/alpakka/current/slick.html#starting-a-database-session">SlickSession</a> defined in the application.conf with profile and db properties for declaring database connection settings. The following is for PostgreSQL, but other database connection types can be added by substituting the profile and driver:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94783012af2f2a70379ada647154f2b4/href">https://medium.com/media/94783012af2f2a70379ada647154f2b4/href</a></iframe><p>In our case, database connection settings are available from Source App in the form of an encrypted XML file. Username and password entries are decrypted and used to create a database session. Note that the constructed config overrides settings loaded from application.conf. This gives us flexibility in how we can create this config object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89458b3e0e5a6ee5d3403ca069e1a5da/href">https://medium.com/media/89458b3e0e5a6ee5d3403ca069e1a5da/href</a></iframe><h4>Create a Source</h4><p>Once a database session is available, we can create a <a href="https://doc.akka.io/docs/alpakka/current/slick.html#using-a-slick-source">Slick Source</a> that emits elements from the results of a SQL query. We now define the auditItems value that you saw in the first code block. The SQL query retrieves data from Source App’s audititems and audititemsdata tables using <a href="http://scala-slick.org/doc/3.3.1/gettingstarted.html#plain-sql--string-interpolation">Slick’s plain SQL / String Interpolation</a>. This returns a result set of the AuditItem class representing the data we’re extracting from the database. The data for our pipeline comes from this <a href="https://doc.akka.io/api/akka/current/akka/stream/scaladsl/Source.html">Source</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2821819b9d4f3489b24316cdf23fb34e/href">https://medium.com/media/2821819b9d4f3489b24316cdf23fb34e/href</a></iframe><h4>Transform Elements</h4><p>Using the Source <a href="https://doc.akka.io/docs/akka/current/stream/operators/Source-or-Flow/map.html">map </a>operator, we transform elements from the auditItems Source defined above into the expected data type — from AuditItem elements to Project elements. The following code illustrates how .map(auditItem =&gt; Project(auditItem)) performs the conversion using the apply factory method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff4547561614ac290e5da90b70d1708b/href">https://medium.com/media/ff4547561614ac290e5da90b70d1708b/href</a></iframe><h4>Create a Flow</h4><p>Then, using an <a href="https://doc.akka.io/api/akka/current/akka/stream/scaladsl/Flow.html">Akka Streams Flow Stage</a>, we process all of theProject elements received from the auditItems Source after the transformation step above. In this stage, we’ll first create the file along with its full path based on the attributes of the Project element and then write to this file with data contained in the Project element’s content field representing the <em>Analytic Project</em>. The writing of data is done using Akka Stream’s <a href="https://doc.akka.io/docs/akka/current/stream/operators/FileIO/toPath.html">FileIO</a> with <a href="https://doc.akka.io/api/akka/current/akka/stream/IOResult.html">IOResult</a> as the return type. Each element is processed asynchronously and in parallel using the <a href="https://doc.akka.io/docs/akka/current/stream/operators/Source-or-Flow/mapAsync.html">mapAsync</a> operator with parallelism set to 4.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8695a7f89770f7272850bb2839177927/href">https://medium.com/media/8695a7f89770f7272850bb2839177927/href</a></iframe><h4>Compress the Results</h4><p>Finally, we construct the processing pipeline and <a href="https://doc.akka.io/docs/akka/current/stream/stream-flows-and-basics.html#defining-and-running-streams">materialize the stream</a>. For simplicity, the IOResult elements emitted downstream after the steps above are discarded in <a href="https://doc.akka.io/docs/akka/current/stream/operators/Sink/ignore.html">Sink.ignore</a>, though you can make use of them using other <a href="https://doc.akka.io/docs/akka/current/stream/operators/index.html#sink-operators">Sink operators</a>. The extract and transform phases are expressed in a clean and concise manner. Upon completion, the expected files and folders have been created and the remaining task is to compress the whole path in the expected layout for importing into Target App.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a2cedac9ccc964fca533922b8e2353e0/href">https://medium.com/media/a2cedac9ccc964fca533922b8e2353e0/href</a></iframe><h3>Summary</h3><p>We demonstrated how the integration connectors from Alpakka and the operators provided by Akka Streams can make implementation of a streaming data extraction and transformation tool quite simple and straightforward. With much of the complexity of streaming managed by Akka, we can easily build applications capable of handling large amounts of data that are efficient and easily extensible. Adding support for customers with Oracle databases, for example, only requires the use of a different Alpakka integration connector with slight changes to the config. This tool has a small memory footprint for the amount of data that it can process and its implementation was made simple by the libraries that Akka provides. There’s a lot of potential to extend the capabilities of this tool as well, as Akka Streams offers a vast array of <a href="https://doc.akka.io/docs/akka/current/stream/operators/index.html">operators</a> that we can add to our processing pipeline to manipulate elements as needed.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=90db4ba2eab4" width="1" height="1" alt=""><hr><p><a href="https://build.diligent.com/migrating-data-from-legacy-systems-using-akka-streams-and-alpakka-90db4ba2eab4">Migrating Data from Legacy Systems Using Akka Streams and Alpakka</a> was originally published in <a href="https://build.diligent.com/">Build Diligent</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>