擅长:python、mysql、java
<p>正如其他人提到的,您的主要问题是打印值而不是返回值。然而,即使在修复之后,在这里使用<code>udf</code>也是非常低效的。在</p>
<p>相反,可以使用<a href="http://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.functions.when" rel="nofollow noreferrer">^{<cd2>}</a>和<a href="http://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.Column.like" rel="nofollow noreferrer">^{<cd3>}</a>。在</p>
<p>尝试:</p>
<pre class="lang-python prettyprint-override"><code>import pyspark.sql.functions as f
new_df = new_df.withColumn(
"Path",
f.when(
f.lower(f.col("Path")).like(r"%opened%"),
f.lit(10)
).when(
f.lower(f.col("Path")).like(r"%clickedurl"),
f.lit(20)
)
)
</code></pre>
<p>默认情况下,<code>when</code>将返回<code>null</code>,如果没有任何条件匹配。在</p>