Wednesday, 17 January 2018

Spark - II

We continue to explore Spark in the second part of the series. The first part of the Spark post is here in case you wish to look at it for the environment setup. We will use be using Hortonworks Sandbox HDP 2.6.3 for all the work in this post. Note that the Spark version is 2.2.x. This is different from the version in first post on Spark. We will begin by looking at the different types of transformations in Spark.

Running below code as in Spark I:

%spark2

sc
sc.appName
sc.applicationId
sc.defaultMinPartitions
sc.defaultParallelism
sc.deployMode
sc.isLocal
sc.master
sc.sparkUser
sc.startTime
sc.version


This returns below results:

res0: org.apache.spark.SparkContext = org.apache.spark.SparkContext@294437ac
res1: String = Zeppelin
res2: String = application_1516119957831_0001
res3: Int = 2
res4: Int = 2
res5: String = client
res6: Boolean = false
res7: String = yarn-client
res8: String = zeppelin
res9: Long = 1516120813279
res10: String = 2.2.0.2.6.3.0-235

Let us create a RDD first before we look at the transformations in Spark as shown below and print it out:

val rdd = sc.makeRDD(Array(1,2,3,4,5,6))
rdd.collect.foreach(print)

This returns below results:

rdd: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[20] at makeRDD at <console>:27

123456

Since the results are not clear, let us print as shown below:

print(rdd.collect.mkString(","))

This returns below results:

1,2,3,4,5,6

Now we have a RDD. let us start with the different transformations:

1) map(func): Returns a new RDD that is created by passing the function to each element of the source RDD


rdd.map(x => x*x).collect

This returns below results:

 res47: Array[Int] = Array(1, 4, 9, 16, 25, 36)

The square of each element in source RDD is returned

2) filter(func): Returns a new RDD with elements that satisfy the function

rdd.filter(x => x%3 == 0).collect
  
This returns below results:

res52: Array[Int] = Array(3, 6)

3) flatMap(func): Returns a new RDD that is created by passing the function to each element of the source RDD such that each input element can be mapped to zero or more output elements. The input elements are flattened to give the output

rdd.flatMap(x => Array(x,x*x)).collect

This returns below results:

res65: Array[Int] = Array(1, 1, 2, 4, 3, 9, 4, 16, 5, 25, 6, 36)

Note that the input values are flattened

4) mapPartitions(func): Similar to map. While the function in map is applied to each element, in mapPartition, func is applied to each partition. func must be of type Iterator<T> => Iterator<U> for an RDD of type T

Let us create a new RDD with three partitions:

val rdd2 = sc.parallelize(1 to 6,3)
rdd2.collect
rdd2.getNumPartitions


This returns below results:

rdd2: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[42] at parallelize at <console>:27
res83: Array[Int] = Array(1, 2, 3, 4, 5, 6)
res84: Int = 3

Applying mapPartition to collect the sum of each partition: 

rdd2.mapPartitions( (iterator: Iterator[Int]) => { var sum = 0
  while (iterator.hasNext) {
    sum += iterator.next
  }
  Iterator(sum)
}).collect

This returns below results:

res87: Array[Int] = Array(3, 7, 11)

5) mapPartitionsWithIndex(func): Similar to mapPartitions but with the addition of an index. func must be of type (Int, Iterator<T>) => Iterator<U>  for an RDD of type T

We will modify above code to add the index:

rdd2.mapPartitionsWithIndex( (index: Int,iterator: Iterator[Int]) => { var sum = 0
  while (iterator.hasNext) {
    sum += iterator.next
  }
  Iterator(index, sum)
}).collect

This returns below results:

res88: Array[Int] = Array(0, 3, 1, 7, 2, 11)

6)  sample(withReplacement, fraction, seed): Return a sample of elements from source RDD based on fraction and seed

rdd.sample(true,0.5).collect.foreach(println)

This returns below results:

2
4
4

Note that values repeat in above case

rdd.sample(false,0.5).collect.foreach(println) 

This returns below results: 

2
5
6

Note that values don't repeat 

7) union(other Dataset): Returns a dataset that is a union of the calling dataset and the called dataset

rdd.union(rdd2).collect 

This returns below results: 

res130: Array[Int] = Array(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6)

We continue in the next post ...

Friday, 12 January 2018

Splunk III

In the third segment of the series on Splunk, we will continue from where we left off in last post and explore some more features of Splunk as well as the creation of Reports and Dashboards in Splunk. We will use Splunk 7.0.1 that we installed in the earlier post for all our work.

We added the data in file employees.csv without treating HIRE_DATE as a date. The result of this is that when try to sort on date, we get incorrect result as shown below:

source="employees.csv" host="SPLUNK-SERVER" sourcetype="csv" FIRST_NAME = "S*" | sort hired_date desc

The results are shown below:


















Note that HIRE_DATE is not sorted correctly as this field is nit recognized as a date field. So, we can use the strptime function as shown below:

source="employees.csv" host="SPLUNK-SERVER" sourcetype="csv" FIRST_NAME = "N*" | eval hired_date = strptime(HIRE_DATE,"%d-%b-%y") | sort hired_date desc

The results are shown below:
















Let us further create a table containing fewer fields to make sense of the sort as shown below:

source="employees.csv" host="SPLUNK-SERVER" sourcetype="csv" FIRST_NAME = "S*" | eval hired_date = strptime(HIRE_DATE,"%d-%b-%y") | sort hired_date desc | table FIRST_NAME LAST_NAME SALARY HIRE_DATE

The results are shown below:










Let us save this as a report so that we can refer to it later:



















On the Save As Report window, enter Employee Subset by Salary for Title and select No for Time Range Picker and click on Save:


















On the next window, click on Permissions to grant others to see and run this report:
















On the next window, select Apps and also Read for Everyone and click on Save:




Then, this report can be seen after clicking Reports menu:

















You can see the report link highlighted below:











Click on Search above and then, a new search with below criteria:

source="employees.csv" host="SPLUNK-SERVER" sourcetype="csv" FIRST_NAME = "S*" | eval hired_date = strptime(HIRE_DATE,"%d-%b-%y") | sort hired_date desc | table LAST_NAME SALARY

The results are shown below:









Click on Visualization above to see column chart as shown below:








Save this report as Employee Salary Chart. Once this report is saved, click on Dashboard as shown below:













On the window, click on Skip:









Click on Create New Dashboard to create a new Dashboard:














On the window, enter Title as shown and click on Shared in App in Permissions and clik on Create Dashboard:




















On the created Dashboard, click on +Add Panel:



















On the Add Panel window, that pops in from the right, locate the reports we just created:
























Select the Employee Subset by Salary Report. This shows details about the report. Then, click on Add to Dashboard:






















This will add the report to the dashboard as shown below:














In the same way, add the Employee Salary Chart to the Dashboard:













Note that it appears below the previous report. Click on Save on right top:











This completes the creation of the Dashboard

Thursday, 11 January 2018

Splunk II

In the second part of the series on Splunk, we will analyze data and  calculate a few measures. We will use Splunk 7.0.1 that we installed in the earlier post for all our work.

Let us look at the services that got installed as part of the previous installation:




We can see that Splunkd Service and splunkweb (legacy purposes only) are installed. But, only Splunkd Service is of consequence. The description for this service is replicated below:

Splunkd is the indexing and searching engine for Splunk, a data platform for operational intelligence. It is required for Splunk instances acting as an indexer. If it is stopped, Splunk will not process data and will be unavailable for search. Splunkweb depends on Splunkd. Please see www.splunk.com for more information. Questions can be submitted to www.splunk.com/answers or for supported customers www.splunk.com/page/submit_issue

Without this service the application will not run. Let us shut down this service and start it on command line as shown below:















Once it is stopped, start the command line and navigate to the directory as shown below:

cd C:\Program Files\Splunk\bin

Then, run below command:

splunk start

Then, wait for the Splunk Service to start and then below command to check status:

splunk status

The results are shown below:
























To know more details about splunk, run below commands. If you are prompted for the username and password, enter credentials:

splunk show servername
splunk show splunkd-port
splunk show web-port 


The results are shown below:


Since the services are up and running, we can start Splunk Enterprise by clicking on the icon as shown below:

















Once logged in, click on Add Data:












Click on Skip below:











On the below screen, click on Upload:











Click on Select File to select employees.csv from our earlier posts. Read the FAQ below. Click on Next:

























Click on Next again:




Click on Review. Read FAQ before that:


















Click on Submit:













Click on Start Searching:












We can see the records classified as events:


Note the search criteria under New Search. We can add filters to get the information that we are seeking. In the below example, we look for employees whose first names start with Ke:

source="employees.csv" host="SPLUNK-SERVER" sourcetype="csv" FIRST_NAME = "Ke*"

The above search criteria are all applied at the same time meaning the searches criteria are all ANDed.

The results are shown below:













We can further narrow down the search by adding another condition as shown below:

source="employees.csv" host="SPLUNK-SERVER" sourcetype="csv" FIRST_NAME = "Ke*" (LAST_NAME = "C*" OR LAST_NAME = "F*")

The results are shown below:










One more criteria is added on a number field:

source="employees.csv" host="SPLUNK-SERVER" sourcetype="csv" FIRST_NAME = "Ke*" (LAST_NAME = "C*" OR LAST_NAME = "F*") SALARY > 3500

The results are shown below:










The next query shows the use of  sort of salary in ascending order:

source="employees.csv" host="SPLUNK-SERVER" sourcetype="csv" FIRST_NAME = "N*" | sort SALARY asc

The results are shown below:











We can select only a few columns passing the results of above query using | as shown below:

source="employees.csv" host="SPLUNK-SERVER" sourcetype="csv" FIRST_NAME = "N*" | sort SALARY asc | table FIRST_NAME LAST_NAME SALARY

The results are shown below:






Lastly, we calculate measures based on SALARY:

source="employees.csv" host="SPLUNK-SERVER" sourcetype="csv" FIRST_NAME = "N*" | sort SALARY asc | table FIRST_NAME LAST_NAME SALARY | stats count(SALARY) sum(SALARY) avg(SALARY) max(SALARY) min(SALARY)

The results are shown below: