Group by with average function in scala - scala
Hi I am totally new to spark scala.I need an idea or any sample solution.I have a data like this
tagid,timestamp,listner,orgid,suborgid,rssi
[4,1496745915,718,4,3,0.30]
[2,1496745915,3878,4,3,0.20]
[4,1496745918,362,4,3,0.60]
[4,1496745913,362,4,3,0.60]
[2,1496745918,362,4,3,0.10]
[3,1496745912,718,4,3,0.05]
[2,1496745918,718,4,3,0.30]
[4,1496745911,1901,4,3,0.60]
[4,1496745912,718,4,3,0.60]
[2,1496745915,362,4,3,0.30]
[2,1496745912,3878,4,3,0.20]
[2,1496745915,1901,4,3,0.30]
[2,1496745910,1901,4,3,0.30]
I want to find for each tag and for each listner last 10 seconds timestamp data. Then For the 10 seconds data I need to find average for rssi values.Like this.
2,1496745918,718,4,3,0.60
2,1496745917,718,4,3,1.30
2,1496745916,718,4,1,2.20
2,1496745914,718,1,2,3.10
2,1496745911,718,1,2,6.10
4,1496745910,1901,1,2,0.30
4,1496745908,1901,1,2,1.30
..........................
..........................
Like this I need to find it. Any solution or suggestions is appreciated.
NOTE: I am doing with spark scala.
I tried through spark sql query .But not works properly.
val filteravg = avg.registerTempTable("avg")
val avgfinal = sqlContext.sql("SELECT tagid,timestamp,listner FROM (SELECT tagid,timestamp,listner,dense_rank() OVER (PARTITION BY _c6 ORDER BY _c5 ASC) as rank FROM avg) tmp WHERE rank <= 10")
avgfinal.collect.foreach(println)
I am trying through array also.Any help will be appreciated.
If you already have a dataframe as
+-----+----------+-------+-----+--------+----+
|tagid|timestamp |listner|orgid|suborgid|rssi|
+-----+----------+-------+-----+--------+----+
|4 |1496745915|718 |4 |3 |0.30|
|2 |1496745915|3878 |4 |3 |0.20|
|4 |1496745918|362 |4 |3 |0.60|
|4 |1496745913|362 |4 |3 |0.60|
|2 |1496745918|362 |4 |3 |0.10|
|3 |1496745912|718 |4 |3 |0.05|
|2 |1496745918|718 |4 |3 |0.30|
|4 |1496745911|1901 |4 |3 |0.60|
|4 |1496745912|718 |4 |3 |0.60|
|2 |1496745915|362 |4 |3 |0.30|
|2 |1496745912|3878 |4 |3 |0.20|
|2 |1496745915|1901 |4 |3 |0.30|
|2 |1496745910|1901 |4 |3 |0.30|
+-----+----------+-------+-----+--------+----+
Doing the following should work for you
df.withColumn("firstValue", first("timestamp") over Window.orderBy($"timestamp".desc).partitionBy("tagid"))
.filter($"firstValue".cast("long")-$"timestamp".cast("long") < 10)
.withColumn("average", avg("rssi") over Window.partitionBy("tagid"))
.drop("firstValue")
.show(false)
you should have output as
+-----+----------+-------+-----+--------+----+-------------------+
|tagid|timestamp |listner|orgid|suborgid|rssi|average |
+-----+----------+-------+-----+--------+----+-------------------+
|3 |1496745912|718 |4 |3 |0.05|0.05 |
|4 |1496745918|362 |4 |3 |0.60|0.54 |
|4 |1496745915|718 |4 |3 |0.30|0.54 |
|4 |1496745913|362 |4 |3 |0.60|0.54 |
|4 |1496745912|718 |4 |3 |0.60|0.54 |
|4 |1496745911|1901 |4 |3 |0.60|0.54 |
|2 |1496745918|362 |4 |3 |0.10|0.24285714285714288|
|2 |1496745918|718 |4 |3 |0.30|0.24285714285714288|
|2 |1496745915|3878 |4 |3 |0.20|0.24285714285714288|
|2 |1496745915|362 |4 |3 |0.30|0.24285714285714288|
|2 |1496745915|1901 |4 |3 |0.30|0.24285714285714288|
|2 |1496745912|3878 |4 |3 |0.20|0.24285714285714288|
|2 |1496745910|1901 |4 |3 |0.30|0.24285714285714288|
+-----+----------+-------+-----+--------+----+-------------------+
Related
How can I make a unique match with join with two spark dataframes and different columns?
I have two dataframes spark(scala): First: +-------------------+------------------+-----------------+----------+-----------------+ |id |zone |zone_father |father_id |country | +-------------------+------------------+-----------------+----------+-----------------+ |2 |1 |123 |1 |0 | |2 |2 |123 |1 |0 | |3 |3 |1 |2 |0 | |2 |4 |123 |1 |0 | |3 |5 |2 |2 |0 | |3 |6 |4 |2 |0 | |3 |7 |19 |2 |0 | +-------------------+------------------+-----------------+----------+-----------------+ Second: +-------------------+------------------+-----------------+-----------------+ |country |id |zone |zone_value | +-------------------+------------------+-----------------+-----------------+ |0 |2 |1 |7 | |0 |2 |2 |7 | |0 |2 |4 |8 | |0 |0 |0 |2 | +-------------------+------------------+-----------------+-----------------+ Then I need following logic: 1 -> If => first.id = second.id && first.zone = second.zone 2 -> Else if => first.father_id = second.id && first.zone_father = second.zone 3 -> If neither the first nor the second is true, follow the latter => first.country = second.zone And the expected result would be: +-------------------+------------------+-----------------+----------+-----------------+-----------------+ |id |zone |zone_father |father_id |country |zone_value | +-------------------+------------------+-----------------+----------+-----------------+-----------------+ |2 |1 |123 |1 |0 |7 | |2 |2 |123 |1 |0 |7 | |3 |3 |1 |2 |0 |7 | |2 |4 |123 |1 |0 |8 | |3 |5 |2 |2 |0 |7 | |3 |6 |4 |2 |0 |8 | |3 |7 |19 |2 |0 |2 | +-------------------+------------------+-----------------+----------+-----------------+-----------------+ I tried to join both dataframes, but due "or" operation, two results for each row is returned, because the last premise returns true regardless of the result of the other two.
How to Split the row by nth delimiter in Spark Scala
I have below data which is stored in a csv file 1|Roy|NA|2|Marry|4.6|3|Richard|NA|4|Joy|NA|5|Joe|NA|6|Jos|9| Now I want to read the file and store it in the spark dataframe, before storing it into dataframe I want to split at every 3rd | and store it as a row. Output Expected : 1|Roy|NA| 2|Marry|4.6| 3|Richard|NA| 4|Joy|NA| 5|Joe|NA| 6|Jos|9| Could you anyone help me out to get the output like above.
Start by reading your csv file val df = spark.read.option("delimiter", "|").csv(file) This will give you this dataframe +---+---+---+-----+---+---+-------+---+---+----+----+----+----+----+----+----+----+----+ |_c1|_c2|_c3|_c4 |_c5|_c6|_c7 |_c8|_c9|_c10|_c11|_c12|_c13|_c14|_c15|_c16|_c17|_c18| +---+---+---+-----+---+---+-------+---+---+----+----+----+----+----+----+----+----+----+ |Roy|NA |2 |Marry|4.6|3 |Richard|NA |4 |Joy |NA |5 |Joe |NA |6 |Jos |9 |null| |Roy|NA |2 |Marry|4.6|3 |Richard|NA |4 |Joy |NA |5 |Joe |NA |6 |Jos |9 |null| |Roy|NA |2 |Marry|4.6|3 |Richard|NA |4 |Joy |NA |5 |Joe |NA |6 |Jos |9 |null| +---+---+---+-----+---+---+-------+---+---+----+----+----+----+----+----+----+----+----+ Last column is created because of the last delimiter in your csv file so we get rid of it val dataframe = df.drop(df.schema.last.name) dataframe.show(false) +---+---+---+---+-----+---+---+-------+---+---+----+----+----+----+----+----+----+----+ |_c0|_c1|_c2|_c3|_c4 |_c5|_c6|_c7 |_c8|_c9|_c10|_c11|_c12|_c13|_c14|_c15|_c16|_c17| +---+---+---+---+-----+---+---+-------+---+---+----+----+----+----+----+----+----+----+ |1 |Roy|NA |2 |Marry|4.6|3 |Richard|NA |4 |Joy |NA |5 |Joe |NA |6 |Jos |9 | |1 |Roy|NA |2 |Marry|4.6|3 |Richard|NA |4 |Joy |NA |5 |Joe |NA |6 |Jos |9 | |1 |Roy|NA |2 |Marry|4.6|3 |Richard|NA |4 |Joy |NA |5 |Joe |NA |6 |Jos |9 | +---+---+---+---+-----+---+---+-------+---+---+----+----+----+----+----+----+----+----+ Then, you need to create an array that contains list of columns name you need to have in your final dataframe val names : Array[String] = Array("colOne", "colTwo", "colThree") Last, you need a function that reads by 3 def splitCSV(dataFrame: DataFrame, columnNames : Array[String], sparkSession: SparkSession) : DataFrame = { import sparkSession.implicits._ val columns = dataFrame.columns var finalDF : DataFrame = Seq.empty[(String,String,String)].toDF(columnNames:_*) for(order <- 0 until(columns.length) -3 by(3) ){ finalDF = finalDF.union(dataFrame.select(col(columns(order)).as(columnNames(0)), col(columns(order+1)).as(columnNames(1)), col(columns(order+2)).as(columnNames(2)))) } finalDF } After we apply this function on dataframe val finalDF = splitCSV(dataframe, names, sparkSession) finalDF.show(false) +------+-------+--------+ |colOne|colTwo |colThree| +------+-------+--------+ |1 |Roy |NA | |1 |Roy |NA | |1 |Roy |NA | |2 |Marry |4.6 | |2 |Marry |4.6 | |2 |Marry |4.6 | |3 |Richard|NA | |3 |Richard|NA | |3 |Richard|NA | |4 |Joy |NA | |4 |Joy |NA | |4 |Joy |NA | |5 |Joe |NA | |5 |Joe |NA | |5 |Joe |NA | +------+-------+--------+
You can use regex for most of it. There's no straightforward regex for "split at nth matching occurence", so we work around it by using a match to pick out the pattern, then insert a custom splitter that we can then use. ds .withColumn("value", regexp_replace('value, "([^\\|]*)\\|([^\\|]*)\\|([^\\|]*)\\|", "$1|$2|$3||")) // 1 .withColumn("value", explode(split('value, "\\|\\|"))) // 2 .where(length('value) > 0) // 3 Explanation Replace every group of 3 |'s with the components, then terminate with || Split on each || and use explode to move each to a separate row Unfortunately, the split picks up the empty group at the end, so we filter it out Output for your given input: +------------+ |value | +------------+ |1|Roy|NA | |2|Marry|4.6 | |3|Richard|NA| |4|Joy|NA | |5|Joe|NA | |6|Jos|9 | +------------+
getting duplicate count but retaining duplicate rows in pyspark
I am trying to find the duplicate count of rows in a pyspark dataframe. I found a similar answer here but it only outputs a binary flag. I would like to have the actual count for each row. To use the orignal post's example, if I have a dataframe like so: +--+--+--+--+ |a |b |c |d | +--+--+--+--+ |1 |0 |1 |2 | |0 |2 |0 |1 | |1 |0 |1 |2 | |0 |4 |3 |1 | |1 |0 |1 |2 | +--+--+--+--+ I would like to result in something like: +--+--+--+--+--+--+--+--+ |a |b |c |d |row_count | +--+--+--+--+--+--+--+--+ |1 |0 |1 |2 |3 | |0 |2 |0 |1 |0 | |1 |0 |1 |2 |3 | |0 |4 |3 |1 |0 | |1 |0 |1 |2 |3 | +--+--+--+--+--+--+--+--+ Is this possible? Thank You
Assuming df is your input dataframe: from pyspark.sql.window import Window from pyspark.sql import functions as F from pyspark.sql.functions import * w = (Window.partitionBy([F.col("a"), F.col("b"), F.col("c"), F.col("D")])) df=df.select(F.col("a"), F.col("b"), F.col("c"), F.col("D"), F.count(F.col("a")).over(w).alias("row_count")) If, as per your example, you want to replace every count 1 with 0 do: from pyspark.sql.window import Window from pyspark.sql import functions as F from pyspark.sql.functions import * w = (Window.partitionBy([F.col("a"), F.col("b"), F.col("c"), F.col("D")])) df=df.select(F.col("a"), F.col("b"), F.col("c"), F.col("D"), F.count(F.col("a")).over(w).alias("row_count")).select("a", "b", "c", "d", F.when(F.col("row_count")==F.lit(1), F.lit(0)). otherwise(F.col("row_count")).alias("row_count"))
PySpark: Creating a column with number of timesteps to an event
I have a dataframe that looks as follows: |id |val1|val2| +---+----+----+ |1 |1 |0 | |1 |2 |0 | |1 |3 |0 | |1 |4 |0 | |1 |5 |5 | |1 |6 |0 | |1 |7 |0 | |1 |8 |0 | |1 |9 |9 | |1 |10 |0 | |1 |11 |0 | |2 |1 |0 | |2 |2 |0 | |2 |3 |0 | |2 |4 |0 | |2 |5 |0 | |2 |6 |6 | |2 |7 |0 | |2 |8 |8 | |2 |9 |0 | +---+----+----+ only showing top 20 rows I want to create a new column with the number of rows until a non-zero value appears in val2, this should be done groupby/partitionby 'id'... if the event never happens, I need to put a -1 in the steps field. |id |val1|val2|steps| +---+----+----+----+ |1 |1 |0 |4 | |1 |2 |0 |3 | |1 |3 |0 |2 | |1 |4 |0 |1 | |1 |5 |5 |0 | event |1 |6 |0 |3 | |1 |7 |0 |2 | |1 |8 |0 |1 | |1 |9 |9 |0 | event |1 |10 |0 |-1 | no further events for this id |1 |11 |0 |-1 | no further events for this id |2 |1 |0 |5 | |2 |2 |0 |4 | |2 |3 |0 |3 | |2 |4 |0 |2 | |2 |5 |0 |1 | |2 |6 |6 |0 | event |2 |7 |0 |1 | |2 |8 |8 |0 | event |2 |9 |0 |-1 | no further events for this id +---+----+----+----+ only showing top 20 rows
Your requirement seems easy but implementing in spark and preserving immutability is a difficult task. I am suggesting you would need a recursive function to generate the steps column. Below I have tried to suggest you a recursive way using a udf function. import org.apache.spark.sql.functions._ //udf function to populate step column def stepsUdf = udf((values: Seq[Row]) => { //sorting the collected struct in reverse order according to val1 column in reverse order val val12 = values.sortWith(_.getAs[Int]("val1") > _.getAs[Int]("val1")) //selecting the first of sorted list val val12Head = val12.head //generating the first step column in the collected list val prevStep = if(val12Head.getAs("val2") != 0) 0 else -1 //generating the first output struct val listSteps = List(steps(val12Head.getAs("val1"), val12Head.getAs("val2"), prevStep)) //recursive function for generating the step column def recursiveSteps(vals : List[Row], previousStep: Int, listStep : List[steps]): List[steps] = vals match { case x :: y => //event changed so step column should be 0 if(x.getAs("val2") != 0) { recursiveSteps(y, 0, listStep :+ steps(x.getAs("val1"), x.getAs("val2"), 0)) } //event doesn't change after the last event change else if(x.getAs("val2") == 0 && previousStep == -1) { recursiveSteps(y, previousStep, listStep :+ steps(x.getAs("val1"), x.getAs("val2"), previousStep)) } //val2 is 0 after the event change so increment the step column else { recursiveSteps(y, previousStep+1, listStep :+ steps(x.getAs("val1"), x.getAs("val2"), previousStep+1)) } case Nil => listStep } //calling the recursive function recursiveSteps(val12.tail.toList, prevStep, listSteps) }) df .groupBy("id") // grouping by id column .agg(stepsUdf(collect_list(struct("val1", "val2"))).as("stepped")) //calling udf function after the collection of struct of val1 and val2 .withColumn("stepped", explode(col("stepped"))) // generating rows from the list returned from udf function .select(col("id"), col("stepped.*")) // final desired output .sort("id", "val1") //optional step just for viewing .show(false) where steps is a case class case class steps(val1: Int, val2: Int, steps: Int) which should give you +---+----+----+-----+ |id |val1|val2|steps| +---+----+----+-----+ |1 |1 |0 |4 | |1 |2 |0 |3 | |1 |3 |0 |2 | |1 |4 |0 |1 | |1 |5 |5 |0 | |1 |6 |0 |3 | |1 |7 |0 |2 | |1 |8 |0 |1 | |1 |9 |9 |0 | |1 |10 |0 |-1 | |1 |11 |0 |-1 | |2 |1 |0 |5 | |2 |2 |0 |4 | |2 |3 |0 |3 | |2 |4 |0 |2 | |2 |5 |0 |1 | |2 |6 |6 |0 | |2 |7 |0 |1 | |2 |8 |8 |0 | |2 |9 |0 |-1 | +---+----+----+-----+ I hope the answer is helpful
How to use collect_set and collect_list functions in windowed aggregation in Spark 1.6?
In Spark 1.6.0 / Scala, is there an opportunity to get collect_list("colC") or collect_set("colC").over(Window.partitionBy("colA").orderBy("colB")?
Given that you have dataframe as +----+----+----+ |colA|colB|colC| +----+----+----+ |1 |1 |23 | |1 |2 |63 | |1 |3 |31 | |2 |1 |32 | |2 |2 |56 | +----+----+----+ You can Window functions by doing the following import org.apache.spark.sql.functions._ import org.apache.spark.sql.expressions._ df.withColumn("colD", collect_list("colC").over(Window.partitionBy("colA").orderBy("colB"))).show(false) Result: +----+----+----+------------+ |colA|colB|colC|colD | +----+----+----+------------+ |1 |1 |23 |[23] | |1 |2 |63 |[23, 63] | |1 |3 |31 |[23, 63, 31]| |2 |1 |32 |[32] | |2 |2 |56 |[32, 56] | +----+----+----+------------+ Similar is the result for collect_set as well. But the order of elements in the final set will not be in order as with collect_list df.withColumn("colD", collect_set("colC").over(Window.partitionBy("colA").orderBy("colB"))).show(false) +----+----+----+------------+ |colA|colB|colC|colD | +----+----+----+------------+ |1 |1 |23 |[23] | |1 |2 |63 |[63, 23] | |1 |3 |31 |[63, 31, 23]| |2 |1 |32 |[32] | |2 |2 |56 |[56, 32] | +----+----+----+------------+ If you remove orderBy as below df.withColumn("colD", collect_list("colC").over(Window.partitionBy("colA"))).show(false) result would be +----+----+----+------------+ |colA|colB|colC|colD | +----+----+----+------------+ |1 |1 |23 |[23, 63, 31]| |1 |2 |63 |[23, 63, 31]| |1 |3 |31 |[23, 63, 31]| |2 |1 |32 |[32, 56] | |2 |2 |56 |[32, 56] | +----+----+----+------------+ I hope the answer is helpful
Existing answer is valid, just adding here a different style of writting window functions: import org.apache.spark.sql.expressions.Window val wind_user = Window.partitionBy("colA", "colA2").orderBy("colB", "colB2".desc) df.withColumn("colD_distinct", collect_set($"colC") over wind_user) .withColumn("colD_historical", collect_list($"colC") over wind_user).show(false)