pyspark

Pyspark drop_duplicates(keep=False)

给你一囗甜甜゛ 提交于 2019-12-20 04:25:17
问题 i need a Pyspark solution for Pandas drop_duplicates(keep=False) . Unfortunately, the keep=False option is not available in pyspark... Pandas Example: import pandas as pd df_data = {'A': ['foo', 'foo', 'bar'], 'B': [3, 3, 5], 'C': ['one', 'two', 'three']} df = pd.DataFrame(data=df_data) df = df.drop_duplicates(subset=['A', 'B'], keep=False) print(df) Expected output: A B C 2 bar 5 three A conversion .to_pandas() and back to pyspark is not an option. Thanks! 回答1: Use window function to count

Converting complex RDD to a flatten RDD with PySpark

好久不见. 提交于 2019-12-20 04:21:33
问题 I have the following CSV (sample) id timestamp routeid creationdate parameters 1000 21-11-2016 22:55 14 21-11-2016 22:55 RSRP=-102, 1002 21-11-2016 22:55 14 21-11-2016 22:55 RA Req. SN=-146,TPC=4,RX Antennas=-8, 1003 21-11-2016 22:55 14 21-11-2016 22:55 RA Req. SN=134,RX Antennas=-91,MCS=-83,TPC=-191, Basically I want to separate parameters from one column into multiple columns as followed : id , timestamp, routeid, creationdate, RSRP ,RA REQ. SN, TPC,RX Antennas,MCS So if there is no value

Connection pooling in a streaming pyspark application

烂漫一生 提交于 2019-12-20 04:15:48
问题 ​What is the proper way of using connection pools in a streaming pyspark application ? I read through https://forums.databricks.com/questions/3057/how-to-reuse-database-session-object-created-in-fo.html and understand the proper way is to use a singleton for scala/java. Is this possible in python ? A small code example would be greatly appreciated. I believe creating a connection perPartition will be very inefficient for a streaming application. 回答1: Long story short connection pools will be

Understand closure in spark

心已入冬 提交于 2019-12-20 04:15:34
问题 In cluster modes, how to write a closure function f to let every worker access the copy of variable N . N=5 lines=sc.parallelize(['early radical', 'french revolution','pejorative way', 'violent means']) def f1(line): return line[:N] l=lines.map(f1) l.collect() I am trying to experiment to find out whether my understanding is right. In my example, f1 works in local mode. I don't have any cluster and I really want to know if it will work in cluster modes? In other word, can worker access

pyspark.sql DataFrame创建及常用操作

▼魔方 西西 提交于 2019-12-20 03:59:48
Spark SQL 简介及参考链接 Spark 是一个基于内存的用于处理大数据的集群计算框架。它提供了一套简单的编程接口,从而使得应用程序开发者方便使用集群节点的CPU,内存,存储资源来处理大数据。 Spark API提供了Scala, Java, Python和R的编程接口,可以使用这些语言来开发Spark应用。为了用Spark支持Python,Apache Spark社区发布了一个工具PySpark。使用PySpark,您也可以使用Python编程语言处理RDD。 Spark SQL将 SQL和HiveSQL的简单与强大融合到一起。 Spark SQL是一个运行在Spark上的Spark库。它提供了比Spark Core更为高层的用于处理结构化数据的抽象. Spark DataFrame 派生于RDD类,分布式但是提供了非常强大的数据操作功能。 本文主要梳理Spark DataFrame的常用方法,之后写一下与DataFrame操作密切配合的Spark SQL内置函数 和 用户UDF (用户定义函数) 和 UDAF (用户定义聚合函数) pyspark.sql 核心类 pyspark.SparkContext: Spark 库的主要入口点,它表示与Spark集群的一个连接,其他重要的对象都要依赖它.SparkContext存在于Driver中,是Spark功能的主要入口

StackOverflow-error when applying pyspark ALS's “recommendProductsForUsers” (although cluster of >300GB Ram available)

ぐ巨炮叔叔 提交于 2019-12-20 03:43:24
问题 Looking for expertise to guide me on issue below. Background: I'm trying to get going with a basic PySpark script inspired on this example As deploy infrastructure I use a Google Cloud Dataproc Cluster. Cornerstone in my code is the function "recommendProductsForUsers" documented here which gives me back the top X products for all users in the model Issue I incur The ALS.Train script runs smoothly and scales well on GCP (Easily >1mn customers). However, applying the predictions: i.e. using

Handle string to array conversion in pyspark dataframe

心不动则不痛 提交于 2019-12-20 03:36:04
问题 I have a file(csv) which when read in spark dataframe has the below values for print schema -- list_values: string (nullable = true) the values in the column list_values are something like: [[[167, 109, 80, ...]]] Is it possible to convert this to array type instead of string? I tried splitting it and using code available online for similar problems: df_1 = df.select('list_values', split(col("list_values"), ",\s*").alias("list_values")) but if I run the above code the array which I get skips

Read range of files in pySpark

主宰稳场 提交于 2019-12-20 03:32:24
问题 I need to read contiguous files in pySpark. The following works for me. from pyspark.sql import SQLContext file = "events.parquet/exportDay=2015090[1-7]" df = sqlContext.read.load(file) How do I read files 8-14? 回答1: Use curly braces. file = "events.parquet/exportDay=201509{08,09,10,11,12,13,14}" Here's a similar question on stack overflow: Pyspark select subset of files using regex glob. They suggest either using curly braces, OR performing multiple reads and then unioning the objects

How to use string variables in VectorAssembler in Pyspark

大兔子大兔子 提交于 2019-12-20 03:19:43
问题 I want to run Random Forests algorithm on Pyspark. It is mentioned in the Pyspark documentation that VectorAssembler accepts only numerical or boolean datatypes. So, if my data contains Stringtype variables, say names of cities, should I be one-hot encoding them in order to proceed further with Random Forests classification/regression? Here is the code I have been trying, input file is here: train=sqlContext.read.format('com.databricks.spark.csv').options(header='true').load('filename') drop

substring multiple characters from the last index of a pyspark string column using negative indexing

ぐ巨炮叔叔 提交于 2019-12-20 03:14:36
问题 Closely related to: Spark Dataframe column with last character of other column but I want to extract multiple characters from the -1 index. I have the following pyspark dataframe df +----------+----------+ | number|event_type| +----------+----------+ |0342224022| 11| |0112964715| 11| +----------+----------+ I want to extract 3 characters from the last index of the number column. I tried the following: from pyspark.sql.functions import substring df.select(substring(df['number'], -1, 3), 'event