我编写了本地运行的Spark代码。我创建了一个用户定义的函数,它需要应用于通过交叉连接从本地文件读取的两个表创建的数据帧。
我正在申请的用户定义的函数不会被分发。我在所有节点上安装了所需的python包。使用spark-submit
我已经指定了核心和内存的数量。这是我的代码:
spark = SparkSession.builder.appName("WordSimilarities").enableHiveSupport().getOrCreate()
spark.sparkContext.parallelize(range(1,1000)).map(imprts)
df = spark.read.csv('./FlightGlobal_distinct_operators.csv', header=False).withColumnRenamed('_c0', 'first').repartition(10)
df.cache()
df2 = spark.read.csv('./TRAC_distinct_operators.csv',header=False).withColumnRenamed('_c0', 'second').repartition(10)
df2.cache()
df3 = df.crossJoin(df2)
df3.write.saveAsTable("hello", format="parquet",mode="overwrite",location="/user/hive/hello/")
df3 = spark.sql("select * from hello").repartition(500)
print(df3.count())
df3.cache()
我正在申请的功能
schema = StructType([StructField("col1", FloatType()), StructField("col2", FloatType()), StructField("col3", FloatType()), StructField(
"col4", FloatType()), StructField("col5", FloatType()), StructField("col6", FloatType()), StructField("col7", FloatType())])
allmethodUDF = udf(all_methods_scores, schema)
finalDF = df3.withColumn("complex", allmethodUDF('first', 'second')).select(
'first', 'second', 'complex.col1', 'complex.col2', 'complex.col3', 'complex.col4', 'complex.col6', 'complex.col7')
finalDF.show()
以下是我在每个节点上应用的软件包:
from pyspark.sql import SparkSession
from pyspark.sql.functions import broadcast
import sys
from pyspark.sql import HiveContext
from pyspark.sql.functions import udf
from pyspark import SparkContext
from pyspark.sql.functions import udf
from pyspark.sql.types import *
import numpy as np
import editdistance
import jellyfish
import fuzzy
import re
from fuzzywuzzy import fuzz
import itertools
def all_methods_scores(original1, original2):
在所有包中的函数内部我正在进行大量的迭代
该函数的代码仅应用于以多列作为输入的每一行。所有RDD块只在一个节点上。
答案 0 :(得分:0)
答案可能过于简单,但你的数据重新分配了吗?如果您正在从单个.csv文件中读取(如您所看到的那样),您将最终得到一个分区。
尝试对您的数据运行repartition():
df.repartition(100)
df2.repartition(100)
阅读CSV文件后,看看这是否解决了您的问题。
你的代码顶部的parallelize()是做什么的?
spark.sparkContext.parallelize(range(1,1000)).map(imprts)
答案 1 :(得分:0)
要使用所有可用的执行程序,您需要在spark-submit命令中指定 - num-executors< value > 。