CSVFileFormat
似乎为字符串列读取和写入空值为null。我一直在寻找,但一直无法找到关于此的明确信息,所以我组织了一个简单的测试。
val df = session.createDataFrame(Seq(
(0, "a"),
(1, "b"),
(2, "c"),
(3, ""),
(4, null)
))
df.coalesce(1).write.mode("overwrite").format("csv")
.option("delimiter", ",")
.option("nullValue", "unknown")
.option("treatEmptyValuesAsNulls", "false")
.save(s"$path/test")
输出:
0,a
1,b
2,c
3,unknown
4,unknown
因此,似乎将空字符串和null
值都视为null
。读取带有空引号字符串和空值的CSV文件时会发生同样的情况。目前有什么方法可以区别对待这些吗?
答案 0 :(得分:0)
仅仅两年半之后,由于Spark 2.4.0,空字符串不再被视为等于
@component('mail::message', ['mailData' => $mailData])
# {{ $mailData['subject'] }}
@if(count($mailData['events']) > 0)
# No te pierdas nuestros próximos eventos
@component('mail::table')
| | |
| :------------- | :------------- |
@foreach($mailData['events'] as $event)
| <img src="{{ $event->image }}" style="min-width:120px; max-width:120px; min-height:100px; max-height:100px;"> | <strong><p>{{ $event->title }}</p></strong><p>{{!! $this->getExcerpt($event->body, 0, 100) !!}}</p> |
@endforeach
@endcomponent
@endif
@endcomponent
的值!有关功能,请参见this commit for a bit of detail。您的代码将在2.4.0+下正常运行:
<?php
namespace App\Traits;
use Str;
trait Strings
{
public function slugify($title){
$cleanString = $this->cleanString($title);
$slugified = Str::lower( Str::slug($title, '-') );
return $slugified;
}
public function getExcerpt($str, $startPos = 0, $maxLength = 30, $end = '[...]') {
if(strlen($str) > $maxLength) {
$excerpt = substr($str, $startPos, $maxLength - 6);
$lastSpace = strrpos($excerpt, ' ');
$excerpt = substr($excerpt, 0, $lastSpace);
$excerpt .= $end;
} else {
$excerpt = $str;
}
return $excerpt;
}
}
结果:
null
答案 1 :(得分:0)
我同意此行为是一个很大的改进,但是如果您使用Sqoop导出由Spark创建的生产环境中的CSV文件,并且在不更改的情况下进行更新,则请注意
.option("nullValue", "null")
然后,您的Sqoop导出将失败,并且可能会抛出一个错误,使您无法完全了解其根本原因。 100%是Sqoop问题,如果可能的话,我强烈敦促每个人都利用Spark JDBC对导出的支持,但要当心并注意这一点。许多企业仍然严重依赖Sqoop。