<?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 William on Medium]]></title>
        <description><![CDATA[Stories by Samuel William on Medium]]></description>
        <link>https://medium.com/@samuelwilliam?source=rss-15cf3e8e5bbf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*6QYMBQPLc3lNjtyI.</url>
            <title>Stories by Samuel William on Medium</title>
            <link>https://medium.com/@samuelwilliam?source=rss-15cf3e8e5bbf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 15 Nov 2026 01:27:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@samuelwilliam/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[Spark-Multi-Dimensional Aggregation- Rollup, Cubes, Grouping Id]]></title>
            <link>https://medium.com/@samuelwilliam/spark-multi-dimensional-aggregation-rollup-cubes-grouping-id-23b8ca43b9a3?source=rss-15cf3e8e5bbf------2</link>
            <guid isPermaLink="false">https://medium.com/p/23b8ca43b9a3</guid>
            <category><![CDATA[rollup]]></category>
            <category><![CDATA[cube]]></category>
            <category><![CDATA[spark]]></category>
            <category><![CDATA[aggregate-function]]></category>
            <dc:creator><![CDATA[Samuel William]]></dc:creator>
            <pubDate>Sun, 19 Jul 2020 16:02:06 GMT</pubDate>
            <atom:updated>2020-07-19T18:03:22.784Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/318/1*Gmiya1yzeDC_krOgdS_ZSA.jpeg" /><figcaption>Rollup and cubes</figcaption></figure><p>While we discuss about aggregation we should be aware of Group By clause, so I am not going to go deep on that now. Here we will discuss about grouping sets, rollup and cubes in sparks which are very less explored.</p><p>Lets create a data frame that will help us to understand Multi dimensional Aggregation:</p><p>Let’s create a sample input set to perform spark aggregate functions.</p><pre><strong>import </strong>org.apache.spark.sql.SparkSession<br><strong>import </strong>org.apache.spark.sql.functions._</pre><pre><strong>object </strong>Multi_Dimensional_Aggregates {<br>  <strong>def </strong>main(args: Array[String]): Unit = {<br>    <strong>val </strong>spark: SparkSession = SparkSession.<em>builder</em>()<br>      .appName(<strong>&quot;Spark example&quot;</strong>)<br>      .master(<strong>&quot;local[*]&quot;</strong>)<br>      .config(<strong>&quot;option&quot;</strong>, <strong>&quot;some-value&quot;</strong>)<br>      .getOrCreate()<br>    <strong>import </strong>spark.implicits._<br>    <strong>val </strong>input = <em>Seq</em>((100, <strong>&quot;computer&quot;</strong>, 75), (100, <strong>&quot;maths&quot;</strong>, 90), (100, <strong>&quot;science&quot;</strong>, 70), (100, <strong>&quot;history&quot;</strong>, 50),<br>      (200, <strong>&quot;computer&quot;</strong>, 50), (200, <strong>&quot;maths&quot;</strong>, 35), (200, <strong>&quot;science&quot;</strong>, 90), (200, <strong>&quot;history&quot;</strong>, 78),<br>      (300, <strong>&quot;computer&quot;</strong>, 92), (300, <strong>&quot;maths&quot;</strong>, 67), (300, <strong>&quot;science&quot;</strong>, 71), (300, <strong>&quot;history&quot;</strong>, 65),<br>      (400, <strong>&quot;computer&quot;</strong>, 56), (400, <strong>&quot;maths&quot;</strong>, 77), (400, <strong>&quot;science&quot;</strong>, 42), (400, <strong>&quot;history&quot;</strong>, 37)<br>    )<br>    <strong>var </strong>inputDataframe = input.toDF(<strong>&quot;id&quot;</strong>, <strong>&quot;subject&quot;</strong>, <strong>&quot;marks&quot;</strong>)<br>    inputDataframe.show()<br>    <br>  }<br>}</pre><ul><li>— -+ — — — — + — — -+<br>| id| subject|marks|<br>+ — -+ — — — — + — — -+<br>|100|computer| 75|<br>|100| maths| 90|<br>|100| science| 70|<br>|100| history| 50|<br>|200|computer| 50|<br>|200| maths| 35|<br>|200| science| 90|<br>|200| history| 78|<br>|300|computer| 92|<br>|300| maths| 67|<br>|300| science| 71|<br>|300| history| 65|<br>|400|computer| 56|<br>|400| maths| 77|<br>|400| science| 42|<br>|400| history| 37|<br>+ — -+ — — — — + — — -+</li></ul><p><strong>Rollup:</strong></p><p>Lets first write the function, see the output and understand Rollup:</p><pre><strong>val </strong>rollupDataframe= inputDataframe.rollup(<strong>&quot;id&quot;</strong>,<strong>&quot;subject&quot;</strong>)<br>  .agg(<em>avg</em>(<strong>&quot;marks&quot;</strong>) as <strong>&quot;avg_marks&quot;</strong>)<br>    .orderBy(<strong>&quot;id&quot;</strong>)<br>rollupDataframe.show(30,<strong>false</strong>)</pre><ul><li>— — + — — — — + — — — — -+<br>|id |subject |avg_marks|<br>+ — — + — — — — + — — — — -+<br>|null|null |65.3125 | <br>|100 |null |71.25 |<br>|100 |computer|75.0 |<br>|100 |history |50.0 |<br>|100 |science |70.0 |<br>|100 |maths |90.0 |<br>|200 |history |78.0 |<br>|200 |science |90.0 |<br>|200 |computer|50.0 |<br>|200 |maths |35.0 |<br>|200 |null |63.25 |<br>|300 |null |73.75 |<br>|300 |science |71.0 |<br>|300 |maths |67.0 |<br>|300 |history |65.0 |<br>|300 |computer|92.0 |<br>|400 |maths |77.0 |<br>|400 |computer|56.0 |<br>|400 |null |53.0 |<br>|400 |history |37.0 |<br>|400 |science |42.0 |<br>+ — — + — — — — + — — — — -+</li><li>Rollup computes the aggregate at the hierarchy level of the columns. Here it assumes that hierarchy starts at id and drill downs to subject.</li><li>First row gives us the average(marks) considering all the input rows hence ID and Subject has NULL values. Average marks of all the students in the class across the subject is 65.3125</li><li>Row 2,11,12,19 gives the average marks of each students, so the ID will have the student Id and the subject column has NULL value since it gives the average of each student.</li><li>Rest all rows will just give the value for each ID and subject.</li><li>Rollup offers a shorthand of group by and just gives us the aggregation based on the columns defined in the query. We can also write the above query using GROUP BY but it will be clumsy. Example below</li></ul><pre><strong>val </strong>group1=inputDataframe.groupBy().agg(<em>avg</em>(<strong>&quot;marks&quot;</strong>) as <strong>&quot;avg_marks&quot;</strong>)<br>  .select(<em>lit</em>(<strong>null</strong>) as <strong>&quot;id&quot; </strong>,<em>lit</em>(<strong>null</strong>) as <strong>&quot;subject&quot; </strong>,<strong>$&quot;avg_marks&quot;</strong>)<br><br><strong>val </strong>group2=inputDataframe.groupBy(<strong>&quot;id&quot;</strong>,<strong>&quot;subject&quot;</strong>).agg(<em>avg</em>(<strong>&quot;marks&quot;</strong>) as <strong>&quot;avg_marks&quot;</strong>)<br><br><strong>val </strong>group3=inputDataframe.groupBy(<strong>&quot;id&quot;</strong>).agg(<em>avg</em>(<strong>&quot;marks&quot;</strong>) as <strong>&quot;avg_marks&quot;</strong>)<br>  .select(<strong>$&quot;id&quot;</strong>,<em>lit</em>(<strong>null</strong>) as <strong>&quot;subject&quot;</strong>, <strong>$&quot;avg_marks&quot;</strong>)<br><br><strong>val </strong>groupByRollupAlternateDf=group1.union(group2).union(group3).orderBy(<strong>&quot;id&quot;</strong>)</pre><p>This will also give the same output as Rollup. You can clearly see that Rollup query is very simple, concise and clear and does not need multiple statements.</p><p><strong>Rollup with Grouping_id:</strong></p><p>Since Rollup offers aggregation on each column that is specified in the query it may be difficult to understand based on which grouping columns the row is aggregated. So we can specify grouping_id as below.</p><pre><strong>val </strong>rollupWithGroupingDf= inputDataframe.rollup(<strong>&quot;id&quot;</strong>,<strong>&quot;subject&quot;</strong>)<br>    .agg(<em>avg</em>(<strong>&quot;marks&quot;</strong>) as <strong>&quot;avg_marks&quot; </strong>,<em>grouping_id</em>() as <strong>&quot;gid&quot;</strong>)<br>.select(<strong>&quot;id&quot;</strong>,<strong>&quot;subject&quot;</strong>,<strong>&quot;marks&quot;</strong>,<strong>&quot;gid&quot;</strong>).orderBy(<strong>&quot;gid&quot;</strong>)<br><br>  rollupWithGroupingDf.show(30, <strong>false</strong>)</pre><ul><li>— — + — — — — + — — — — -+ — -+<br>|id |subject |avg_marks|gid|<br>+ — — + — — — — + — — — — -+ — -+<br>|200 |history |78 |0 |<br>|300 |science |71 |0 |<br>|400 |maths |77 |0 |<br>|200 |maths |35 |0 |<br>|400 |science |42 |0 |<br>|400 |history |37 |0 |<br>|200 |computer|50 |0 |<br>|300 |history |65 |0 |<br>|300 |computer|92 |0 |<br>|200 |science |90 |0 |<br>|100 |history |50 |0 |<br>|100 |science |70 |0 |<br>|100 |maths |90 |0 |<br>|100 |computer|75 |0 |<br>|300 |maths |67 |0 |<br>|400 |computer|56 |0 |<br>|300 |null |295 |1 |<br>|400 |null |212 |1 |<br>|200 |null |253 |1 |<br>|100 |null |285 |1 |<br>|null|null |1045 |3 |<br>+ — — + — — — — + — — — — -+ — -+</li><li>Here in the above example grouping_id 0 says that the grouping columns are id and subject. grouping_id 1 says that grouping columns are based on the id and grouping id 3 says that grouping is done on all the columns.</li></ul><p><strong>Cube:</strong></p><p>Cube function helps us aggregates across all columns in the specified group. It helps us calculate the subtotal and grand total across all the columns specified within the cube clause. Cube does not assume any hierarchy and just produces result across all the possible columns defined.</p><pre><strong>val </strong>cubeDf=inputDataframe.cube(<strong>&quot;id&quot;</strong>, <strong>&quot;subject&quot;</strong>).agg(<em>avg</em>(<strong>&quot;marks&quot;</strong>) as <strong>&quot;avg_marks&quot;</strong>)<br>  .orderBy(<strong>&quot;id&quot;</strong>)<br><br>cubeDf.show(30, <strong>false</strong>)</pre><ul><li>— — + — — — — + — — — — -+<br>|id |subject |avg_marks|<br>+ — — + — — — — + — — — — -+<br>|null|history |57.5 |<br>|null|computer|68.25 |<br>|null|maths |67.25 |<br>|null|science |68.25 |<br>|null|null |65.3125 |<br>|100 |science |70.0 |<br>|100 |null |71.25 |<br>|100 |history |50.0 |<br>|100 |computer|75.0 |<br>|100 |maths |90.0 |<br>|200 |history |78.0 |<br>|200 |maths |35.0 |<br>|200 |science |90.0 |<br>|200 |null |63.25 |<br>|200 |computer|50.0 |<br>|300 |history |65.0 |<br>|300 |computer|92.0 |<br>|300 |science |71.0 |<br>|300 |null |73.75 |<br>|300 |maths |67.0 |<br>|400 |maths |77.0 |<br>|400 |computer|56.0 |<br>|400 |null |53.0 |<br>|400 |history |37.0 |<br>|400 |science |42.0 |<br>+ — — + — — — — + — — — — -+</li><li>Here in comparison with the rollup results, first 3 rows gives us the aggregate of the subject and hence id is NULL.</li></ul><p>Please refer to the github link for the code <a href="https://github.com/Samuel-William/ProgrammingExamples.git">https://github.com/Samuel-William/ProgrammingExamples.git</a></p><p>Please clap if you liked this article and please add any comments if you did not like something.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23b8ca43b9a3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Know Basics of GIT- Still in Draft]]></title>
            <link>https://medium.com/@samuelwilliam/know-basics-of-git-still-in-draft-d1344fa83d5f?source=rss-15cf3e8e5bbf------2</link>
            <guid isPermaLink="false">https://medium.com/p/d1344fa83d5f</guid>
            <dc:creator><![CDATA[Samuel William]]></dc:creator>
            <pubDate>Thu, 16 Jan 2020 14:18:50 GMT</pubDate>
            <atom:updated>2020-01-16T14:54:04.495Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>GIT-Version control Tool</strong></h3><p>GIT is a version control tool, GIT nowadays is used as version control tool in most of the companies. This article is written with the understanding that you have already installed git and you just want to know the basic GIT commands.</p><p>Open the GIT bash from the specific directory where you need to create, Here I have opened git bash from the BigData Repository.</p><p>If you have any questions or feedback please give your comments. It will be very much helpful to me.</p><p><strong>GIT Commands:-</strong></p><p>To Create a GIT Branch, - <em>git init &lt;branch_name&gt;</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*7sagRun_-8Lhc91c1GROpQ.png" /><figcaption>GIT command for creating a branch</figcaption></figure><p>To know whether you are in GIT Repo— <strong>git status</strong></p><p>If it is not a git repo, it will show as below</p><figure><img alt="An antique typewrite" src="https://cdn-images-1.medium.com/max/740/1*dpHqrUinFbjenff2hFMFTw.png" /><figcaption>git status command</figcaption></figure><p>If you are right on the git repo, it will show as below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*SW_UcHPERtIA_kP6SscQJQ.png" /><figcaption>git status command when you type this from the git repository</figcaption></figure><p>Create a file on git repo-<strong> touch &lt;name_of_file&gt; </strong>Example: <strong>touch HelloWorld</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*6bzeMo28EXXi5XAFPuRXQw.png" /></figure><p>Add a file in GIT- <strong>git add &lt;name_of_file&gt;</strong> Example: <strong>git add HelloWorld</strong></p><p><strong>git add</strong> is like a basket which you carry while you shopping in a grocery store, you can select the groceries and drop that in the basket and before you go to the counter you can filter out what you want and what you do not, so generally git add is like a staging area.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*1VjT17yA_lmhKpkgpSR9RA.png" /><figcaption>git add HelloWorld</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d1344fa83d5f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>