- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试在具有 8 个分区的 RDD 上使用 pyspark 来使用 forEachPartition() 方法。我的自定义函数尝试为给定的字符串输入生成字符串输出。这是代码
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
import pandas as pd
import datetime
def compute_sentiment_score(text):
client = language.LanguageServiceClient()
document = types.Document(content=text,type=enums.Document.Type.PLAIN_TEXT, language='en')
sentiment = client.analyze_sentiment(document=document).document_sentiment
return str(sentiment.score)
def compute_sentiment_magnitude(text):
client = language.LanguageServiceClient()
document = types.Document(content=text,type=enums.Document.Type.PLAIN_TEXT, language='en')
sentiment = client.analyze_sentiment(document=document).document_sentiment
return str(sentiment.magnitude)
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/path-to-file.json"
imdb_reviews = pd.read_csv('imdb_reviews.csv', header=None, names=['input1', 'input2'], encoding= "ISO-8859-1")
imdb_reviews.head()
input1 input2
0 first think another Disney movie, might good, ... 1
1 Put aside Dr. House repeat missed, Desperate H... 0
2 big fan Stephen King's work, film made even gr... 1
3 watched horrid thing TV. Needless say one movi... 0
4 truly enjoyed film. acting terrific plot. Jeff... 1
spark_imdb_reviews = spark.createDataFrame(imdb_reviews) # create spark dataframe
spark_imdb_reviews.printSchema()
root
|-- input1: string (nullable = true)
|-- input2: long (nullable = true)
这是我的自定义函数 -
def compute_sentiment_score(text):
client = language.LanguageServiceClient()
document = types.Document(content=text,type=enums.Document.Type.PLAIN_TEXT, language='en')
sentiment = client.analyze_sentiment(document=document).document_sentiment
return str(sentiment.score)
def compute_sentiment_magnitude(text):
client = language.LanguageServiceClient()
document = types.Document(content=text,type=enums.Document.Type.PLAIN_TEXT, language='en')
sentiment = client.analyze_sentiment(document=document).document_sentiment
return str(sentiment.magnitude)
这是我尝试使用 forEachPartition() 方法的方法 -
create_rdd = spark_imdb_reviews.select("input1").rdd # create RDD
print(create_rdd.getNumPartitions()) # print the partitions
print(create_rdd.take(1)) # display data
new_rdd = create_rdd.foreachPartition(compute_sentiment_score) # compute score
这给出了这个输出和一个错误 -
8
[Row(input1="first think another Disney movie, might good, it's kids movie. watch it, can't help enjoy it. ages love movie. first saw movie 10 8 years later still love it! Danny Glover superb could play part better. Christopher Lloyd hilarious perfect part. Tony Danza believable Mel Clark. can't help, enjoy movie! give 10/10!")]
File "<ipython-input-106-e3fd65ce75cc>", line 3, in compute_sentiment_score
TypeError: <itertools.chain object at 0x11ab7f198> has type itertools.chain, but expected one of: bytes, unicode
最佳答案
有两个类似的函数:
这两个函数都需要另一个函数作为参数(此处为compute_sentiment_score
)。该函数获取以迭代器形式传递的分区的内容。问题中的 text
参数实际上是一个迭代器,可以在 compute_sentiment_score
内部使用。
foreachPartition
和 mapPartition
之间的区别在于,foreachPartition
是 Spark 操作,而 mapPartition
是转换。这意味着 foreachPartition
调用的代码会立即执行,并且 RDD 保持不变,而 mapPartition
可用于创建新的 RDD。为了存储计算出的情绪分数,应使用 mapPartitions
。
def compute_sentiment_score(itr_text):
#setup the things that are expensive and should be prepared only once per partition
client = language.LanguageServiceClient()
#run the loop for each row of the partition
for text in itr_text:
document = types.Document(content=text.value,type=enums.Document.Type.PLAIN_TEXT, language='en')
sentiment = client.analyze_sentiment(document=document).document_sentiment
yield (text.value, sentiment.score)
df_with_score = df.rdd.mapPartitions(compute_sentiment_score)
df_with_score.foreach(print)
在此示例中,每个分区都会调用 client = language.LanguageServiceClient()
一次。可能必须减少分区数量,例如 coalesce .
关于pyspark - 如何在 pyspark 数据帧上使用 forEachPartition?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63806467/
spark foreachPartition,如何获取分区的索引(或者序列号,或者识别分区的东西)? val docs: RDD[String] = ... println("num partitio
我正在使用 2.3 版本的 pySpark(在我当前的开发系统中无法更新到 2.4),并且有以下关于 foreachPartition 的问题. 首先介绍一点背景信息:据我了解,pySpark-UDF
我的环境如下 Spark 1.6.1Hadoop 2.6.2 我们的需求如下(全部在Java Spark中) 1. 读取 CSV 文件并应用架构并将其转换为数据框 2.通过Spark分区SQL获取所有
我尝试在具有 8 个分区的 RDD 上使用 pyspark 来使用 forEachPartition() 方法。我的自定义函数尝试为给定的字符串输入生成字符串输出。这是代码 from google.c
我尝试在具有 8 个分区的 RDD 上使用 pyspark 来使用 forEachPartition() 方法。我的自定义函数尝试为给定的字符串输入生成字符串输出。这是代码 from google.c
我通过 spark-shell 评估了以下几行 scala 代码: val a = sc.parallelize(Array(1,2,3,4,5,6,7,8,9,10)) val b = a.coal
我们正在使用 spark 进行文件处理。我们正在处理相当大的文件,每个文件大约 30 GB,大约有 40-50 百万行。这些文件是格式化的。我们将它们加载到数据框中。最初的要求是识别符合条件的记录并将
我有一个 Spark Streaming 作业,它从 Kafka 读取数据,并在再次写入 Postrges 之前与 Postgres 中的现有表进行一些比较。这是它的样子: val message =
我有一个 DataProc 集群,其中有一个 master 和 4 个 worker。我有这个 Spark 工作: JavaRDD rdd_data = javaSparkContext.parall
我有一些这样的代码: println("\nBEGIN Last Revs Class: "+ distinctFileGidsRDD.getClass) val lastRe
我正在尝试找到一种方法来捕获 Spark 在其驱动程序的 foreachPartition() 方法内引发的异常。在数据集上使用 foreachPartition() 时,我传递了一个在各个工作线程之
我目前正在探索 Spark。我面临以下任务 - 获取一个 RDD,根据特定条件对其进行分区,然后将多个文件写入 S3 存储桶中的不同文件夹。 在我们来到上传到 S3 部分之前,一切都很好。我已经在 S
我想在 foreachPartition 中使用 SparkContext 和 SQLContext ,但由于序列化错误而无法执行。我知道这两个对象都不可序列化,但我认为 foreachPartiti
我有一个简单的 Spark 作业,用于将结果映射、计算并将结果写入 Oracle DB。我在将结果写入数据库时遇到问题。 按键减少结果后,我调用 foreachPartition 操作来建立连接并
我想知道 foreachPartition 与 foreach 方法相比是否会由于更高级别的并行性而产生更好的性能,考虑到我的情况'm 流经一个 RDD 以便对累加器变量执行一些求和。 最佳答案 fo
我正在尝试为我的数据集的每个分区拟合一个 ML 模型,但我不知道如何在 Spark 中执行此操作。 我的数据集基本上看起来像这样并且按公司划分: Company | Features | Target
我有以下使用结构化流 (Spark 2.2) 的工作代码,以便从 Kafka (0.10) 读取数据。我无法解决的唯一问题是在 ForeachWriter 中使用 kafkaProducer 时与 T
我想将每个分区的数据保存到MySQL数据库。为此,我创建了实现 VoidFunction<> 的类: public class DatabaseSaveFunction implements Void
我有一个 DataFrame,其中一列有逗号分隔的数据。 例如:数据如下所示:[{value:1}, {value:2, value:3}, {some value}, {somevalue, oth
我正在使用 Spark 读取一堆文件,对它们进行详细说明,然后将它们全部保存为序列文件。我想要的是每个分区有 1 个序列文件,所以我这样做了: SparkConf sparkConf = new Sp
我是一名优秀的程序员,十分优秀!