pyspark implementation of difflib.get_close_matches - pyspark

Is there any pyspark equivalent function for difflib.get_close_matches. My Dataset is huge and wanted to compare and get the close match. I am not able to broadcast the compared dataset as it is not iterable.

Related

Spark grouped map UDF in Scala

I am trying to write some code that would allow me to compute some action on a group of rows of a dataframe. In PySpark, this is possible by defining a Pandas UDF of type GROUPED_MAP. However, in Scala, I only found a way to create custom aggregators (UDAFs) or classic UDFs.
My temporary solution is to generate a list of keys that would encode my groups which would allow me to filter the dataframe and perform my action for each subset of dataframe. However, this approach is not optimal and very slow.
The performed actions are made sequentially, thus taking a lot of time. I could parallelize the loop but I'm sure this would show any improvement since Spark is already distributed.
Is there any better way to do what I want ?
Edit: Tried parallelizing using Futures but there was no speed improvement, as expected
To the best of my knowledge, this is something that's not possible in Scala. Depending on what you want, I think there could be other ways of applying a transformation to a group of rows in Spark / Scala:
Do a groupBy(...).agg(collect_list(<column_names>)), and use a UDF that operates on the array of values. If desired, you can use a select statement with explode(<array_column>) to revert to the original format
Try rewriting what you want to achieve using window functions. You can add a new column with an aggregate expression like so:
import org.apache.spark.sql.expressions.Window
val w = Window.partitionBy('group)
val result = spark.range(100)
.withColumn("group", pmod('id, lit(3)))
.withColumn("group_sum", sum('id).over(w))

XGBClassifier fit with pyspark dataframe?

Is it possible to pass a pyspark dataframe to a XGBClassifer as:
from xgboost import XGBClassifer
model1 = XGBClassifier()
model1.fit (df.select(features), df.select('label'))
If not, what is the best way to fit a pyspark dataframe to xgboost?
Many thanks
I believe there are two ways to skin this particular cat.
You can either:
Move your pyspark dataframe to pandas using the toPandas() method (or even better, using pyarrow). pandas dataframes will work just fine withxgboost. However, your data needs to fit in the memory, so you might need to subsample if you're working with TB or even GB of data.
Have a look at the xgboost4j and xgboost4j-spark packages. In the same way as pyspark is a wrapper using py4j, these guys can leverage SparkML built-ins, albeit typically for Scala-Spark. For example, the XGBoostEstimator from these packages can be used as a stage in SparkML Pipeline() object.
Hope this helps.

What does rdd mean in pyspark dataframe

I am new to to pyspark. I am wondering what does rdd mean in pyspark dataframe.
weatherData = spark.read.csv('weather.csv', header=True, inferSchema=True)
These two line of the code has the same output. I am wondering what the effect of having rdd
weatherData.collect()
weatherData.rdd.collect()
A data frame is a table, or two-dimensional array-like structure, in which each column contains measurements on one variable, and each row contains one case.
So, a DataFrame has additional metadata due to its tabular format, which allows Spark to run certain optimizations on the finalized query.
An RDD, on the other hand, is merely a Resilient Distributed Dataset that is more of a blackbox of data that cannot be optimized as the operations that can be performed against it, are not as constrained.
However, you can go from a DataFrame to an RDD via its .rdd method, and you can go from an RDD to a DataFrame (if the RDD is in a tabular format) via the .toDF() method
In general, it is recommended to use a DataFrame where possible due to the built in query optimization.

PySpark how is pickle used in SparkSql and Dataframes

I am trying to understand how PySpark uses pickle for RDDs and avoids it for SparkSql and Dataframes. The basis of the question is from slide#30 in this link.I am quoting it below for reference:
"[PySpark] RDDs are generally RDDs of pickled objects. Spark SQL (and DataFrames) avoid some of this".
How is pickle used in Spark Sql?
In the original Spark RDD model, RDDs described distributed collections of Java objects or pickled Python objects. However, SparkSQL "dataframes" (including Dataset) represent queries against one or more sources/parents.
To evaluate a query and produce some result, Spark does need to process records and fields, but these are represented internally in a binary, language-neutral format (called "encoded"). Spark can decode these formats to any supported language (e.g., Python, Scala, R) when needed, but will avoid doing so if it's not explicitly required.
For example: if I have a text file on disk, and I want to count the rows, and I use a call like:
spark.read.text("/path/to/file.txt").count()
there is no need for Spark to ever convert the bytes in the text to Python strings -- Spark just needs to count them.
Or, if we did a spark.read.text("...").show() from PySpark, then Spark would need to convert a few records to Python strings -- but only the ones required to satisfy the query, and show() implies a LIMIT so only a few records are evaluated and "decoded."
In summary, with the SQL/DataFrame/DataSet APIs, the language you use to manipulate the query (Python/R/SQL/...) is just a "front-end" control language, it's not the language in which the actual computation is performed nor does it require converting original data sources to the language you are using. This approach allows higher performance across all language front ends.

Group data based on multiple column in spark using scala's API

I have an RDD, want to group data based on multiple column. for large dataset spark cannot work using combineByKey, groupByKey, reduceByKey and aggregateByKey, these gives heap space error. Can you give another method for resolving it using Scala's API?
You may want to use treeReduce() for doing incremental reduce in Spark. However, you hypothesis that spark can not work on large dataset is not true, and I suspect you just don't have enough partitions in your data, so maybe a repartition() is what you need.