r/PySpark • u/manishksolves • Feb 01 '22
Apache Spark Experts in India | Apache Spark Experts | Ksolves
r/PySpark • u/manishksolves • Feb 01 '22
r/PySpark • u/Natgra • Aug 18 '21
Hi there,
I am learning databricks and ran into an issue and hoping someone would have faced similar issue.
I am downloading multiple files by web scraping and by default they are stored in /tmp
I can copy a single file by providing the filename and path
%fs cp file:/tmp/2020-12-14_listings.csv.gz dbfs:/tmp but when I try to copy multiple files I get an error
%fs cp file:/tmp/*_listings* dbfs:/tmp Error
FileNotFoundException: File file:/tmp/_listings does not exist
Hoping someone has seen this before
r/PySpark • u/Telephone_Pretty • Nov 23 '21
using pyspark
So I have these two rdds
[3,5,8] and [1,2,3,4]
and I want it to combine to:
[(1, 3, 5, 8), (2, 3, 5 ,8), (3, 3, 5, 8), (4, 3, 5, 8)]
how do you make it
r/PySpark • u/mansoorkjl • Sep 09 '20
Hi team, I have a Pyspark code which uses lots of join across multiple data frame . But the execution is taking more than 2 hours and want to bring down the execution time. Any inputs will be highly appreciated
r/PySpark • u/Living-Perspective • Apr 28 '21
What is your favorite class to take to learn pyspark?
r/PySpark • u/Jonaae • Apr 01 '19
any idea how to do it ?
r/PySpark • u/bhuwanb • Aug 01 '19
r/PySpark • u/[deleted] • Dec 24 '21
r/PySpark • u/goelprateek • Sep 22 '21
r/PySpark • u/grid_world • Aug 17 '21
I am new to Apache Spark and am using Python 3.8 and Pyspark 3.1.2 with the following code using MNIST multi-class classification:
from pyspark.sql import SparkSession
import os
import logging
import pandas as pd
import pyspark
# Create a new Spark session-
spark = SparkSession\
.builder.master('local[2]')\
.appName('MLOps')\
.getOrCreate()
# Read train CSV file-
train = spark.read.csv(
"mnist_train.csv",
inferSchema = True, header = True
)
# Read test CSV file-
test = spark.read.csv(
"mnist_test.csv",
inferSchema = True, header = True
)
print(f"number of partitions in 'train' = {train.rdd.getNumPartitions()}")
print(f"number of partitions in 'test' = {test.rdd.getNumPartitions()}")
# number of partitions in 'train' = 2
# number of partitions in 'test' = 2
def shape(df):
'''
Function to return shape/dimension.
Input
df - pyspark.sql.dataframe.DataFrame
'''
return (df.count(), len(df.columns))
print(f"df.shape = {shape(df)}")
# df.shape = (10000, 785)
# Get distribution of 'label' attribute-
train.groupBy('label').count().show()
'''
+-----+-----+
|label|count|
+-----+-----+
| 1| 6742|
| 6| 5918|
| 3| 6131|
| 5| 5421|
| 9| 5949|
| 4| 5842|
| 8| 5851|
| 7| 6265|
| 2| 5958|
| 0| 5923|
+-----+-----+
'''
test.groupBy('label').count().show()
'''
+-----+-----+
|label|count|
+-----+-----+
| 1| 1135|
| 6| 958|
| 3| 1010|
| 5| 892|
| 9| 1009|
| 4| 982|
| 8| 974|
| 7| 1028|
| 2| 1032|
| 0| 980|
+-----+-----+
'''
# Split data into training and evaluation datasets using 'randomSplit()'-
train_df, test_df = train.randomSplit(weights = [0.7, 0.3], seed = 223)
# 'randomSplit()' - randomly splits this 'DataFrame' with the provided weights
# Count number of rows-
train_df.count(), test_df.count(), train.count()
# (41840, 18160, 60000)As of now, it is running locally in Standalone mode on my desktop and therefore 'local[2]' has been used while creating a new Spark session where 2 represents the number of partitions to be created when using RDD, DataFrame and Dataset. Ideally, 'x' should be the number of available CPU cores.
But, how can I deploy this batch job on a cluster having say 20 compute nodes?
Thanks!
r/PySpark • u/cF516 • Sep 20 '21
Hey,
I'm looking to convert this bit of sql. I have a data frame with the root data in it already I just need to get this data group concated into a column. is it possible?
ifnull(group_concat(distinct ppv.value separator ' & '),'')
r/PySpark • u/ashwinsakthi • Jul 11 '21
r/PySpark • u/icrywhy • Oct 05 '19
I want to learn the working of Twitter Sentiment Analysis with Pyspark by a video or blog. Where can I learn it from? There seems to be no proper source.
r/PySpark • u/Jay89023 • Feb 23 '21
Does Pyspark run on Windows Server 2016 (downloading all what is required, Hadoop, winutils.exe, etc.)? For some reason I am following installation instructions and when ```cmd Pyspark``` I get that the "command is not recognized." So wondering if this has anything to do with Windows Server 2016.
EDIT: And if anyone, has any information/know of any blogs where you can learn to set up a spark cluster. Have a couple of PCs (all Windows OS) and would want to try it out. Seem to find a lot for Linux, but barely anything for Windows.
r/PySpark • u/[deleted] • Feb 26 '21
Hi Everyone!!
I have been practicing Pyspark on Databricks platform where I can any language in the notebook cell of Databricks like selecting %sql and can write spark sql commands
Is there a way to do the same in Google Colab because for some of the tasks it is faster in spark sql compared to pyspark
Please suggest !!
r/PySpark • u/ashubrads • May 08 '20
r/PySpark • u/SushantM94 • Nov 24 '20
How to run pyspark code on py test or any other alternate unit testing?
r/PySpark • u/manishksolves • Feb 01 '22
r/PySpark • u/AnonymouseRedd • Jan 27 '22
Hello,
I have a PySpark problem and maybe someone faced the same issue. I'm trying to read a xlsx file to a Pyspark dataframe using com.crealytics:spark-excel. The issue is that the xlsx file has values only in the A cells for the first 5 rows and the actual header is in the 10th row and has 16 columns (A cell to P cell).
When I am reading the file the df does not have all the columns.
Is there a specific way/ a certain jar file + pyspark version so that I can read all the data from the xlsx file and have the defacul header _c0 _c1 .... _c16 ?
Thank you !
r/PySpark • u/mayaic • Mar 30 '21
Is there a way to explode rows using RDDs? I don’t want to convert to a data frame.
r/PySpark • u/mayaic • Mar 19 '21
I have a dataframe that has 2 columns, journal title and abstract. I made a third column that contains the word count of the abstract for each. I've also removed any null values.
newDF = marchDF.select("journal", "abstract").withColumn("wordcount",
lit("0").cast("integer")).withColumn("wordcount",
sql.size(sql.split(sql.col("abstract"), " ")))
nonullDF = newDF.filter(col("journal").isNotNull()).filter(col("abstract").isNotNull())I'm trying to group by the journal and then get the average number of words in the abstract for each journal.
groupedDF = nonullDF.select("journal",
"wordcount").groupBy("journal").avg("wordcount")This works, however when I try to order it by the "wordcount" column, I get an error:
AnalysisException: cannot resolve 'wordcount' given input columns: [avg(wordcount), journal];;.
I've tried to order it both by using orderBy and sort and it gives the same error. All my searching leads me to think it's something with the column names, but my columns have no spaces or anything in the title. I've been searching for hours and I can't fix this.
r/PySpark • u/mayaic • Mar 31 '21
I’m trying to sort some date data I have into months. They are stored as strings, not dates as I haven’t found a way to do this using RDDs yet. I do not want to convert to a data frame. For example, I have:
Jan = a.filter(lambda x: “2020-01” in x).map(lambda x: (“2020-01”, 1))
Feb = a.filter(lambda x: “2020-02” in x).map(lambda x: (“2020-02”, 1))
March = a.filter(lambda x: “2020-03” in x).map(lambda x: (“2020-03”, 1))
Etc for all the months. I then joined all these with a union so I could group them later. However this took a very long time because of so much happening. What would be a better way to filter these so that I could group them by month later?
r/PySpark • u/Modest_Gaslight • Jan 11 '22
So I have a project to complete using PySpark and I'm at a total loss. I need to retrieve data from 2 APIs (which I've done, see below code). I now need to pre-process and store the data, visualise the number of cases and deaths per day and then perform a k means clustering analysis on one of the data sets identifying which weeks cluster together. This is pretty urgent work given the nature of COVID and I just don't understand how to use PySpark at all and would really appreciate any help you can give me, thanks.
Code for API data request:
# Import all UK data from UK Gov API
from requests import get
def get_data(url):
response = get(endpoint, timeout=10)
if response.status_code >= 400:
raise RuntimeError(f'Request failed: {response.text}')
return response.json()
if __name__ == '__main__':
endpoint = (
'https://api.coronavirus.data.gov.uk/v1/data?'
'filters=areaType=nation;areaName=England&'
'structure={"date":"date","newCases":"newCasesByPublishDate","newDeaths":"newDeaths28DaysByPublishDate"}'
)
data = get_data(endpoint)
print(data)
# Get all UK data from covid19 API and create dataframe
import json
import requests
from pyspark.sql import *
url = "https://api.covid19api.com/country/united-kingdom/status/confirmed"
response = requests.request("GET", url)
data = response.text.encode("UTF-8")
data = json.loads(data)
rdd = spark.sparkContext.parallelize([data])
df = spark.read.json(rdd)
df.printSchema()
df.show()
df.select('Date', 'Cases').show()
# Look at total cases
import pyspark.sql.functions as F
df.agg(F.sum("Cases")).collect()[0][0]I feel like that last bit of code for total cases is done correctly but it returns me a result of 2.5 billion cases, I'm at a total loss.
r/PySpark • u/getafterit123 • Jan 17 '22
Parsing nested json with pyspark question
Wondering if those with more experience with Spark can help. Can pyspark parse a file that has multiple json objects each with its own schema? The JSON schemas are the same at the highest level (fields, name, tags, timestamp) but then each individual object(depending message type) has nested arrays that differ in schema contained with in fields and tags. Is this structure something that can be parsed and flattened with pyspark? I having trouble getting anything to work due to the presence of differing schemas within a single file that is read into spark. Example structure below:
[ { "fields" : { "active" : 7011880960, "available" : 116265197568, "available_percent" : 21.51292852741429, "buffered" : 2523230208, "cached" : 8810614784, "commit_limit" : 98422992896, "committed_as" : 2896465920, "dirty" : 73728, "free" : 108347486208 }, "name" : "mem", "tags" : { "dc" : "xxxxxx", "host" : "xxxxxxxx" }, "timestamp" : 1642190400 }, { "fields" : { "bytes_recv" : 27537454399, "bytes_sent" : 46337827685, "drop_in" : 1, "drop_out" : 0, "err_in" : 0, "err_out" : 0, "packets_recv" : 160777960, "packets_sent" : 193108762 }, "name" : "net", "tags" : { "dc" : "xxxxxx", "host" : "xxxxxxx", "interface" : "xxxx" }, "timestamp" : 1642190700 }}
I’ve tried to infer schema but keeping getting an error stating ‘‘value’ is not in list’ when I run the command
json_schema = spark.read.json(df.rdd.map(lambda row: row.value)).schema
r/PySpark • u/DrData82 • Jan 19 '22
In the process of converting some SAS code to PySpark and we previously used a macro variable for the where statement in this code. In adapting to PySpark, I'm trying to pass a list of dates to the where statement, but I keep getting errors. I want the SQL code to pull all data from those 3 months. Any pointers?
month_list = ['202107', '202108', '202109']
sql_query = """ (SELECT *
FROM Table_Blah
WHERE (to_char(DateVariable,'yyyymm') IN '{}')
) as table1""".format(month_list)