{"id":1032,"date":"2020-10-25T11:37:51","date_gmt":"2020-10-25T11:37:51","guid":{"rendered":"http:\/\/marcel-jan.eu\/datablog\/?p=1032"},"modified":"2021-11-09T22:14:03","modified_gmt":"2021-11-09T22:14:03","slug":"i-built-a-working-hadoop-spark-hive-cluster-on-docker-here-is-how","status":"publish","type":"post","link":"https:\/\/marcel-jan.eu\/datablog\/2020\/10\/25\/i-built-a-working-hadoop-spark-hive-cluster-on-docker-here-is-how\/","title":{"rendered":"I built a working Hadoop-Spark-Hive cluster on Docker. Here is how."},"content":{"rendered":"<p>TL;DR: I made a Docker compose that runs Hadoop, Spark and Hive in a multi-container environment. You can find the necessary files for it here:<\/p>\n<p><a href=\"https:\/\/github.com\/Marcel-Jan\/docker-hadoop-spark\">https:\/\/github.com\/Marcel-Jan\/docker-hadoop-spark<\/a><\/p>\n<p>[<strong>Update 2021-11-09<\/strong>: Since Docker Desktop turned &#8220;Expose daemon on tcp:\/\/localhost:2375 without TLS&#8221; off by default there have been all kinds of connection problems running the complete docker-compose. Turning this option on again (Settings &gt; General &gt; Expose daemon on tcp:\/\/localhost:2375 without TLS) makes it all work. I&#8217;m still looking for a more secure solution to this]<\/p>\n<p>&nbsp;<\/p>\n<h2>How it started<\/h2>\n<p>We at <a href=\"https:\/\/marcel-jan.eu\/datablog\/2019\/04\/25\/starting-at-dikw-may-1st-2019\/\">DIKW<\/a> are working on a <a href=\"https:\/\/academy.dikw.com\/nl\/opleidingen\/certified-data-engineering-professional\">Certified Data Engineering Professional course<\/a>. It is a course where you learn all aspects we could think of of being a data engineer: the cool big data stuff, but also how data warehousing works and how it all can work together.<\/p>\n<p>One of the topics is Hadoop. Now our course has an important practical aspect. We&#8217;re not just going to bombard you with theory. You have to try the products\/methods yourself. On your own laptop. So for the Hadoop module I suggested using the <a href=\"https:\/\/www.cloudera.com\/downloads\/hortonworks-sandbox\/hdp.html\">Cloudera sandbox<\/a> on Docker, because our practice environments work on Docker and the Cloudera sandbox has it all.<\/p>\n<p>And at one moment my colleague Hugo Koopmans told me we had a problem: building the Cloudera sandbox on his laptop took way too long and required way too much memory. Could we use an simpler (and much older) Hadoop implementation instead?<\/p>\n<p>My thoughts were: Simpler? Yes! Old version? No way! We&#8217;re not going to start a new course with a 5 year old Hadoop version. And off I went on a quest for lightweight Hadoop cluster on Docker. Ideally with Spark and maybe Hive. Because I like databases.<\/p>\n<p>&nbsp;<\/p>\n<h2>The quest for a lightweight and up to date Hadoop cluster<\/h2>\n<p>After searching and finding all kinds of Hadoop on Docker images, I found most of them where old. But it turned out that Big Data Europe has a <a href=\"https:\/\/github.com\/big-data-europe\/docker-hadoop\">Docker environment with Hadoop 3.2.1<\/a> and it&#8217;s only 9 months old. Much better. <a href=\"https:\/\/github.com\/big-data-europe\/docker-spark\">Their Spark version<\/a> is also pretty much up to date.<\/p>\n<p>But how to get the Spark nodes to connect to the Hadoop nodes? I could not get the docker-composed Hadoop nodes and the docker-composed Spark nodes to speak to each other.<\/p>\n<p>(There was a reason for that and I just found out why. I thought I used Big Data Europe&#8217;s Spark setup, but it looks like I got a different one. One that had a spark-net network defined. And I can&#8217;t remember where I got it from. It looks like <a href=\"https:\/\/github.com\/sdesilva26\/docker-spark\/blob\/master\/docker\/docker-compose.yml\">sdesliva26&#8217;s version<\/a> but it&#8217;s not that one either.)<\/p>\n<p>Anyhow, I learned gradually I needed to combine the docker-compose.yml files somehow.<\/p>\n<p>&nbsp;<\/p>\n<h2>Quick! Learn docker-compose<\/h2>\n<p>Now when you&#8217;ve worked with docker-compose for a while, you might think &#8220;how hard could it be?&#8221;. But I had no idea what the principles of this thing were.<\/p>\n<p>Docker-compose is a way to quickly create a multi-container environment. Perfect to create clusters, like a Hadoop cluster with a namenode and datanode. And it is defined in a docker-compose.yml file. But from the docker-compose.yml file there can be references to shell scripts to run or files with environmental settings. So you need these files too.<\/p>\n<p>I&#8217;ve spent countless hours combining docker-compose services and trying to get it to work and not understanding why it would not. But after removing these Spark networks it worked much better. Turns out that when you don&#8217;t define any network in docker-compose, the services are all part of the same network that Docker creates automatically.<\/p>\n<p>&nbsp;<\/p>\n<h2>Disecting a docker-compose.yml file.<\/h2>\n<p><em>You can skip this section if you just want to run the Docker Hadoop environment and don&#8217;t really care how. (Go ahead. I won&#8217;t be judgemental. That&#8217;s how I started myself.)<\/em><\/p>\n<p>So here is a simplified example of one service I took from the Hadoop docker-compose.yml:<\/p>\n<pre>version: \"3\"\r\n\r\nservices:\r\n  namenode:\r\n    image: bde2020\/hadoop-namenode:2.0.0-hadoop3.2.1-java8\r\n    container_name: namenode\r\n    restart: always\r\n    ports:\r\n      - 9870:9870\r\n      - 9000:9000\r\n    volumes:\r\n      - hadoop_namenode:\/hadoop\/dfs\/name\r\n    environment:\r\n      - CLUSTER_NAME=test\r\n      - CORE_CONF_fs_defaultFS=hdfs:\/\/namenode:8020\r\n    env_file:\r\n      - .\/hadoop.env\r\n\r\nvolumes:\r\n  hadoop_namenode:<\/pre>\n<p>You can see it starts with a version. That&#8217;s the version of the docker-compose file and 3 is the latest.<\/p>\n<p>The namenode service is based on an image prepared by Big Data Europe. Docker images are like blueprints for Docker containers. I sometimes think of the Docker image as an installation file and the container is the actual application running. I hope you get the idea. This service defintion refers to where the image can be found on Docker Hub. Docker Hub is like an app store for Docker images.<\/p>\n<p>We can also see ports defined. Hadoop namenodes has some stuff running on these ports and we want to address these from outside the container. For example, when you have started the containers, you find namenode information on http:\/\/localhost:9870. Should you want a different port on your laptop, because multiple containers run on port 80 or something, an important thing to remember is that the first port is the port on the outside, the second is the one in the container.<\/p>\n<p>The namenode also needs some permanent place to store data. For this there is the volume definition. But remember you have to add the list of volumes after defining the services also.<\/p>\n<p>And there is a .env file apparently and it contains all kinds of environmental variables necessary for running Hadoop.<\/p>\n<p>&nbsp;<\/p>\n<h2>Let&#8217;s get this thing started<\/h2>\n<p>Now you can download Big Data Europe&#8217;s docker-hadoop repository or from my docker-hadoop-spark repository and from the directory where you placed it all it takes is this command to get the multi-container environment running:<\/p>\n<pre>docker-compose up -d<\/pre>\n<p>The -d means it runs in de background.<\/p>\n<p>BTW the config file can have another name than docker-compose.yml. But then you need the -f option to point docker-compose to the correct file:<\/p>\n<pre>docker-compose up -f mymulticontainers.yml -d<\/pre>\n<p>And you can break it all down again by going to that same directory and running this:<\/p>\n<pre>docker-compose down<\/pre>\n<p>All the containers will then be stopped and removed. But: the images and volumes stay! So don&#8217;t be surprised that the csv file you uploaded to HDFS will still be there.<\/p>\n<p>&nbsp;<\/p>\n<h2>Combining docker-compose files<\/h2>\n<p>It turns out you can copy paste services from the Spark docker-compose.yml to the Hadoop docker-compose.yml, provided that I added the directories provided in the docker-spark Github repository. And I learned that I needed to remove the spark-network network (wherever it came from).<\/p>\n<p>And I thought I needed to break the environment down and build it up again every time I changed docker-compose.yml. Because that&#8217;s how stuff usually works. But not with docker-compose. You can edit the docker-compose.yml file and run &#8220;docker-compose up&#8221; again. I learned that after a whole lot of building up and breaking down docker environments BTW.<\/p>\n<p>&nbsp;<\/p>\n<h2>How the Hadoop-Spark-Hive docker-compose was built<\/h2>\n<p>So in the end it was a question of adding services from one docker-compose.yml to the other and all the necessary files. But it took me a while to understand how to use it and from where. But I got that all figured out now and I&#8217;ve written the quick starts for HDFS, Spark and Hive.<\/p>\n<p>&nbsp;<\/p>\n<h2>Quick starts<\/h2>\n<h3>Quick start HDFS<\/h3>\n<p>Find the Container ID of the namenode.<\/p>\n<pre><code>  docker ps |grep namenode\r\n\r\n1df7a57164de        bde2020\/hadoop-namenode:2.0.0-hadoop3.2.1-java8          \"\/entrypoint.sh \/run\u2026\"   27 hours ago        Up 12 hours (healthy)      0.0.0.0:9000-&gt;9000\/tcp, 0.0.0.0:9870-&gt;9870\/tcp             namenode\r\n\r\n<\/code><\/pre>\n<p>Copy breweries.csv to the namenode.<\/p>\n<pre><code>  docker cp breweries.csv 1df7a57164de:breweries.csv\r\n<\/code><\/pre>\n<p>Go to the bash shell on the namenode with that same Container ID of the namenode.<\/p>\n<pre><code>  docker exec -it 1df7a57164de bash\r\n<\/code><\/pre>\n<p>Create a HDFS directory \/data\/\/openbeer\/breweries.<\/p>\n<pre><code>hdfs dfs -mkdir \/data\r\nhdfs dfs -mkdir \/data\/openbeer\r\nhdfs dfs -mkdir \/data\/openbeer\/breweries\r\n<\/code><\/pre>\n<p>Copy breweries.csv to HDFS:<\/p>\n<pre><code>  hdfs dfs -put breweries.csv \/data\/openbeer\/breweries\/breweries.csv\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>Quick start Spark<\/h3>\n<p>Go to http:\/\/&lt;dockerhadoop_IP_address&gt;:8080 or <a href=\"http:\/\/localhost:8080\/\" rel=\"nofollow\">http:\/\/localhost:8080\/<\/a> on your Docker host (laptop). Here you find the spark:\/\/ master address:<\/p>\n<pre><code>  Spark Master at spark:\/\/452dd59615b0:7077\r\n<\/code><\/pre>\n<p>Go to the command line of the Spark master and start spark-shell.<\/p>\n<pre><code>  docker ps |grep spark\r\nefef70177b0b        bde2020\/spark-worker:3.0.0-hadoop3.2                     \"\/bin\/bash \/worker.sh\"   27 hours ago        Up 12 hours                0.0.0.0:8081-&gt;8081\/tcp                                     spark-worker-1\r\n453dd19695b0        bde2020\/spark-master:3.0.0-hadoop3.2                     \"\/bin\/bash \/master.sh\"   27 hours ago        Up 12 hours                0.0.0.0:7077-&gt;7077\/tcp, 6066\/tcp, 0.0.0.0:8080-&gt;8080\/tcp   spark-master\r\n\r\n  docker exec -it 453dd19695b0 bash\r\n  \r\n  spark\/bin\/spark-shell --master spark:\/\/452dd59615b0:7077\r\n<\/code><\/pre>\n<p>Load breweries.csv from HDFS.<\/p>\n<pre><code>  val df = spark.read.csv(\"hdfs:\/\/namenode:8020\/data\/openbeer\/breweries\/breweries.csv\")\r\n  \r\n  df.show()\r\n+----+--------------------+-------------+-----+---+\r\n| _c0|                 _c1|          _c2|  _c3|_c4|\r\n+----+--------------------+-------------+-----+---+\r\n|null|                name|         city|state| id|\r\n|   0|  NorthGate Brewing |  Minneapolis|   MN|  0|\r\n|   1|Against the Grain...|   Louisville|   KY|  1|\r\n|   2|Jack's Abby Craft...|   Framingham|   MA|  2|\r\n|   3|Mike Hess Brewing...|    San Diego|   CA|  3|\r\n|   4|Fort Point Beer C...|San Francisco|   CA|  4|\r\n|   5|COAST Brewing Com...|   Charleston|   SC|  5|\r\n|   6|Great Divide Brew...|       Denver|   CO|  6|\r\n|   7|    Tapistry Brewing|     Bridgman|   MI|  7|\r\n|   8|    Big Lake Brewing|      Holland|   MI|  8|\r\n|   9|The Mitten Brewin...| Grand Rapids|   MI|  9|\r\n|  10|      Brewery Vivant| Grand Rapids|   MI| 10|\r\n|  11|    Petoskey Brewing|     Petoskey|   MI| 11|\r\n|  12|  Blackrocks Brewery|    Marquette|   MI| 12|\r\n|  13|Perrin Brewing Co...|Comstock Park|   MI| 13|\r\n|  14|Witch's Hat Brewi...|   South Lyon|   MI| 14|\r\n|  15|Founders Brewing ...| Grand Rapids|   MI| 15|\r\n|  16|   Flat 12 Bierwerks| Indianapolis|   IN| 16|\r\n|  17|Tin Man Brewing C...|   Evansville|   IN| 17|\r\n|  18|Black Acre Brewin...| Indianapolis|   IN| 18|\r\n+----+--------------------+-------------+-----+---+\r\nonly showing top 20 rows\r\n\r\n<\/code><\/pre>\n<p>How cool is that? Your own Spark cluster to play with.<\/p>\n<p>&nbsp;<\/p>\n<h3>Quick start Hive<\/h3>\n<p>Find the Container ID of the Hive Server.<\/p>\n<pre><code>  docker ps |grep hive-server\r\n\r\n60f2c3b5eb32        bde2020\/hive:2.3.2-postgresql-metastore                  \"entrypoint.sh \/bin\/\u2026\"   27 hours ago        Up 12 hours                       0.0.0.0:10000-&gt;10000\/tcp, 10002\/tcp                        hive-server\r\n\r\n<\/code><\/pre>\n<p>Go to the command line of the Hive server and start hiveserver2<\/p>\n<pre><code>  docker exec -it 60f2c3b5eb32 bash\r\n\r\n  hiveserver2\r\n<\/code><\/pre>\n<p>Maybe a little check that something is listening on port 10000 now<\/p>\n<pre><code>  netstat -anp | grep 10000\r\ntcp        0      0 0.0.0.0:10000           0.0.0.0:*               LISTEN      446\/java\r\n\r\n<\/code><\/pre>\n<p>Okay. Beeline is the command line interface with Hive. Let&#8217;s connect to hiveserver2 now.<\/p>\n<pre><code>  beeline\r\n  \r\n  !connect jdbc:hive2:\/\/127.0.0.1:10000 scott tiger\r\n<\/code><\/pre>\n<p>Didn&#8217;t expect to encounter scott\/tiger again after my Oracle days. But there you have it. Definitely not a good idea to keep that user on production.<\/p>\n<p>Not a lot of databases here yet.<\/p>\n<pre><code>  show databases;\r\n  \r\n+----------------+\r\n| database_name  |\r\n+----------------+\r\n| default        |\r\n+----------------+\r\n1 row selected (0.335 seconds)\r\n<\/code><\/pre>\n<p>Let&#8217;s change that.<\/p>\n<pre><code>  create database openbeer;\r\n  use openbeer;\r\n<\/code><\/pre>\n<p>And let&#8217;s create a table.<\/p>\n<pre><code>CREATE EXTERNAL TABLE IF NOT EXISTS breweries(\r\n    NUM INT,\r\n    NAME CHAR(100),\r\n    CITY CHAR(100),\r\n    STATE CHAR(100),\r\n    ID INT )\r\nROW FORMAT DELIMITED\r\nFIELDS TERMINATED BY ','\r\nSTORED AS TEXTFILE\r\nlocation '\/data\/openbeer\/breweries';\r\n<\/code><\/pre>\n<p>And have a little select statement going.<\/p>\n<pre><code>  select name from breweries limit 10;\r\n+----------------------------------------------------+\r\n|                        name                        |\r\n+----------------------------------------------------+\r\n| name                                                                                                 |\r\n| NorthGate Brewing                                                                                    |\r\n| Against the Grain Brewery                                                                            |\r\n| Jack's Abby Craft Lagers                                                                             |\r\n| Mike Hess Brewing Company                                                                            |\r\n| Fort Point Beer Company                                                                              |\r\n| COAST Brewing Company                                                                                |\r\n| Great Divide Brewing Company                                                                         |\r\n| Tapistry Brewing                                                                                     |\r\n| Big Lake Brewing                                                                                     |\r\n+----------------------------------------------------+\r\n10 rows selected (0.113 seconds)\r\n<\/code><\/pre>\n<p>There you go: your private Hive server to play with.<\/p>\n<p>&nbsp;<\/p>\n<h2>Conclusion<\/h2>\n<p>I got the lightweight Hadoop environment that I wanted. On my Windows 10 laptop with WSL2 (Windows Subsystem for Linux 2) installed, it uses only 3 GB memory. That&#8217;s not half bad. It sometimes was <a href=\"https:\/\/twitter.com\/marceljankr\/status\/1309962152489385985?s=20\">a frustrating journey<\/a>, but I learned a lot about Docker and docker-compose and learned to love it.<\/p>\n<p>I hope you have fun with this Hadoop-Spark-Hive cluster too.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1038 size-large\" src=\"https:\/\/marcel-jan.eu\/datablog\/wp-content\/uploads\/2020\/10\/docker-hadoop-spark-hive-1024x650.png\" alt=\"Screenshot of Docker Desktop with the hadoop-spark-hive cluster\" width=\"640\" height=\"406\" \/><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TL;DR: I made a Docker compose that runs Hadoop, Spark and Hive in a multi-container environment. You can find the necessary files for it here: https:\/\/github.com\/Marcel-Jan\/docker-hadoop-spark [Update 2021-11-09: Since Docker Desktop turned &#8220;Expose daemon on tcp:\/\/localhost:2375 without TLS&#8221; off by default there have been all kinds of connection problems running [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55,192],"tags":[110,233,184,122,232,4,5],"class_list":["post-1032","post","type-post","status-publish","format-standard","hentry","category-howto","category-spark","tag-apache-spark","tag-big-data-europe","tag-dikw","tag-docker","tag-docker-compose","tag-hadoop","tag-hive"],"_links":{"self":[{"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/posts\/1032","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/comments?post=1032"}],"version-history":[{"count":12,"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/posts\/1032\/revisions"}],"predecessor-version":[{"id":1280,"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/posts\/1032\/revisions\/1280"}],"wp:attachment":[{"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/media?parent=1032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/categories?post=1032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/marcel-jan.eu\/datablog\/wp-json\/wp\/v2\/tags?post=1032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}