我尝试用下面的代码将一个TSV文件读入到一个DataFrame对象中:
SQLContext sqlContext = new SQLContext(javaSparkContext);
Map<String, String> sqlContextOptions = new HashMap<>();
sqlContextOptions.put("header", "true");
sqlContextOptions.put("delimiter", "\t");
DataFrame df = sqlContext.read()
.format("com.databricks.spark.csv")
.options(sqlContextOptions)
.load(path);现在,如果遇到空文件,代码就会抛出UnsupportedOperationException。我想处理空文件,但我不想假设这个异常总是意味着空文件。检查给定文件是否为空的最佳实践是什么?
发布于 2016-08-09 06:18:49
我没有看到显式定义的path,但我假设它是一个包含文件路径的字符串。如果是这种情况,您可以在BufferedReader对象中打开它,并检查是否可以读取它。
BufferedReader br = new BufferedReader(new FileReader(path));
if (br.readLine() == null) {
// handle empty file...
} else {
//do something...
}https://stackoverflow.com/questions/38839162
复制相似问题