2016-11-06 20 views

cevap

6

Bunu yapmak için basit bir işlev oluşturabilirsiniz. İlk ithalatta bir çift:

import org.apache.spark.sql.functions.{trim, length, when} 
import org.apache.spark.sql.Column 

ve tanımı: Sonunda

def emptyToNull(c: Column) = when(length(trim(c)) > 0, c) 

hızlı bir testi:

val df = Seq(" ", "foo", "", "bar").toDF 
df.withColumn("value", emptyToNull($"value")) 

aşağıdaki sonucu verecektir:

+-----+ 
|value| 
+-----+ 
| null| 
| foo| 
| null| 
| bar| 
+-----+ 

ise boş s yerine geçmek istiyorsun

def emptyToNullString(c: Column) = when(length(trim(c)) > 0, c).otherwise("NULL") 
: dize ile tring "NULL Eğer otherwise maddeyi ekleyebilir
İlgili konular