How to loop through each row of dataFrame in pyspark

You simply cannot. DataFrames, same as other distributed data structures, are not iterable and can be accessed using only dedicated higher order function and / or SQL methods. You can of course collect for row in df.rdd.collect(): do_something(row) or convert toLocalIterator for row in df.rdd.toLocalIterator(): do_something(row) and iterate locally as shown above, but it beats … Read more

How to use JDBC source to write and read data in (Py)Spark?

Writing data Include applicable JDBC driver when you submit the application or start shell. You can use for example –packages: bin/pyspark –packages group:name:version or combining driver-class-path and jars bin/pyspark –driver-class-path $PATH_TO_DRIVER_JAR –jars $PATH_TO_DRIVER_JAR These properties can be also set using PYSPARK_SUBMIT_ARGS environment variable before JVM instance has been started or using conf/spark-defaults.conf to set spark.jars.packages … Read more

Provide schema while reading csv file as a dataframe in Scala Spark

Try the below code, you need not specify the schema. When you give inferSchema as true it should take it from your csv file. val pagecount = sqlContext.read.format(“csv”) .option(“delimiter”,” “).option(“quote”,””) .option(“header”, “true”) .option(“inferSchema”, “true”) .load(“dbfs:/databricks-datasets/wikipedia-datasets/data-001/pagecounts/sample/pagecounts-20151124-170000”) If you want to manually specify the schema, you can do it as below: import org.apache.spark.sql.types._ val customSchema = StructType(Array( … Read more

How to use Column.isin with list?

According to documentation, isin takes a vararg, not a list. List is actually a confusing name here. You can try converting your List to vararg like this: val items = List(“a”, “b”, “c”) sqlContext.sql(“select c1 from table”) .filter($”c1″.isin(items:_*)) .collect .foreach(println) Your variant with mkString compiles, because one single String is also a vararg (with number … Read more

Spark – SELECT WHERE or filtering?

According to spark documentation “where() is an alias for filter()“ filter(condition) Filters rows using the given condition. where() is an alias for filter(). Parameters: condition – a Column of types.BooleanType or a string of SQL expression. >>> df.filter(df.age > 3).collect() [Row(age=5, name=u’Bob’)] >>> df.where(df.age == 2).collect() [Row(age=2, name=u’Alice’)] >>> df.filter(“age > 3”).collect() [Row(age=5, name=u’Bob’)] >>> … Read more

How to join on multiple columns in Pyspark?

You should use & / | operators and be careful about operator precedence (== has lower precedence than bitwise AND and OR): df1 = sqlContext.createDataFrame( [(1, “a”, 2.0), (2, “b”, 3.0), (3, “c”, 3.0)], (“x1”, “x2”, “x3”)) df2 = sqlContext.createDataFrame( [(1, “f”, -1.0), (2, “b”, 0.0)], (“x1”, “x2”, “x3”)) df = df1.join(df2, (df1.x1 == df2.x1) … Read more

how to filter out a null value from spark dataframe

Let’s say you have this data setup (so that results are reproducible): // declaring data types case class Company(cName: String, cId: String, details: String) case class Employee(name: String, id: String, email: String, company: Company) // setting up example data val e1 = Employee(“n1”, null, “n1@c1.com”, Company(“c1”, “1”, “d1”)) val e2 = Employee(“n2”, “2”, “n2@c1.com”, Company(“c1”, … Read more

Pyspark: Split multiple array columns into rows

Spark >= 2.4 You can replace zip_ udf with arrays_zip function from pyspark.sql.functions import arrays_zip, col, explode (df .withColumn(“tmp”, arrays_zip(“b”, “c”)) .withColumn(“tmp”, explode(“tmp”)) .select(“a”, col(“tmp.b”), col(“tmp.c”), “d”)) Spark < 2.4 With DataFrames and UDF: from pyspark.sql.types import ArrayType, StructType, StructField, IntegerType from pyspark.sql.functions import col, udf, explode zip_ = udf( lambda x, y: list(zip(x, y)), … Read more

Create Spark DataFrame. Can not infer schema for type

SparkSession.createDataFrame, which is used under the hood, requires an RDD / list of Row/tuple/list/dict* or pandas.DataFrame, unless schema with DataType is provided. Try to convert float to tuple like this: myFloatRdd.map(lambda x: (x, )).toDF() or even better: from pyspark.sql import Row row = Row(“val”) # Or some other column name myFloatRdd.map(row).toDF() To create a DataFrame … Read more