<?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 Saeid Dadkhah on Medium]]></title>
        <description><![CDATA[Stories by Saeid Dadkhah on Medium]]></description>
        <link>https://medium.com/@saeiddadkhah?source=rss-1c55a9aab37a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*VWmBnOj5UvoZoqmmCRr-FA.jpeg</url>
            <title>Stories by Saeid Dadkhah on Medium</title>
            <link>https://medium.com/@saeiddadkhah?source=rss-1c55a9aab37a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 04:19:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@saeiddadkhah/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[Mastering Generic Self-Type Traits and Type Bounds in Scala: A Guide to Best Practices and Advanced…]]></title>
            <link>https://medium.com/codex/mastering-generic-self-type-traits-and-type-bounds-in-scala-a-guide-to-best-practices-and-advanced-3c59aa02f066?source=rss-1c55a9aab37a------2</link>
            <guid isPermaLink="false">https://medium.com/p/3c59aa02f066</guid>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Saeid Dadkhah]]></dc:creator>
            <pubDate>Mon, 23 Jan 2023 09:32:01 GMT</pubDate>
            <atom:updated>2023-01-23T13:56:27.702Z</atom:updated>
            <content:encoded><![CDATA[<h3>Mastering Generic Self-Type Traits and Type Bounds in Scala: A Guide to Best Practices and Advanced Techniques</h3><blockquote>Self-types are a way to declare that a trait must be mixed into another trait, even though it doesn’t directly extend it.</blockquote><figure><img alt="Scala — Generic Types, Traits, and Upper Type Bounds" src="https://cdn-images-1.medium.com/max/1024/1*F_dzgk7ysKd1aHHm3b15Lw.png" /><figcaption>Scala — Generic Types, Traits, self types, and Upper Type Bounds</figcaption></figure><p>The self-type definition can be utilized to access the superclass. Further information on this topic can be found <a href="https://docs.scala-lang.org/tour/self-types.html">here</a>. However, it is assumed that the reader of this blog is already familiar with the concept of self-type traits and is specifically interested in the implementation of a generic self-type trait.</p><h3>Case Study</h3><p>The presence of unnecessary whitespace within a class can negatively impact code readability and maintainability. Smart constructors or factories can be utilized to address this issue. Specialized cleaning functions for handling the cleaning process are the alternative. This allows for the preservation of both versions for different use cases.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d340da0ed6f587cb19430e5b6a7ab5bb/href">https://medium.com/media/d340da0ed6f587cb19430e5b6a7ab5bb/href</a></iframe><p>The implementation of a trait that provides a method for cleaning its subclasses can be useful in addressing the issue of unnecessary whitespace within a class. This trait can be defined as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e3c60edd4a4869be25dc3ade56bedf1/href">https://medium.com/media/7e3c60edd4a4869be25dc3ade56bedf1/href</a></iframe><p>Now, The class Mess can then extend this trait and implement the clean method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7cdea8f06f774553646115ec71ea150a/href">https://medium.com/media/7cdea8f06f774553646115ec71ea150a/href</a></iframe><p>Now, we have a clean object.</p><h3>Call only One Method</h3><p>In order to achieve a completely clean object, it may be beneficial to implement an additional method in the Cleaner trait, which utilizes the clean method in a repeated or recursive manner.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/427e9bbfa3b47a60d0be74791452cb4b/href">https://medium.com/media/427e9bbfa3b47a60d0be74791452cb4b/href</a></iframe><p>The implementation of the cleanAll method should be provided within the Mess class, which extends the Cleaner trait.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/efe4a69d0d1d38a35a0f9615ab8f0e89/href">https://medium.com/media/efe4a69d0d1d38a35a0f9615ab8f0e89/href</a></iframe><h3>Generic Self-Type Traits at Its Best</h3><p>In order to reduce code redundancy, it may be appropriate to include the implementation of the cleanAll method within the Cleaner trait. This ensures that any class that extends the Cleaner trait will have access to the cleanAll method and its implementation, without the need for duplicate code within each individual class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/430fadba2514c6ca01b87cb4fd38e559/href">https://medium.com/media/430fadba2514c6ca01b87cb4fd38e559/href</a></iframe><p>But this implementation will result in a compile-time error.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6a5b6115043aa0bbb85cc2702b75970/href">https://medium.com/media/e6a5b6115043aa0bbb85cc2702b75970/href</a></iframe><p>The assumption that the type parameter T of the Cleaner trait is a superclass of the implementing class, in this case Mess, may not always be valid. The implementation of the Mess class may specify a different type as T and still conform to the requirements of the Cleaner trait, leading to a valid implementation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66f689399aefdc2aaa91e4d13daea469/href">https://medium.com/media/66f689399aefdc2aaa91e4d13daea469/href</a></iframe><p>It is not possible to invoke the clean method on the unwantedMess object as it is not an instance of the Cleaner trait. How can we add the assumption to the implementation of Cleaner?</p><p>Upper-type bound acts as a superhero here and will save us! To further enhance encapsulation, the clean method can be designated as protected and the cleanAll method can be marked as final.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e36460d87a641486a32e4eef2ce7200/href">https://medium.com/media/3e36460d87a641486a32e4eef2ce7200/href</a></iframe><p>Incompatibility of the UnwantedMess class with the constraints set by the Cleaner trait will result in a compilation error.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3cbb086de9c33a2e8414bbaa39b3b072/href">https://medium.com/media/3cbb086de9c33a2e8414bbaa39b3b072/href</a></iframe><p>Conversely, the implementation of the Mess class is expected to conform to the constraints set by the Cleaner trait and thus will not result in compilation errors.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ea587e703d8edd559f826db2397aac1/href">https://medium.com/media/4ea587e703d8edd559f826db2397aac1/href</a></iframe><h3><strong>Use Case</strong></h3><p>I faced this issue when I was trying to implement a PATCH method in a REST server. Can you think of any other use case for this method?</p><p>A practical implementation of the generic self-type trait is available in the ‘23_update’ project within my <a href="https://github.com/SaeidDadkhah/clean-arch-example">GitHub repository</a>. Feel free to check it and open issues and PRs.</p><p><a href="https://github.com/SaeidDadkhah/clean-arch-example">GitHub - SaeidDadkhah/clean-arch-example: An implementation of Clean Architecture in Scala</a></p><p>I hope you enjoyed this tutorial. Thank you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3c59aa02f066" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/mastering-generic-self-type-traits-and-type-bounds-in-scala-a-guide-to-best-practices-and-advanced-3c59aa02f066">Mastering Generic Self-Type Traits and Type Bounds in Scala: A Guide to Best Practices and Advanced…</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementing a Clean Architecture Application in Scala — Part 2]]></title>
            <link>https://medium.com/codex/implementing-a-clean-architecture-application-in-scala-part-2-4555af428d34?source=rss-1c55a9aab37a------2</link>
            <guid isPermaLink="false">https://medium.com/p/4555af428d34</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Saeid Dadkhah]]></dc:creator>
            <pubDate>Sat, 09 Jul 2022 10:16:06 GMT</pubDate>
            <atom:updated>2022-07-09T17:01:59.872Z</atom:updated>
            <content:encoded><![CDATA[<h3>Implementing a Clean Architecture Application in Scala — Part 2</h3><p>The Clean Architecture is a set of guidelines proposed by Robert C. Martin (Uncle Bob). I’ve written about this architecture and described our style in one of my previous posts “<a href="https://saeiddadkhah.medium.com/clean-architecture-in-scala-41d1ab05a618">Clean Architecture in Scala</a>,” and I recommend you read it before starting this one. The bellow schematic is our style of clean architecture.</p><figure><img alt="Our Clean Architecture" src="https://cdn-images-1.medium.com/max/601/1*VvEDFkd5-7crH_PHzw34Mg.png" /><figcaption>Our Clean Architecture</figcaption></figure><p><a href="https://medium.com/codex/clean-architecture-in-scala-41d1ab05a618">Clean Architecture in Scala</a></p><p>I’ve implemented a small blog application like Medium available on my <a href="https://github.com/SaeidDadkhah/clean-arch-example">GitHub repository</a>. This implementation is not a performant one, and its only usage is to describe concepts provided here.</p><p><a href="https://github.com/SaeidDadkhah/clean-arch-example">GitHub - SaeidDadkhah/clean-arch-example: An implementation of Clean Architecture in Scala</a></p><p>This blog is totally based on my previous post “<a href="https://saeiddadkhah.medium.com/implementing-a-clean-architecture-application-in-scala-part-1-1442f0438b03">Implementing a Clean Architecture Application in Scala — Part 1</a>,” so make sure to read it and understand the concepts described there.</p><p><a href="https://medium.com/codex/implementing-a-clean-architecture-application-in-scala-part-1-1442f0438b03">Implementing a Clean Architecture Application in Scala — Part 1</a></p><h3>Application Layer - Use Cases</h3><p>The primary layer of application is the use case layer which is part of the application layer. The full implementation of this layer is provided in 04_application project in 04_application directory. The structure of com.github.saeiddadkhah.application package is completely the same as the structure of com.github.saeiddadkhah.contract.service package. There is a use case inside application package for each service inside the packages of service package with the same name, and each use case extends its correspondence service and implements its call method.</p><figure><img alt="The structure of application package" src="https://cdn-images-1.medium.com/max/220/1*t8Tsp5mPh9QeT9M-fXTjzA.png" /><figcaption>The structure of the use cases of the application layer</figcaption></figure><p>Anyone who wants to use the application should sign-up at first, so it is the first use case anyone interacts with and a good point to start inspecting the architecture for us.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e22766ed4b1fed00bd434cc9a583277/href">https://medium.com/media/5e22766ed4b1fed00bd434cc9a583277/href</a></iframe><p>Here is the implementation of the sign-up use case. The sign-up service has been defined as abstract class SignUpService extends Service[SignUpService.Request, User], so the signature of its call method is def call(request: SignUpService.Request)(implicit ec: ExecutionContext): Future[User], and I override it in SignUpUseCase.</p><p>Here, I hash the password provided by the user, check username availability, and create the user. In a real implementation, you may check password strength, validate username and e-mail, and do some other validations, before adding the user. The result of checking the availability of the username is an instance of Future[Unit]. In case of unavailability of the username, instead of throwing any kind of exception, we return an instance of failed Future to follow the principles of functional programming.</p><p>This use case requires to access users to validate username availability and add the user. We add userCallback: UserCallback parameter to the constructor to show this requirement. As the dependency inversion principle states, the important point is to use callbacks <strong><em>(NOT repositories)</em></strong> to specify these requirements. If the use case uses other services, we should add the services <strong><em>(NOT use cases)</em></strong> to the constructor. For example, suppose that we should change the implementation such that the service returns an instance of Session class, and we want to use SignInService. We can modify the above code, use SignInService, and call it at the end of the implementation of the call method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e9a057ae9b721b6780be198c8f345fcd/href">https://medium.com/media/e9a057ae9b721b6780be198c8f345fcd/href</a></iframe><h3>Configuration</h3><p>Before talking about the repositories, let me explain the modules. There are three essential modules ConfigModule, CallbackModule, and ServiceModule. There should be also exactly one module for each type of external data source, e.g. database and REST API.</p><h4>Config Module</h4><p>Config module provides several methods to read some data related to the environment that the application runs in. For example, if we want to use different databases in test and production environments, we can use different config files to set connection information related to each environment without changing the code. Although using a set of environment variables seems to be a good option, there are better options because of the capability of structuring config entries, e.g. JSON and XML files. We use theconfig library of com.typesafe to load configs. It utilizes <a href="https://github.com/lightbend/config/blob/main/HOCON.md">HOCON</a> files to load configs. Here is the implementation of our ConfigModule.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/275aaad00118dcbd8a14b529cda8ab50/href">https://medium.com/media/275aaad00118dcbd8a14b529cda8ab50/href</a></iframe><p>There are multiple ways to specify the config file which we want to run the application with. Expectedly, the config library offers the c.t.c.ConfigFactory.parseFile function to parse the config file. You may set an environment variable to specify the location of the config file and load it using this function. A more elegant method is to use the-DConfig.file JVM option and use this function ConfigFactory.load().withFallback(ConfigFactory.defaultApplication()).resolve.</p><p>An important <strong>security</strong> point is <strong><em>NOT</em></strong> to commit config files. Put some entries to ignore config files in .gitignore file, e.g. application.conf, application.staging*.conf, and application.prodcution*.conf. Since you should specify the required config entries and provide sample values and possible valid values for enums, you can include the application.template.conf file in the repository and fill it with sample values.</p><h4>Callback and Service Modules</h4><p>We use these modules to bind implementations to abstractions. Here is the implementation of ServiceModule. We use callback instances imported from CallbackModule instead of instantiating repositories here. In this implementation, we instantiate each use case only once which makes them singleton. It may be a better idea to make the value lazy. If you want to use a dependency injection library, you should bind implementations to abstractions in these files.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a2cb686df0ff94fa90a187cb0a21853/href">https://medium.com/media/5a2cb686df0ff94fa90a187cb0a21853/href</a></iframe><h4>Simple in-Memory Module</h4><p>In this module, I implemented a simple in-memory database that is not performant.</p><h3>Adapter Layer - Repositories</h3><p>We should create a specific package for each type of data source in this package. We are currently using only the in-memory data source, so we have only one package inside repository package. Each repository package may contain two packages named repository and adapter. The in-memory data source does not include an adapter package because its implementation takes a type parameter, and we are using it with domain classes. The package structure of the adapter and repository should be the same as the domain and callback, respectively.</p><figure><img alt="The structure of the repositories of the adapter layer" src="https://cdn-images-1.medium.com/max/214/1*VOyprlA2iZF9ZGqNNv1vlw.png" /><figcaption>The structure of the repositories of the adapter layer</figcaption></figure><figure><img alt="The structure of the repositories of the adapter layer with multiple data source types and adapter package" src="https://cdn-images-1.medium.com/max/223/1*ylsxYlXGXhXpLL-nFwznQg.png" /><figcaption>The structure of the repositories of the adapter layer with multiple data source types and adapter package</figcaption></figure><p>Repository classes extend their corresponding callback and module and implement their abstract members. Here is the definition of UserRepository.</p><pre>class UserRepository extends UserCallback with SimpleInMemoryModule[User] {<br>   ???<br>}</pre><h3>Using the Application</h3><p>The application is ready to use right now. You should use only the service instances provided in the service module or get service instances from the dependency injection framework, and call only call method. For example, if you are implementing a REST server, you can use a service for each endpoint and authentication and authorization services to control users’ access to endpoints and filter requests.</p><p>Stay tuned for the next posts to add entries, e.g. REST API. Feel free to check the <a href="https://github.com/SaeidDadkhah/clean-arch-example">GitHub repository</a> and open issues. I hope you enjoyed this tutorial. Thank you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4555af428d34" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/implementing-a-clean-architecture-application-in-scala-part-2-4555af428d34">Implementing a Clean Architecture Application in Scala — Part 2</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementing a Clean Architecture Application in Scala — Part 1]]></title>
            <link>https://medium.com/codex/implementing-a-clean-architecture-application-in-scala-part-1-1442f0438b03?source=rss-1c55a9aab37a------2</link>
            <guid isPermaLink="false">https://medium.com/p/1442f0438b03</guid>
            <category><![CDATA[clean-architecture]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[scala]]></category>
            <dc:creator><![CDATA[Saeid Dadkhah]]></dc:creator>
            <pubDate>Tue, 26 Apr 2022 17:02:37 GMT</pubDate>
            <atom:updated>2022-04-26T17:39:32.636Z</atom:updated>
            <content:encoded><![CDATA[<h3>Implementing a Clean Architecture Application in Scala — Part 1</h3><p>The Clean Architecture is a set of guidelines proposed by Robert C. Martin (Uncle Bob), and I will implement an application in this post using these guidelines. Although The Clean Architecture is just a set of guidelines, and any project may have its version, the important principle is to implement a version in the team of engineers and stick to it in all projects. This schematic is our Clean Architecture style.</p><figure><img alt="Our Clean Architecture" src="https://cdn-images-1.medium.com/max/601/1*VvEDFkd5-7crH_PHzw34Mg.png" /><figcaption>Our Clean Architecture</figcaption></figure><p>I’ve already described our style in my previous post “<a href="https://saeiddadkhah.medium.com/clean-architecture-in-scala-41d1ab05a618">Clean Architecture in Scala</a>.” This tutorial is completely based on my previous post, so please read it, and make sure you are familiar with the structure of the project.</p><p><a href="https://medium.com/codex/clean-architecture-in-scala-41d1ab05a618">Clean Architecture in Scala</a></p><p>I’ve implemented a small application like Medium available on my <a href="https://github.com/SaeidDadkhah/clean-arch-example">GitHub repository</a>! This implementation is not a performant one, and its only usage is to describe concepts provided here.</p><p><a href="https://github.com/SaeidDadkhah/clean-arch-example">GitHub - SaeidDadkhah/clean-arch-example: An implementation of Clean Architecture in Scala</a></p><h3>Enterprise Layer - Domain</h3><p>An implementation always should begin with the enterprise layer. The full implementation of this layer is provided in 02_domain project in ./02_domain directory. There are two packages inside com.github.saeiddadkhah.domain package. auth package contains User and Session classes, and blog package contains Post, Paragraph, Clap, and Comment classes.</p><figure><img alt="The structure of domain package" src="https://cdn-images-1.medium.com/max/155/1*mMSNeMSuzZFHtlAVe_LINg.png" /><figcaption>The structure of domain package</figcaption></figure><p>One of the best practices for the structure of this package is to put all classes inside a special package; for example, User class. Although authentication and authorization are not the only usages for User class, we put this class inside auth package. Let’s implement entities. The simplest entities have only some parameters, e.g., Clap and Session classes. The parameters of entities can be only primitives, basic Java or Scala classes, and entities.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a194dbd52781cf38a97e9fc1cf851100/href">https://medium.com/media/a194dbd52781cf38a97e9fc1cf851100/href</a></iframe><p>Some entities may have a set of simple functionalities and validations, e.g., Post, Clap, and User classes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd9de61d2eb0d62f2543840b43196385/href">https://medium.com/media/dd9de61d2eb0d62f2543840b43196385/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50364465ede810c4296546c52735c0ea/href">https://medium.com/media/50364465ede810c4296546c52735c0ea/href</a></iframe><p>Some entities may have different types each with its special data and functionalities, e.g., Paragraph class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6e083ce61346b9f96b8d80078871c0ed/href">https://medium.com/media/6e083ce61346b9f96b8d80078871c0ed/href</a></iframe><p>I implemented these three types to keep the tutorial simple, however, depending on the use case, you may use each of these types or even combine them, for example, you may have an abstract class with attributes, methods, and validations and several sub-classes.</p><h3>Application Layer - Contracts</h3><p>Building the application layer starts with setting the contracts. 03_contract project is a copy of 02_domain project and the contracts of the application layer. Here is the structure of this part of the project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/231/1*DNy47HiyYdbmyNG-YebWEg.png" /><figcaption>The structure of the contracts of the application layer</figcaption></figure><p>The structure of packages inside callback and service packages are exactly the same as the structure of packages inside domain, and callbacks and services defined for a class are in the same packages as the class. The following code snippet is the complete definition of publish service. Request class is defined in the companion object of the service to specify inputs to the service, and PublishPostService extends Service and specifies the type of Request and Response. According to the definition of Service, any instance of this <em>abstract</em> class has <strong>only one</strong> method named call with a Request parameter and an implicit scala.concurrent.ExecutionContext parameter and will return an instance of Future[Post].</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9257fae760ea1a512d903e8c3c90e402/href">https://medium.com/media/9257fae760ea1a512d903e8c3c90e402/href</a></iframe><p>Callbacks, unlike services, does not extends any other class and may have several methods. Methods defined in callbacks are often simple CRUD (Create, Read, Update, and Delete) operations. Another difference between callbacks and services is that we may define several services on an entity, but there is <strong>only one</strong> callback for an entity - here we have AuthorizeService, SignInService, SignOutService, and SignUpService and only SessionCallback for Session class, for example. Here is the definition of PostCallback.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ced2682d0de0549ecf6db085aaec2cbb/href">https://medium.com/media/ced2682d0de0549ecf6db085aaec2cbb/href</a></iframe><p>You are not supposed to know exactly which methods the callback should contain. You can add methods as you implement use cases. One of the best practices for defining get methods is to wrap the response inside Option class to explicitly specify the case that there is no data with the provided criteria. Another practice is to consider limit and offset parameters for the methods expected to return a list of elements.</p><p>Stay tuned for the next posts to implement the rest of the application. Feel free to check the <a href="https://saeiddadkhah.medium.com/clean-architecture-in-scala-41d1ab05a618">previous post</a> and <a href="https://github.com/SaeidDadkhah/clean-arch-example">GitHub repository</a> and open issues. I hope you enjoyed this tutorial. Thank you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1442f0438b03" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/implementing-a-clean-architecture-application-in-scala-part-1-1442f0438b03">Implementing a Clean Architecture Application in Scala — Part 1</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting up a Multi-Node Apache Spark Cluster on Apache Hadoop and Apache Hive]]></title>
            <link>https://medium.com/codex/setting-up-a-multi-node-apache-spark-cluster-on-apache-hadoop-and-apache-hive-412764ab6881?source=rss-1c55a9aab37a------2</link>
            <guid isPermaLink="false">https://medium.com/p/412764ab6881</guid>
            <category><![CDATA[spark]]></category>
            <category><![CDATA[hadoop]]></category>
            <category><![CDATA[big-data]]></category>
            <category><![CDATA[hive]]></category>
            <category><![CDATA[data-engineering]]></category>
            <dc:creator><![CDATA[Saeid Dadkhah]]></dc:creator>
            <pubDate>Tue, 12 Apr 2022 14:52:23 GMT</pubDate>
            <atom:updated>2022-04-12T15:56:47.612Z</atom:updated>
            <content:encoded><![CDATA[<p>Apache Spark™ is a multi-language engine for executing data engineering, data science, and machine learning on single-node machines or clusters. In this post, I will set up Apache Spark 3.0.1 on Apache Hadoop 3.3.0. Since there should be a running Apache Hadoop cluster to set up an Apache Spark cluster, I recommend using my previous blog post <a href="https://medium.com/codex/running-a-multi-node-hadoop-cluster-257068e5f276">Running a Multi-Node Hadoop Cluster</a> to set up your Apache Hadoop cluster.</p><p><a href="https://medium.com/codex/running-a-multi-node-hadoop-cluster-257068e5f276">Running a Multi-Node Hadoop Cluster</a></p><figure><img alt="Apache Spark" src="https://cdn-images-1.medium.com/max/1024/1*rq98phowGgaUatq-Hx90PQ.png" /><figcaption><a href="https://spark.apache.org/">Apache Spark</a></figcaption></figure><p>I may use some Scala codes and CentOS (RHEL) commands.</p><h3>Setup Apache Spark Environment</h3><p>After running the Hadoop framework, we can start setting up Apache Spark. I will assume that you have set up the Hadoop framework using my post <a href="https://medium.com/codex/running-a-multi-node-hadoop-cluster-257068e5f276">Running a Multi-Node Hadoop Cluster</a> to be on the same page. Now, download and copy the file to all workers.</p><pre>[hduser@master ~]# wget {Apache Spark LINK (I downloaded spark-3.0.1-bin-hadoop3.2.tgz)}<br>[hduser@master ~]# scp spark-3.0.1-bin-hadoop3.2.tgz hduser@worker1:/home/hduser/spark-3.0.1-bin-hadoop3.2.tgz<br>[hduser@master ~]# scp spark-3.0.1-bin-hadoop3.2.tgz hduser@worker2:/home/hduser/spark-3.0.1-bin-hadoop3.2.tgz</pre><p><strong>On all servers</strong>, extract Apache Spark, and create a soft link to be able to change its version simply in the future. I will extract all files in the /opt directory.</p><pre>[hduser@{server} ~]# cd /opt<br>[hduser@{server} /opt]# sudo tar xzf /home/hduser/spark-3.0.1-bin-hadoop3.2.tgz<br>[hduser@{server} /opt]# sudo ln -s spark-3.0.1-bin-hadoop3.2/ spark<br>[hduser@{server} /opt]# sudo chown -R hduser:hadoop spark<br>[hduser@{server} /opt]# sudo chown -R hduser:hadoop spark-3.0.1-bin-hadoop3.2</pre><h3>Configure Apache Spark</h3><p>Before starting configuring process, I should mention that configuring the Apache Spark cluster on an Apache Hadoop cluster is a two-step process. At first, Spark should know how Hadoop is working, then it should know how it is supposed to work! Let me show you the code. Copy the config files of Hadoop to the config directory of Spark on all servers. All Spark config files are available under the directory /pot/spark/conf.</p><pre>[hduser@{server} ~]# cd /pot/spark/conf<br>[hduser@{server} /opt/spark/conf]# cp /opt/hadoop/etc/hadoop/core-site.xml .<br>[hduser@{server} /opt/spark/conf]# cp /opt/hadoop/etc/hadoop/hdfs-site.xml .<br>[hduser@{server} /opt/spark/conf]# cp /opt/hadoop/etc/hadoop/yarn-site.xml .</pre><p>On the master server, edit the config files of Spark and copy them to workers.</p><pre>[hduser@master ~]# cd /pot/spark/conf<br>[hduser@master /opt/spark/conf]# cp slaves.template slaves<br>[hduser@master /opt/spark/conf]# cp spark-env.sh.template spark-env.sh<br>[hduser@master /opt/spark/conf]# cp log4j.properties.template log4j.properties<br>[hduser@master /opt/spark/conf]# vi slaves</pre><pre># Add the names of all workers<br>master<br>worker1<br>worker2</pre><pre>[hduser@master /opt/spark/conf]# vi spark-env.sh</pre><pre># Add these lines<br>export JAVA_HOME=/opt/jdk</pre><pre>export SPARK_HOME=/opt/spark<br>export SPARK_CONF_DIR=$SPARK_HOME/conf<br>export HADOOP_HOME=/opt/hadoop<br>export HADOOP_CONF_DIR=$HADOOP_HOME/etc/hadoop<br>export YARN_CONF_DIR=$HADOOP_HOME/etc/hadoop</pre><pre>export SPARK_WORKER_DIR=/tmp/spark/work</pre><pre>[hduser@master /opt/spark/conf]# vi log4j.properties</pre><pre># Add this line<br>log4j.logger.org=OFF</pre><pre>[hduser@master /opt/spark/conf]# scp log4j.properties spark-env.sh hduser@{slaves}:/opt/spark/conf/</pre><p>Finally, edit .bashrc file, and add these lines.</p><pre># Spark<br>export SPARK_HOME=/opt/spark<br>export SPARK_CONF_DIR=$SPARK_HOME/conf<br>export PATH=$SPARK_HOME/bin:$PATH</pre><h3>Provide Apache Spark JAR Files</h3><p>Configuring Apache Spark is finished but before submitting any Spark application we should provide Spark JAR files. Simply, run these commands on any server!</p><pre>[hduser@{server} ~]# hdfs dfs -mkdir -p /user/spark/share/lib<br>[hduser@{server} ~]# hadoop fs -put /opt/spark/jars/* /user/spark/share/lib/</pre><p>This point is where you can submit your Spark applications and use Apache HDFS and Apache Yarn using a command like this one on any server.</p><pre>[hduser@{server} ~]# /opt/spark/bin/spark-submit \<br>--master yarn --deploy-mode cluster \<br>--class com.github.saeiddadkhah.hadoop.spark.Application \<br>--driver-memory 2G --driver-cores 2 \<br>--executor-memory 1G --executor-cores 1\<br>--name MyApp \<br>--num-executors 10 \<br>MyApp.jar</pre><p>You should provide some other configurations to Apache Spark inside your application. The following code is a Scala code but Apache Spark has a similar API for the other languages.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5005cc41bba886fcf0b0a3e3c1373a4f/href">https://medium.com/media/5005cc41bba886fcf0b0a3e3c1373a4f/href</a></iframe><h3>Add Apache Hive support</h3><p>Deciding to use Apache Hive in your Spark application or not is your responsibility, and it depends completely on the use case and your preferences. Here, I will just configure Apache Spark to use Apache Hive 3.1.2. Naturally, we need a running Apache Hive, and the good news is you can use my previous post “<a href="https://medium.com/codex/setting-up-an-apache-hive-data-warehouse-6074775cf66">Setting up an Apache Hive Data Warehouse</a>” to set it up.</p><p><a href="https://medium.com/codex/setting-up-an-apache-hive-data-warehouse-6074775cf66">Setting up an Apache Hive Data Warehouse</a></p><p>Now, copy the config file of Hive to the config directory of Spark on all servers.</p><pre>[hduser@{server} ~]# cd /pot/spark/conf<br>[hduser@{server} /opt/spark/conf]# cp /opt/hive/conf/hive-site.xml .</pre><p>You should also add other configs inside your application to enable Hive support.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc1db70d85db4a3d5319a4377aba42ff/href">https://medium.com/media/bc1db70d85db4a3d5319a4377aba42ff/href</a></iframe><p>Stay tuned for the next posts to set up more tools. I hope you enjoyed this tutorial. Thank you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=412764ab6881" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/setting-up-a-multi-node-apache-spark-cluster-on-apache-hadoop-and-apache-hive-412764ab6881">Setting up a Multi-Node Apache Spark Cluster on Apache Hadoop and Apache Hive</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Clean Architecture in Scala]]></title>
            <link>https://medium.com/codex/clean-architecture-in-scala-41d1ab05a618?source=rss-1c55a9aab37a------2</link>
            <guid isPermaLink="false">https://medium.com/p/41d1ab05a618</guid>
            <category><![CDATA[sofware-development]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[scala]]></category>
            <dc:creator><![CDATA[Saeid Dadkhah]]></dc:creator>
            <pubDate>Mon, 28 Mar 2022 10:59:25 GMT</pubDate>
            <atom:updated>2022-03-28T11:42:49.009Z</atom:updated>
            <content:encoded><![CDATA[<p>First things first, what is Clean Architecture? Robert C. Martin (Uncle Bob) claims that although system architectures vary in their details, they are very similar. Hexagonal Architecture and Onion Architecture are the popular ones. These architectures will lead programmers to produce systems with these characteristics:</p><ul><li>Independent of frameworks</li><li>Testable</li><li>Independent of UI</li><li>Independent of database</li><li>Independent of any external agency</li></ul><p>You can read more about this in his <a href="https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html">blog</a> or his book <em>Clean Architecture: A Craftsman’s Guide to Software Structure and Design</em>.</p><h3>Our Style</h3><p>Although everyone expects an architecture to provide a set of exact components and dictate to them what to do, Clean Architecture is a guideline that let us modify and utilize it. This schematic is our Clean Architecture style.</p><figure><img alt="Our Clean Architecture" src="https://cdn-images-1.medium.com/max/601/1*VvEDFkd5-7crH_PHzw34Mg.png" /><figcaption>Our Clean Architecture</figcaption></figure><p>As you can see it is very similar to Uncle Bob’s style. It consists of four layers named enterprise, application, adapter, and infrastructure layers.</p><p>The enterprise layer that contains domain entities and domain-related use cases is the underlying layer of the application layer. On the other hand, the schematic tries to show the architecture and the flow of a request at the same time, given that domain plays no role in the flow, so entities are not part of the schematic. However, the yellow box should be considered as enterprise territory, then any object entering this part of the architecture should be either a defined entity or primitive data. Converting <a href="https://en.wikipedia.org/wiki/Data_transfer_object">Data Transfer Objects (DTO)</a> and <a href="https://en.wikipedia.org/wiki/Data_access_object">Data Access Objects (DAO)</a> to domain objects at the border of this area is the role of factories. Each external interface and repository may have its own DTO or DAO, so it is essential to create an adapter package for each.</p><figure><img alt="Uncle Bob’s Clean Architecture" src="https://cdn-images-1.medium.com/max/772/1*B7LkQDyDqLN3rRSrNYkETA.jpeg" /><figcaption>Uncle Bob’s Clean Architecture</figcaption></figure><h3>Scala Template</h3><p>Before implementing an application, I want to create a template. All the following structures and codes are available in <a href="https://github.com/SaeidDadkhah/clean-arch-example">my GitHub repository</a>. Feel free to create issues and PRs.</p><p>This project contains multiple subprojects. Here, I will describe the template project in ./01_template. The following image is the packaging of the project.</p><figure><img alt="01. Template — Packaging" src="https://cdn-images-1.medium.com/max/293/1*pRljkChf83d41FWz2r05Ww.png" /><figcaption>01. Template — Packaging</figcaption></figure><h3>Enterprise Layer</h3><p>Constructing an application need a starting point and Application.scala is the starting point of our application. domain package is the most important package and the core of our application which will contain entities and enterprise rules. To show its relation with the architecture, it is the only package in the enterprise layer.</p><h3>Application Layer</h3><p>Definitely, programmers will put a fair amount of logic and effort into this layer, so it is a good idea to use some abstractions here. The application layer consists of application and contract packages, andcontract package contains only abstractions. service package is one of the packages in contract package. The definition of all use cases will be in this package, and all of them should inherit thec.s.Service class. Here is the implementation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/862137a9c80477f94de73939404161f9/href">https://medium.com/media/862137a9c80477f94de73939404161f9/href</a></iframe><p>Service abstract class is the definition of what should we expect when a service is called. Every service should have only one public member to use it. All results and responses of services should be wrapped in a container class to let us handle exceptions in a functional way. Try and Either can be our potential options but they have one critical drawback which is being synchronous. The best option without using any external framework and library is to use Scala’s Future. Therefore each service has a method call getting a parameter and an ExecutionContext and returning a Future object. For example, we can use an instance of CheckUserService[String, Boolean] like this checkUserService.call(&quot;Saeid&quot;)and expect a Future[Boolean] object.</p><p>callback package will contain abstract callback classes for almost all entities with the signature of simple CRUD operations. We will put the implementation of all services in application package.</p><h3>Adapter Layer</h3><p>Enterprise and application layers will do the job but we need to trigger them and store side effects and results. Adapter layer is responsible to connect logic with infrastructure. entry package is, as its name indicates, where we connect external interfaces to services <strong><em>(NOT use cases)</em></strong>, and we can use data sources in repository package. If we decide to change the database at some point, it will affect only here. If both the old and the new databases support the same connector, let’s say JDBC, it will not affect even here. Therefore it is the reason Uncle Bob claims it is <strong><em>independent of database</em></strong>.</p><h3>Infrastructure Layer and Configuration</h3><p>Finalizing the template is easy now. We need to connect our application to the infrastructure, and we only need a set of configurations to do that. It is fascinating that I didn&#39;t talk about frameworks until right now, and that is why Uncle Bob claims that it is <strong><em>independent of frameworks</em></strong><em> </em>and <strong><em>independent of UI</em></strong>.</p><p>Stay tuned for the next posts to implement an application. Feel free to check <a href="https://github.com/SaeidDadkhah/clean-arch-example">the GitHub repository</a> and open issues. I hope you enjoyed this tutorial. Thank you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41d1ab05a618" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/clean-architecture-in-scala-41d1ab05a618">Clean Architecture in Scala</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting up an Apache Hive Data Warehouse]]></title>
            <link>https://medium.com/codex/setting-up-an-apache-hive-data-warehouse-6074775cf66?source=rss-1c55a9aab37a------2</link>
            <guid isPermaLink="false">https://medium.com/p/6074775cf66</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[big-data]]></category>
            <category><![CDATA[hadoop]]></category>
            <category><![CDATA[apache]]></category>
            <category><![CDATA[data-engineering]]></category>
            <dc:creator><![CDATA[Saeid Dadkhah]]></dc:creator>
            <pubDate>Mon, 09 Aug 2021 10:38:14 GMT</pubDate>
            <atom:updated>2021-08-29T13:38:43.734Z</atom:updated>
            <content:encoded><![CDATA[<p>The Apache Hive™ data warehouse software facilitates reading, writing, and managing large datasets using SQL in Hadoop Distributed File System. In this post, I will set up Apache Hive 3.1.2 on Apache Hadoop 3.3.0. Hive requires a ready-to-go Hadoop framework. I strongly recommend you to set up your Hadoop framework using the post that I’ve already published named <a href="https://medium.com/codex/running-a-multi-node-hadoop-cluster-257068e5f276">Running a Multi-Node Hadoop Cluster</a>.</p><p><a href="https://medium.com/codex/running-a-multi-node-hadoop-cluster-257068e5f276">Running a Multi-Node Hadoop Cluster</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nd79weIivnt7f43-mlP-DQ.png" /><figcaption><a href="https://hive.apache.org/">Apache Hive</a></figcaption></figure><p>Apache Hive uses a database to store its metadata. I will use a <a href="https://www.postgresql.org/">Postgres</a> database as the external metastore thus we should install a Postgres database as our first step. I may use some CentOS (RHEL) commands. We will go through the following steps to run Apache Hive.</p><ol><li>Configure Postgres Database</li><li>Configure Apache Hive</li><li>Resolve Some Errors</li><li>Start Apache Hive</li></ol><h3>Configure Postgres Database</h3><p>After running the Hadoop framework, we can start setting up Apache Hive. I will assume that you have set up the Hadoop framework using my post <a href="https://medium.com/codex/running-a-multi-node-hadoop-cluster-257068e5f276">Running a Multi-Node Hadoop Cluster</a> to be on the same page. Select one of the Hadoop nodes as the Hive server and install Postgres on it. Apache Hive uses an execution engine (Map-Reduce as default, Tez, or Spark) to run queries distributedly although it will be available on only one server to use. Now, let&#39;s configure the Postgres database.</p><pre>[root@hiveserver ~]# sudo -u postgres psql</pre><pre>postgres=# create user hive_user with encrypted password &#39;hive_password&#39;;<br>postgres=# create database hive_database;<br>postgres=# grant all privileges on database hive_database to hive_user;</pre><h3>Configure Apache Hive</h3><p>Download and extract Hive.</p><pre>[hduser@hiveserver ~]# wget {Apache Hive LINK (I downloaded apache-hive-3.1.2-bin.tar.gz)}<br>[hduser@hiveserver ~]# cd /opt<br>[hduser@hiveserver /opt]# sudo tar xzf /home/hduser/apache-hive-3.1.2-bin.tar.gz<br>[hduser@hiveserver /opt]# sudo ln -s apache-hive-3.1.2-bin/ hive<br>[hduser@hiveserver /opt]# sudo chown -R hduser:hadoop hive<br>[hduser@hiveserver /opt]# sudo chown -R hduser:hadoop apache-hive-3.1.2-bin</pre><p>All Hive config files are available under the directory /pot/hive/conf. Now, edit Hive config files.</p><pre>[hduser@hiveserver ~]# cd /opt/hive/conf/<br>[hduser@hiveserver /opt/hive/conf]# cp hive-env.sh.template hive-env.sh<br>[hduser@hiveserver /opt/hive/conf]# vi hive-env.sh</pre><pre># Add these commands<br>HADOOP_HOME=/opt/hadoop<br>export HIVE_HOME=/opt/hive<br>export HIVE_CONF_DIR=${HIVE_HOME}/conf</pre><pre>[hduser@hiveserver /opt/hive/conf]# cp hive-default.xml.template hive-site.xml<br>[hduser@hiveserver /opt/hive/conf]# vi hive-site.xml</pre><pre># Add these elements<br>  &lt;property&gt;<br>    &lt;name&gt;system:java.io.tmpdir&lt;/name&gt;<br>    &lt;value&gt;/tmp/hive/java&lt;/value&gt;<br>  &lt;/property&gt;<br>  &lt;property&gt;<br>    &lt;name&gt;system:user.name&lt;/name&gt;<br>    &lt;value&gt;${user.name}&lt;/value&gt;<br>  &lt;/property&gt;</pre><pre># Find javax.jdo.option.ConnectionUserName property and change its value to username of Postgres database: hive_user</pre><pre># Find javax.jdo.option.ConnectionPassword property and change its value to username of Postgres database: hive_password</pre><pre># Find javax.jdo.option.ConnectionURL property and change its value to username of Postgres database: jdbc:postgresql://hiveserver:5432/hive_database</pre><pre># Find hive.server2.enable.doAs property and change its value to false</pre><p>Hive uses some directories in HDFS as a temporary one or to store data.</p><pre>[hduser@hiveserver ~]# # Create Hive directories<br>[hduser@hiveserver ~]# ## Create warehouse and temp directories<br>[hduser@hiveserver ~]# hdfs dfs -mkdir /tmp<br>[hduser@hiveserver ~]# hdfs dfs -chmod g+w /tmp<br>[hduser@hiveserver ~]# hdfs dfs -mkdir -p /user/hive/warehouse<br>[hduser@hiveserver ~]# hdfs dfs -chmod g+w /user/hive/warehouse<br>[hduser@hiveserver ~]# ## Check permissions to be drwxrwxr-x<br>[hduser@hiveserver ~]# hdfs dfs -ls /<br>[hduser@hiveserver ~]# hdfs dfs -ls /user/hive</pre><p>It is time to set environment variables.</p><pre>[hduser@hiveserver ~]# vi .bashrc</pre><pre># Add these commands<br># Hive<br>export HIVE_HOME=/opt/hive<br>export HIVE_CONF_DIR=$HIVE_HOME/conf<br>export PATH=$HIVE_HOME/bin:$PATH</pre><p>OK! Get a rest!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xw5Q9a7sifdgVf8x" /><figcaption>Photo by <a href="https://unsplash.com/@mohammaddadkhah?utm_source=medium&amp;utm_medium=referral">Mohammad Dadkhah</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Resolve Some Errors</h3><p>Although we have configured Hive correctly, you may face some errors. The first one is a peculiar RuntimeException of type com.ctc.wstx.WstxParsingException.</p><pre>Exception in thread &quot;main&quot; java.lang.RuntimeException: com.ctc.wstx.exc.WstxParsingException: Illegal character entity: expansion character</pre><p>I don’t know exactly why it should happen because we copied template config files and edited them. But the solution is simple, we need to remove one special character in hive-site.xml.</p><pre>[hduser@hiveserver /opt/hive/conf]# vi hive-site.xml</pre><pre># Find hive.txn.xlock.iow property and delete the special character between for and transactional words in its description.</pre><p>The other error is more peculiar.</p><pre>Exception in thread &quot;main&quot; java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;Ljava/lang/Object;)</pre><p>This error happens because of inconsistency among library versions. Resolving this error requires a small version controlling. Check version of guava library in Apache Hive and Apache Hadoop, and copy the latest to the other.</p><pre>[hduser@hiveserver ~]# ls /opt/hive/lib/ | grep guava<br>guava-19.0.jar</pre><pre>[hduser@hiveserver ~]# ls /opt/hadoop/share/hadoop/hdfs/lib/ | grep guava<br>guava-27.0-jre.jar</pre><p>In my case, I should copy the guava jar file from Hadoop libraries to Hive libraries.</p><pre>[hduser@hiveserver ~]# mv /opt/hive/lib/guava-19.0.jar /opt/hive/lib/guava-19.0.jar.bak<br>[hduser@hiveserver ~]# cp /opt/hadoop/share/hadoop/hdfs/lib/guava-27.0-jre.jar /opt/hive/lib/guava-27.0-jre.jar</pre><p>Hopefully, there will be no more errors. Let’s start Hive.</p><h3>Start Apache Hive</h3><p>Use these commands to start the Hive server and meta store server.</p><pre>[hduser@hiveserver ~]# hiveserver2<br>[hduser@hiveserver ~]# hive --service metastore</pre><p>You can also create services to control the Hive server and meta store server.</p><pre>[hduser@hiveserver ~]# cd /etc/systemd/system<br>[hduser@hiveserver /etc/systemd/system]# sudo vi hive.service</pre><pre># Add these lines<br>[Unit]<br>Description=Hive Server 2<br>After=syslog.target network.target remote-fs.target nss-lookup.target network-online.target<br>Requires=network-online.target hdfs.service yarn.service</pre><pre>[Service]<br>User=hduser<br>Group=hadoop<br>Type=simple<br>ExecStart=/opt/hive/bin/hiveserver2<br>WorkingDirectory=/home/hduser<br>TimeoutStartSec=2min<br>Restart=on-failure</pre><pre>[Install]<br>WantedBy=multi-user.target</pre><pre>[hduser@hiveserver /etc/systemd/system]# sudo vi hive-metastore.service</pre><pre># Add these lines<br>[Unit]<br>Description=Hive Meta Store Server<br>After=syslog.target network.target remote-fs.target nss-lookup.target network-online.target<br>Requires=network-online.target hdfs.service yarn.service hive.service</pre><pre>[Service]<br>User=hduser<br>Group=hadoop<br>Type=simple<br>ExecStart=/opt/hive/bin/hive --service metastore<br>WorkingDirectory=/home/hduser<br>TimeoutStartSec=2min<br>Restart=on-failure</pre><pre>[Install]<br>WantedBy=multi-user.target</pre><p>I hope this tutorial was useful. Thank You.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6074775cf66" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/setting-up-an-apache-hive-data-warehouse-6074775cf66">Setting up an Apache Hive Data Warehouse</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Running a Multi-Node Hadoop Cluster]]></title>
            <link>https://medium.com/codex/running-a-multi-node-hadoop-cluster-257068e5f276?source=rss-1c55a9aab37a------2</link>
            <guid isPermaLink="false">https://medium.com/p/257068e5f276</guid>
            <category><![CDATA[hadoop]]></category>
            <category><![CDATA[data-engineering]]></category>
            <category><![CDATA[apache]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[big-data]]></category>
            <dc:creator><![CDATA[Saeid Dadkhah]]></dc:creator>
            <pubDate>Wed, 14 Jul 2021 09:19:34 GMT</pubDate>
            <atom:updated>2021-07-18T14:44:17.887Z</atom:updated>
            <content:encoded><![CDATA[<p>The Apache Hadoop software library is a framework that allows for the distributed processing of large data sets across clusters of computers. Setting up a Hadoop cluster is not that hard but there is not a cohesive document, so I decided to write an article about it. Let’s BEGIN…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*McESTYuIIxkef2HhbyqQCg.png" /><figcaption>Apache Hadoop[<a href="https://hadoop.apache.org/">https://hadoop.apache.org/</a>]</figcaption></figure><p>Let’s assume that we have three Linux servers, one master and the others worker1 and worker2. I may use some CentOS (RHEL) commands. We will go through the following steps to run the cluster.</p><ol><li>Prepare Servers</li><li>Install JDK</li><li>Config HDFS and YARN</li><li>Start HDFS and YARN daemons</li></ol><h3>Prepare Servers</h3><p>As HDFS and YARN use hostname to communicate, we should set the hostname for all servers, and add them to /etc/hosts file. Let’s assume the IP addresses of servers are 192.168.0.1, 192.168.0.2, and 192.168.0.3. SSH each server and copy these lines to /etc/hosts file.</p><pre>[root@192.168.0.x ~]# vi /etc/hosts</pre><pre># Add these lines:<br>192.168.0.1   master<br>192.168.0.2   worker1<br>192.168.0.3   worker2</pre><p>I will use this notation [{user}@{server} {working directory}] to specify the <em>user</em>, <em>server</em>, and <em>working directory</em> on which the commands should be run, so check them before running scripts and commands. Now, it is time to set the hostname. SSH each server, and set the corresponding hostname using the following commands, and reboot them:</p><pre>[root@192.168.0.x ~]# hostnamectl set-hostname {name} # name: master, worker1, worker2<br>[root@192.168.0.x ~]# hostnamectl # Should print {name}<br>[root@192.168.0.x ~]# reboot</pre><p>Create a user named hduser in hadoop group, make it sudoer, and set a password for it, using these commands:</p><pre>[root@{hostname} ~]# groupadd hadoop<br>[root@{hostname} ~]# useradd -m hduser<br>[root@{hostname} ~]# usermod -aG hadoop hduser<br>[root@{hostname} ~]# usermod -aG wheel hduser<br>[root@{hostname} ~]# passwd hduser</pre><p>hduser on <strong>master</strong> should be able to connect to <strong>itself and the other servers</strong> with <strong>passwordless</strong> ssh. We should create an ssh key for hduser on the <strong>master</strong> server, and copy it to <strong>all servers including itself</strong>:</p><pre>[hduser@master ~]# ssh-keygen -t rsa -P &#39;&#39; -f ~/.ssh/id_rsa<br>[hduser@master ~]# ssh-copy-id -i .ssh/id_rsa.pub hduser@{master and workers}</pre><p>You can verify that after running the latest commands, we are allowed to ssh all servers with hduser without providing the password. After these simple steps, the servers are ready, and we can go to the next step.</p><h3>Install Java</h3><p>Before using Apache Hadoop, we should install Java, so download JDK or JRE (I prefer JDK), and copy it to workers.</p><pre>[hduser@master ~]# wget {JDK LINK (I downloaded jdk-8u291-linux-x64.tar.gz)}<br>[hduser@master ~]# scp jdk-8u291-linux-x64.tar.gz hduser@worker1:/home/hduser/jdk-8u291-linux-x64.tar.gz<br>[hduser@master ~]# scp jdk-8u291-linux-x64.tar.gz hduser@worker2:/home/hduser/jdk-8u291-linux-x64.tar.gz</pre><p><strong>On all servers</strong>, extract JDK, and create a soft link to be able to change its version simply in the future. I will extract all files in the /opt directory.</p><pre>[hduser@{server} ~]# cd /opt<br>[hduser@{server} /opt]# sudo tar xzf /home/hduser/jdk-8u291-linux-x64.tar.gz<br>[hduser@{server} /opt]# sudo ln -s jdk1.8.0_291/ jdk<br>[hduser@{server} /opt]# sudo chown -R hduser:hadoop jdk<br>[hduser@{server} /opt]# sudo chown -R hduser:hadoop jdk1.8.0_291<br><br>[hduser@{server} /opt]# sudo update-alternatives --install /usr/bin/java java /opt/jdk/bin/java 100<br>[hduser@{server} /opt]# sudo update-alternatives --install /usr/bin/javac javac /opt/jdk/bin/javac 100<br>[hduser@{server} /opt]# # Check java is installed successfully<br>[hduser@{server} /opt]# update-alternatives --display java<br>[hduser@{server} /opt]# update-alternatives --display javac<br>[hduser@{server} /opt]# java -version</pre><h3>Config HDFS and Yarn</h3><p>Currently, we have our servers set up and java! Let’s download Apache Hadoop, copy it to all servers, create links, and everything.</p><pre>[hduser@master ~]# wget {Apache Hadoop LINK (I downloaded hadoop-3.3.0-aarch64.tar.gz)}<br>[hduser@master ~]# scp hadoop-3.3.0-aarch64.tar.gz hduser@worker1:/home/hduser/hadoop-3.3.0-aarch64.tar.gz<br>[hduser@master ~]# scp hadoop-3.3.0-aarch64.tar.gz hduser@worker2:/home/hduser/hadoop-3.3.0-aarch64.tar.gz</pre><pre>[hduser@{server} ~]# cd /opt<br>[hduser@{server} /opt]# sudo tar xzf /home/hduser/hadoop-3.3.0-aarch64.tar.gz<br>[hduser@{server} /opt]# sudo ln -s hadoop-3.3.0/ hadoop<br>[hduser@{server} /opt]# sudo chown -R hduser:hadoop hadoop<br>[hduser@{server} /opt]# sudo chown -R hduser:hadoop hadoop-3.3.0</pre><p><strong>On all servers</strong>, create a directory, and store all data there. I will create this directory under /data directory, so if you’ve chosen another directory, don’t forget to change the scripts. We will create four directories:</p><ul><li>data: store data related to <em>DataNode</em></li><li>name: store data related to <em>NameNode</em></li><li>pid: create pid file</li><li>tmp: allow Apache Hadoop to create temp files</li></ul><pre>/opt/hadoop/etc/hadoop<br>[hduser@{server} /data]# sudo mkdir hadoop<br>[hduser@{server} /data]# sudo chown -R hduser:hadoop hadoop<br>[hduser@{server} /data/hadoop]# cd hadoop<br>[hduser@{server} /data/hadoop]# mkdir data<br>[hduser@{server} /data/hadoop]# mkdir name<br>[hduser@{server} /data/hadoop]# mkdir pid<br>[hduser@{server} /data/hadoop]# mkdir tmp</pre><p>All Hadoop config files are available under /opt/hadoop/etc/hadoop directory and most of the configs should be the same among all servers, so we will modify them one by one on the <strong>master</strong>, then copy them to the <strong>workers</strong>, and make some modifications on workers.</p><pre>[hduser@master ~]# cd /opt/hadoop/etc/hadoop<br>[hduser@master /opt/hadoop/etc/hadoop]# vi core-site.xml</pre><pre># Add these elements:<br>## Do not forget to change the tmp directory<br>  &lt;property&gt;<br>    &lt;name&gt;fs.defaultFS&lt;/name&gt;<br>    &lt;value&gt;hdfs://{master}:9000&lt;/value&gt;<br>  &lt;/property&gt;<br>  &lt;property&gt;<br>    &lt;name&gt;hadoop.tmp.dir&lt;/name&gt;<br>    &lt;value&gt;/data/hadoop/tmp&lt;/value&gt;<br>  &lt;/property&gt;</pre><pre>[hduser@master /opt/hadoop/etc/hadoop]# vi hadoop-env.sh</pre><pre># Add these commands:<br>## Do not forget to change the PID directory<br>export JAVA_HOME=/opt/jdk<br>export HADOOP_PID_DIR=/data/hadoop/pid</pre><pre>[hduser@master /opt/hadoop/etc/hadoop]# vi hdfs-site.xml</pre><pre># Add these elements:<br>## dfs.replication property specifies replication factor, you may want to change it. I prefer replication factor of two for small clusters.<br>## Do not forget to change the data directory<br>## Do not forget to change the name directory<br>  &lt;property&gt;<br>    &lt;name&gt;dfs.replication&lt;/name&gt;<br>    &lt;value&gt;2&lt;/value&gt;<br>  &lt;/property&gt;<br>  &lt;property&gt;<br>    &lt;name&gt;dfs.data.dir&lt;/name&gt;<br>    &lt;value&gt;/data/hadoop/data&lt;/value&gt;<br>  &lt;/property&gt;<br>  &lt;property&gt;<br>    &lt;name&gt;dfs.name.dir&lt;/name&gt;<br>    &lt;value&gt;/data/hadoop/name&lt;/value&gt;<br>  &lt;/property&gt;<br>  &lt;property&gt;<br>    &lt;name&gt;dfs.permission&lt;/name&gt;<br>    &lt;value&gt;false&lt;/value&gt;<br>  &lt;/property&gt;</pre><pre>[hduser@master /opt/hadoop/etc/hadoop]# vi mapred-site.xml</pre><pre># Add these elements:<br>  &lt;property&gt;<br>    &lt;name&gt;mapreduce.framework.name&lt;/name&gt;<br>    &lt;value&gt;yarn&lt;/value&gt;<br>  &lt;/property&gt;<br>  &lt;property&gt;<br>    &lt;name&gt;mapreduce.application.classpath&lt;/name&gt;<br>    &lt;value&gt;$HADOOP_MAPRED_HOME/share/hadoop/mapreduce/*:$HADOOP_MAPRED_HOME/share/hadoop/mapreduce/lib/*&lt;/value&gt;<br>  &lt;/property&gt;<br>  &lt;property&gt;<br>    &lt;name&gt;mapred.job.tracker&lt;/name&gt;<br>    &lt;value&gt;master:54311&lt;/value&gt;<br>    &lt;description&gt;The host and port that the MapReduce job tracker runs<br>    at.  If &quot;local&quot;, then jobs are run in-process as a single map<br>    and reduce task.<br>    &lt;/description&gt;<br>  &lt;/property&gt;</pre><pre>[hduser@master /opt/hadoop/etc/hadoop]# vi workers</pre><pre># Add the names of all workers<br>master<br>worker1<br>worker2</pre><pre>[hduser@master /opt/hadoop/etc/hadoop]# vi yarn-site.xml</pre><pre># Add these lines<br>## You may want to change the max-disk-utilization-per-disk-percentage.<br>## Notice that the value of &quot;yarn.nodemanager.hostname&quot; property is {worker}. We leave it for now, and will change it later<br>  &lt;property&gt;<br>    &lt;name&gt;yarn.nodemanager.aux-services&lt;/name&gt;<br>    &lt;value&gt;mapreduce_shuffle&lt;/value&gt;<br>  &lt;/property&gt;<br>  &lt;property&gt;<br>    &lt;name&gt;yarn.nodemanager.env-whitelist&lt;/name&gt;<br>    &lt;value&gt;JAVA_HOME,HADOOP_COMMON_HOME,HADOOP_HDFS_HOME,HADOOP_CONF_DIR,CLASSPATH_PREPEND_DISTCACHE,HADOOP_YARN_HOME,HADOOP_MAPRED_HOME&lt;/value&gt;<br>  &lt;/property&gt;<br>  &lt;property&gt;<br>    &lt;name&gt;yarn.nodemanager.disk-health-checker.max-disk-utilization-per-disk-percentage&lt;/name&gt;<br>    &lt;value&gt;95&lt;/value&gt;<br>  &lt;/property&gt;<br>  &lt;property&gt;<br>    &lt;name&gt;yarn.resourcemanager.hostname&lt;/name&gt;<br>    &lt;value&gt;master&lt;/value&gt;<br>  &lt;/property&gt;<br>  &lt;property&gt;<br>    &lt;name&gt;yarn.nodemanager.hostname&lt;/name&gt;<br>    &lt;value&gt;{worker}&lt;/value&gt;<br>  &lt;/property&gt;</pre><p>OK! Done! Now, we will copy all config files to all workers then change some of them.</p><pre>[hduser@master /opt/hadoop/etc/hadoop]# scp core-site.xml hadoop-env.sh hdfs-site.xml mapred-site.xml yarn-site.xml hduser@{workers}:/opt/hadoop/etc/hadoop/</pre><pre>[hduser@{server} /opt/hadoop/etc/hadoop]# vi yarn-site.xml</pre><pre># Change the value of &quot;yarn.nodemanager.hostname&quot; to the name of server.<br>## On master: &lt;value&gt;master&lt;/value&gt;<br>## On worker1: &lt;value&gt;worker1&lt;/value&gt;</pre><p>It is time to set environment variables on all servers. I recommend doing this on master and copy it to the others.</p><pre>[hduser@{server} ~]# vi .bashrc</pre><pre># Add these commands<br># Java<br>export JAVA_HOME=/opt/jdk</pre><pre># Haddop &amp; YARN<br>export PDSH_RCMD_TYPE=ssh<br>export HADOOP_HOME=/opt/hadoop<br>export HADOOP_CONF_DIR=/opt/hadoop/etc/hadoop<br>export HADOOP_MAPRED_HOME=/opt/hadoop<br>export HADOOP_COMMON_HOME=/opt/hadoop<br>export HADOOP_HDFS_HOME=/opt/hadoop<br>export HADOOP_YARN_HOME=/opt/hadoop<br>export PATH=$HADOOP_HOME/bin:$PATH<br>export HDFS_NAMENODE_USER=&quot;hduser&quot;<br>export HDFS_DATANODE_USER=&quot;hduser&quot;<br>export HDFS_SECONDARYNAMENODE_USER=&quot;hduser&quot;<br>export YARN_RESOURCEMANAGER_USER=&quot;hduser&quot;<br>export YARN_NODEMANAGER_USER=&quot;hduser&quot;<br>export LD_LIBRARY_PATH=$HADOOP_HOME/lib/native</pre><h3>Start HDFS and YARN daemons</h3><p>Downloaded, shared files, and configured all servers! Now, it is time to start the cluster. We should format the NameNode then start HDFS and YARN. If you want to start HDFS and YARN, start HDFS and then YARN, and when you want to stop them, stop YARN first and then HDFS.</p><pre>[hduser@master ~]# hadoop namenode -format<br>[hduser@master ~]# /opt/hadoop/sbin/start-dfs.sh<br>[hduser@master ~]# /opt/hadoop/sbin/start-yarn.sh<br>[hduser@master ~]# /opt/hadoop/sbin/stop-yarn.sh<br>[hduser@master ~]# /opt/hadoop/sbin/stop-dfs.sh</pre><p>You can also create services to control HDFS and YARN.</p><pre>[hduser@master ~]# cd /etc/systemd/system<br>[hduser@master /etc/systemd/system]# vi hdfs.service</pre><pre># Add these lines<br>## Do not forget to change the PID directory<br>[Unit]<br>Description=Hadoop DFS namenode and datanode<br>After=syslog.target network.target remote-fs.target nss-lookup.target network-online.target<br>Requires=network-online.target</pre><pre>[Service]<br>User=hduser<br>Group=hadoop<br>Type=simple<br>ExecStart=/opt/hadoop/sbin/start-dfs.sh<br>ExecStop=/opt/hadoop/sbin/stop-dfs.sh<br>WorkingDirectory=/home/hduser<br>TimeoutStartSec=2min<br>Restart=on-failure<br>PIDFile=/data/hadoop/pid/hadoop-hduser-namenode.pid</pre><pre>[Install]<br>WantedBy=multi-user.target</pre><pre>[hduser@master /etc/systemd/system]# vi yarn.service</pre><pre># Add these lines<br>## Do not forget to change the PID directory<br>[Unit]<br>Description=YARN resourcemanager and nodemanagers<br>After=syslog.target network.target remote-fs.target nss-lookup.target network-online.target<br>Requires=network-online.target</pre><pre>[Service]<br>User=hduser<br>Group=hadoop<br>Type=simple<br>ExecStart=/opt/hadoop/sbin/start-yarn.sh<br>ExecStop=/opt/hadoop/sbin/stop-yarn.sh<br>WorkingDirectory=/home/hduser<br>TimeoutStartSec=2min<br>Restart=on-failure<br>PIDFile=/data/hadoop/pid/hadoop-hduser-resourcemanager.pid</pre><pre>[Install]<br>WantedBy=multi-user.target</pre><p>I hope this tutorial was useful. Thank You.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=257068e5f276" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/running-a-multi-node-hadoop-cluster-257068e5f276">Running a Multi-Node Hadoop Cluster</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>