我在使用 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. C语言I博客作业02

    这个作业属于那个课程  C语言程序设计I 这个作业要求在哪 https://edu.cnblogs.com/campus/zswxy/CST2019-3/homework/8656 我在这个课程的目标 ...

  2. pikachu-数字型注入(post)#手工注入

    1, 因为是post型,所以需要抓取数据包 2, 测试结果为数字型注入 提交恒等的语句可以查询到所有的数据信息 3, 使用UNION联合查询法 判断字段数,测试为2个字段时没有报错,所以可以判断字段数 ...

  3. flexible.js分析--JavaScript

    //立即执行函数 (function flexible(window, document) { // 获取的html 的根元素 var docEl = document.documentElement ...

  4. Faith 信念

    Today I’d like to talk about faith. With faith, you’ll go further and never be lost. Faith is free a ...

  5. 调用对象 “ha-datastoresystem”的“HostDatastoreSystem.QueryVmfsDatastoreCreateOptions” 失败。

    VMware vSphere Client上显示:在 ESXi“10.10.10.3”上调用对象 “ha-datastoresystem”的“HostDatastoreSystem.QueryVmfs ...

  6. 机器学习实战_KNN(一)

    [是什么] KNN 即 k_近邻算法(k- nearest neighbor) ,就是寻找K个邻居作为该样本的特征,近朱者赤,近墨者黑,你的邻居是什么特征,那么就认为你也具备该特征:核心公式为: 数据 ...

  7. (未完)经典Web漏洞实战演练靶场笔记

    记录下自己写的经典Web漏洞靶场的write up,包括了大部分的经典Web漏洞实战场景,做个笔记. 0x01 任意文件下载漏洞 if(!empty($_GET['filename'])){ $fil ...

  8. 深入了解一下HTTP缓存机制

    HTTP 缓存机制作为 web 性能优化的重要手段,是Web 开发知识体系库中的一个基础环节,但是对于很多学习者来说,仅仅只是知道浏览器会对请求的静态文件进行缓存,但是为什么被缓存,缓存是怎样生效的, ...

  9. wait,notify,notifyAll详细介绍

    https://www.cnblogs.com/pangyang/articles/5916349.html

  10. python soket服务和客户端Demo

    #服务端from socket import * s=socket(AF_INET,SOCK_STREAM)#IVP4 寻址 tcp协议 s.bind(('',6666))#补丁端口 s.listen ...