我在使用 Structured Streaming 的 ForeachWriter,写 HDFS 文件时,出现了这个异常

这个异常出现的原因是HDFS作为一个分布式文件系统,支持多线程读,但是不支持多线程写入。所以HDFS引入了一个时间类型的锁机制,也就是HDFS的租约机制(** lease holder**)。

这个知识点来源于这篇文章 http://blog.csdn.net/weixin_44252761/article/details/89517393

大数据计算时,多线程与分布式的并行可以很好的加速数据的处理速度。可在大数据存储时,分布式的文件存储系统对并发的写请求支持存在天然的缺陷。这是一对天然的矛盾,暂时无法解决,只能缓和。

怎么缓和呢?不得不崇拜Spark开发者的智商,非常的简单和实用。不能同时写一个文件,但是可以同时写多个文件啊,只要我(spark或者程序)认为这多个文件是一个文件,那写一个和多个就没有区别了。

按照这个想法,修改我的代码,真正代码篇幅太长,主要就是一个地方:

val hdfsWritePath = new Path(path) 改为 val hdfsWritePath = new Path(path + "/" + partitionId) 即可。

有兴趣的朋友可以看看更全面的代码,原来的源代码如下:

       inputStream match {
case Some(is) =>
is.writeStream
.foreach(new ForeachWriter[Row]() {
var successBufferedWriter: Option[BufferedWriter] = None def openHdfs(path: String, partitionId: Long, version: Long): Option[BufferedWriter] = {
val configuration: Configuration = new Configuration()
configuration.set("fs.defaultFS", hdfsAddr) val fileSystem: FileSystem = FileSystem.get(configuration)
val hdfsWritePath = new Path(path) val fsDataOutputStream: FSDataOutputStream =
if (fileSystem.exists(hdfsWritePath))
fileSystem.append(hdfsWritePath)
else
fileSystem.create(hdfsWritePath) Some(new BufferedWriter(new OutputStreamWriter(fsDataOutputStream, StandardCharsets.UTF_8)))
} override def open(partitionId: Long, version: Long): Boolean = {
successBufferedWriter =
if (successBufferedWriter.isEmpty) openHdfs(successPath, partitionId, version)
else successBufferedWriter
true
} override def process(value: Row): Unit = {
successBufferedWriter.get.write(value.mkString(","))
successBufferedWriter.get.newLine()
} override def close(errorOrNull: Throwable): Unit = {
successBufferedWriter.get.flush()
successBufferedWriter.get.close()
}
})
.start()
.awaitTermination()

上述代码初看没问题,却会导致标题错误,修改如下:

       inputStream match {
case Some(is) =>
is.writeStream
.foreach(new ForeachWriter[Row]() {
var successBufferedWriter: Option[BufferedWriter] = None def openHdfs(path: String, partitionId: Long, version: Long): Option[BufferedWriter] = {
val configuration: Configuration = new Configuration()
configuration.set("fs.defaultFS", hdfsAddr) val fileSystem: FileSystem = FileSystem.get(configuration)
val hdfsWritePath = new Path(path + "/" + partitionId) val fsDataOutputStream: FSDataOutputStream =
if (fileSystem.exists(hdfsWritePath))
fileSystem.append(hdfsWritePath)
else
fileSystem.create(hdfsWritePath) Some(new BufferedWriter(new OutputStreamWriter(fsDataOutputStream, StandardCharsets.UTF_8)))
} override def open(partitionId: Long, version: Long): Boolean = {
successBufferedWriter =
if (successBufferedWriter.isEmpty) openHdfs(successPath, partitionId, version)
else successBufferedWriter
true
} override def process(value: Row): Unit = {
successBufferedWriter.get.write(value.mkString(","))
successBufferedWriter.get.newLine()
} override def close(errorOrNull: Throwable): Unit = {
successBufferedWriter.get.flush()
successBufferedWriter.get.close()
}
})
.start()
.awaitTermination()

如此轻松(其实困扰了我一天)就解决了这个可能大家都会遇到的问题,读取时路径到 successPath 即可,分享出来。

如果有什么问题或不足,希望大家可以与我联系,共同进步。

完~~~~

你遇到了吗?Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.fs.FileAlreadyExistsException)的更多相关文章

  1. 异常-Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlException): Permission denied: user=hdfs, access=WRITE, inode="/hbase":root:supergroup:drwxr-xr-x

    1 详细异常 Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlExce ...

  2. Hive执行count函数失败,Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlException)

    Hive执行count函数失败 1.现象: 0: jdbc:hive2://192.168.137.12:10000> select count(*) from emp; INFO : Numb ...

  3. Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlException):

    用windows连接hadoop集群执行mapreduce任务的时候出现以下错误: org.apache.hadoop.security.AccessControlException:Permissi ...

  4. Hive JDBC:java.lang.RuntimeException: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.authorize.AuthorizationException): User: root is not allowed to impersonate anonymous

    今天使用JDBC来操作Hive时,首先启动了hive远程服务模式:hiveserver2 &(表示后台运行),然后到eclipse中运行程序时出现错误: java.sql.SQLExcepti ...

  5. 一脸懵逼加从入门到绝望学习hadoop之 org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlException): Permission denied: user=Administrator, access=WRITE, inode="/":root:supergroup:drwxr-xr报错

    1:初学hadoop遇到各种错误,这里贴一下,方便以后脑补吧,报错如下: 主要是在window环境下面搞hadoop,而hadoop部署在linux操作系统上面:出现这个错误是权限的问题,操作hado ...

  6. org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlException)

    在运行hadoop的程序时,向hdfs中写文件时候,抛出异常信息如下: Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hado ...

  7. hive运行query语句时提示错误:org.apache.hadoop.ipc.RemoteException: java.io.IOException: java.io.IOException:

    hive> select product_id, track_time from trackinfo limit 5; Total MapReduce jobs = 1 Launching Jo ...

  8. org.apache.hadoop.ipc.RemoteException(java.io.IOException)

    昨晚突然之间mr跑步起来了 jps查看 进程都在的,但是在reduce任务跑了85%的时候会抛异常 异常情况如下: 2016-09-21 21:32:28,538 INFO [org.apache.h ...

  9. 运行基准测试hadoop集群中的问题:org.apache.hadoop.ipc.RemoteException: java.io.IOException: File /benchmarks/TestDFSIO/io_data/test_

    在master(即:host2)中执行 hadoop jar hadoop-test-1.1.2.jar DFSCIOTest -write -nrFiles 12 -fileSize 10240 - ...

随机推荐

  1. bat脚本自动安装Jmeter&Jdk

    一句话能解决的事情,绝对不要写一篇文章:一篇文章能解决的事情,绝对不要使用各种工具:一个工具能解决的事情,绝对不要跑东跑西…… 文章主要介绍脚本如何下载.安装.配置Jmeter&Jdk. 不多 ...

  2. sql中的 where 、group by 和 having 用法解析

    --sql中的 where .group by 和 having 用法解析 --如果要用到group by 一般用到的就是“每这个字” 例如说明现在有一个这样的表:每个部门有多少人 就要用到分组的技术 ...

  3. GoLang 获取两个时间相差多少小时

    package main import ( "fmt" "time" ) func main() { fmt.Println(getHourDiffer(&qu ...

  4. ionic3 浏览器端返回

    首屏component.ts文件中使用setupBrowserBackButtonBehavior() { // Register browser back button action(s) wind ...

  5. 如何使用Externalizable接口自定义Java中的序列化

    Java序列化过程的缺点 我们都知道如何使用Serializable接口序列化/反序列化一个对象,并且如何使用writeObject 和readObject方法自定义序列化过程. 但是这些自定义还不够 ...

  6. 2019头条java面试总结 (包含面试题解析)

    2019滴滴java面试总结  (包含面试题) 本人8年开发经验.今年年初找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.滴滴等公司offer,岗位是Java后端开发. 面试了很多家公司,感觉大部分 ...

  7. Windows 服务程序(一)

    Windows 服务程序简介: Windows服务应用程序是一种需要长期运行的应用程序,它对于服务器环境特别适合. 它没有用户界面,并且也不会产生任何可视输出.任何用户消息都会被写进Windows事件 ...

  8. 零基础:如何快速学习JavaScript,html+css技术

    前端开发要学的知识内容涉及的会很宽泛,虽然说主要是HTML.CSS和JavaScript这些基础知识点,但达妹今天想强调一下,学前端开发除了要学这些基础知识外,学员还要在这之上进行延伸和深入的去学,而 ...

  9. 洛谷P2051 [AHOI2009] 中国象棋(状压dp)

    题目简介 n*m的棋盘,对每行放炮,要求每行每列炮数<=2,求方案数%9999973 N,M<=100 题目分析 算法考虑 考虑到N,M范围较小,每一行状态只与前面的行状态有关,考虑状压D ...

  10. php 加入 unless 语法

    1. php 的版本 :PHP 7.3.0-dev (cli) (built: Mar 18 2018 00:28:55) ( NTS ) 2. unless 语法结构: unless($cond){ ...