rdd

Which function in spark is used to combine two RDDs by keys

谁说我不能喝 提交于 2019-12-01 02:22:41
Let us say I have the following two RDDs, with the following key-pair values. rdd1 = [ (key1, [value1, value2]), (key2, [value3, value4]) ] and rdd2 = [ (key1, [value5, value6]), (key2, [value7]) ] Now, I want to join them by key values, so for example I want to return the following ret = [ (key1, [value1, value2, value5, value6]), (key2, [value3, value4, value7]) ] How I can I do this, in spark using Python or Scala? One way is to use join, but join would create a tuple inside the tuple. But I want to only have one tuple per key value pair. I would union the two RDDs and to a reduceByKey to

Use SparkContext hadoop configuration within RDD methods/closures, like foreachPartition

喜夏-厌秋 提交于 2019-12-01 02:20:32
问题 I am using Spark to read a bunch of files, elaborating on them and then saving all of them as a Sequence file. What I wanted, was to have 1 sequence file per partition, so I did this: SparkConf sparkConf = new SparkConf().setAppName("writingHDFS") .setMaster("local[2]") .set("spark.streaming.stopGracefullyOnShutdown", "true"); final JavaSparkContext jsc = new JavaSparkContext(sparkConf); jsc.hadoopConfiguration().addResource(hdfsConfPath + "hdfs-site.xml"); jsc.hadoopConfiguration()

How long does RDD remain in memory?

不羁岁月 提交于 2019-12-01 01:09:39
问题 Considering memory being limited, I had a feeling that spark automatically removes RDD from each node. I'd like to know is this time configurable? How does spark decide when to evict an RDD from memory Note: I'm not talking about rdd.cache() 回答1: I'd like to know is this time configurable? How does spark decide when to evict an RDD from memory An RDD is an object just like any other. If you don't persist/cache it, it will act as any other object under a managed language would and be collected

How to flatten nested lists in PySpark?

半腔热情 提交于 2019-12-01 00:25:33
问题 I have an RDD structure like: rdd = [[[1],[2],[3]], [[4],[5]], [[6]], [[7],[8],[9],[10]]] and I want it to become: rdd = [1,2,3,4,5,6,7,8,9,10] How do I write a map or reduce function to make it work? 回答1: You can for example flatMap and use list comprehensions: rdd.flatMap(lambda xs: [x[0] for x in xs]) or to make it a little bit more general: from itertools import chain rdd.flatMap(lambda xs: chain(*xs)).collect() 来源: https://stackoverflow.com/questions/34711149/how-to-flatten-nested-lists

Flattening JSON into Tabular Structure using Spark-Scala RDD only fucntion

北城以北 提交于 2019-12-01 00:25:31
I have nested JSON and like to have output in tabular structure. I am able to parse the JSON values individually , but having some problems in tabularizing it. I am able to do it via dataframe easily. But I want do it using "RDD ONLY " functions. Any help much appreciated. Input JSON: { "level":{"productReference":{ "prodID":"1234", "unitOfMeasure":"EA" }, "states":[ { "state":"SELL", "effectiveDateTime":"2015-10-09T00:55:23.6345Z", "stockQuantity":{ "quantity":1400.0, "stockKeepingLevel":"A" } }, { "state":"HELD", "effectiveDateTime":"2015-10-09T00:55:23.6345Z", "stockQuantity":{ "quantity"

Spark filtering with regex

做~自己de王妃 提交于 2019-11-30 23:31:36
I am trying to filter file data into good and bad data per the date, hence will get 2 result files. From test file, first 4 lines need to go in good data and last 2 lines in bad data. I am having 2 issues I am not getting any good data, result file is empty and bad data result looks like following - picking up the name characters only (,C,h) (,J,u) (,T,h) (,J,o) (,N,e) (,B,i) Test file Christopher|Jan 11, 2017|5 Justin|11 Jan, 2017|5 Thomas|6/17/2017|5 John|11-08-2017|5 Neli|2016|5 Bilu||5 Load and RDD scala> val file = sc.textFile("test/data.txt") scala> val fileRDD = file.map(x => x.split("|

Spark migrate sql window function to RDD for better performance

限于喜欢 提交于 2019-11-30 22:12:32
A function should be executed for multiple columns in a data frame def handleBias(df: DataFrame, colName: String, target: String = target) = { val w1 = Window.partitionBy(colName) val w2 = Window.partitionBy(colName, target) df.withColumn("cnt_group", count("*").over(w2)) .withColumn("pre2_" + colName, mean(target).over(w1)) .withColumn("pre_" + colName, coalesce(min(col("cnt_group") / col("cnt_foo_eq_1")).over(w1), lit(0D))) .drop("cnt_group") } This can be written nicely as shown above in spark-SQL and a for loop. However this is causing a lot of shuffles ( spark apply function to columns in

Which function in spark is used to combine two RDDs by keys

大兔子大兔子 提交于 2019-11-30 21:52:09
问题 Let us say I have the following two RDDs, with the following key-pair values. rdd1 = [ (key1, [value1, value2]), (key2, [value3, value4]) ] and rdd2 = [ (key1, [value5, value6]), (key2, [value7]) ] Now, I want to join them by key values, so for example I want to return the following ret = [ (key1, [value1, value2, value5, value6]), (key2, [value3, value4, value7]) ] How I can I do this, in spark using Python or Scala? One way is to use join, but join would create a tuple inside the tuple. But

Flattening JSON into Tabular Structure using Spark-Scala RDD only fucntion

青春壹個敷衍的年華 提交于 2019-11-30 20:05:03
问题 I have nested JSON and like to have output in tabular structure. I am able to parse the JSON values individually , but having some problems in tabularizing it. I am able to do it via dataframe easily. But I want do it using "RDD ONLY " functions. Any help much appreciated. Input JSON: { "level":{"productReference":{ "prodID":"1234", "unitOfMeasure":"EA" }, "states":[ { "state":"SELL", "effectiveDateTime":"2015-10-09T00:55:23.6345Z", "stockQuantity":{ "quantity":1400.0, "stockKeepingLevel":"A"

RDD转化成类型的方式进行访问

主宰稳场 提交于 2019-11-30 19:54:46
1)创建一个样例类 scala> case class People(name:String,age:Long) defined class People 2)创建DataSet scala> val caseClassDS = Seq(People("Andy",32)).toDS() caseClassDS: org.apache.spark.sql.Dataset[People] = [name: string, age: bigint] 这样people不仅仅有类型,而且还有了结构,这样用起来会更加方便一些。 3)caseClassDS.你会发现这里有很多种方法,也可以show,也可以limit等等。 scala> caseClassDS. agg describe intersect reduce toDF alias distinct isLocal registerTempTable toJSON apply drop isStreaming repartition toJavaRDD as dropDuplicates javaRDD rollup toLocalIterator cache dtypes join sample toString checkpoint except joinWith schema transform coalesce explain