我在使用 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. 如何让谷歌浏览器支持小于12px的字体

    CSS3有个新的属性transform,而我们用到的就是transform:scale() 书写一段代码 <body> <p>我是一个小于12PX的字体</p> & ...

  2. (八十三)c#Winform自定义控件-导航菜单(扩展)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  3. java并发之内存模型

    java内存模型知识导图 一 并发问题及含义   并发编程存在原子性.可见性.有序性问题. 原子性即一系列操作要么都执行,要么都不执行. 可见性,一个线程对共享变量的修改,另一个线程可能不会马上看到. ...

  4. 从0开始学FreeRTOS-(消息队列)-5

    ## 问题解答 曾经有人问我,FreeRTOS那么多API,到底怎么记住呢? 我想说,其实API不难记,就是有点难找,因为FreeRTOS的API很多都是带参宏,所以跳来跳去的比较麻烦,而且注释也很多 ...

  5. Color Length UVALive - 5841

    题文:见网页:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=sho ...

  6. How to Get What You Want 如何得到你想要的

    [1]If you want something, give it away. [2]When a farmer wants more seeds, he takes his seeds and gi ...

  7. 【包教包会】Chrome拓展开发实践

    首发于微信公众号<前端成长记>,写于 2019.10.18 导读 有句老话说的好,好记性不如烂笔头.人生中,总有那么些东西你愿去执笔写下. 本文旨在把整个开发的过程和遇到的问题及解决方案记 ...

  8. Zookeeper未授权访问测试

    前言 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提 ...

  9. 后渗透神器Cobalt Strike的安装

    0x01 简介 Cobalt Strike集成了端口转发.扫描多模式端口监听Windows exe木马,生成Windows dll(动态链接库)木马,生成java木马,生成office宏病毒,生成木马 ...

  10. Python3之多线程学习

    这里做一个自己复习多线程的笔记 Python中使用线程有两种方式:函数或者用类来包装线程对象. 函数式:调用 _thread 模块中的start_new_thread()函数来产生新线程.语法如下: ...