java 追加写入hdfs_java操作之HDFS-创建-删除目录-读写文件-追加写文件
Hadoop文件操作之HDFS,创建。删除目录,读写文件,追加写文件
package hadoop.hadoop_demo;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
*
org.apache.hadoop
hadoop-common
2.7.2
org.apache.hadoop
hadoop-hdfs
2.7.2
*
*/
public class App {
public static void main(String[] args) throws Exception {
System.out.println("Hello World!");
String hdfs = "hdfs://192.168.1.111:9000";
// mkdir(hdfs,"gaojs");
// touchFile(hdfs,"gjs/1.log");
rmdir(hdfs, "zookeeper_server.pid");
appendFile(hdfs, "gjs/1.log");
// readFile(hdfs);
}
/**
* 追加文件,新版本才支持
* @param hdfs
* @param fullNasme
* @throws Exception
*/
private static void appendFile(String hdfs, String fullNasme)
throws Exception {
FileSystem fileSystem = getFileSystem();
OutputStream out = fileSystem.append(new Path(hdfs + "/" + fullNasme));
out.write(("I am gaojs, who are you" + System.currentTimeMillis() + "\r\n")
.getBytes("UTF-8"));
out.flush();
out.close();
}
/**
* 取得FileSystem
* @return
* @throws Exception
*/
public static final FileSystem getFileSystem() throws Exception {
String hdfs = "hdfs://192.168.1.111:9000";
Configuration conf = new Configuration();
conf.set("dfs.client.block.write.replace-datanode-on-failure.policy","NEVER");
conf.set("dfs.client.block.write.replace-datanode-on-failure.enable","true");
FileSystem fileSystem = FileSystem.get(URI.create(hdfs), conf);
System.out.println(fileSystem);
return fileSystem;
}
/**
* 读文件
* @param hdfs
* @throws Exception
*/
private static void readFile(String hdfs) throws Exception {
FileSystem fileSystem = getFileSystem();
InputStream in = fileSystem.open(new Path(hdfs + "/"
+ "zookeeper_server.pid"));
IOUtils.copy(in, System.out);
fileSystem.close();
}
/**
* 创建空文件
* @param hdfs
* @param fullNasme
* @throws Exception
*/
private static void touchFile(String hdfs, String fullNasme)
throws Exception {
FileSystem fileSystem = getFileSystem();
boolean res = fileSystem
.createNewFile(new Path(hdfs + "/" + fullNasme));
if (res) {
System.out.println("-------create File Success------" + fullNasme);
} else {
System.out.println("-------create File Fail------" + fullNasme);
}
fileSystem.close();
}
/**
* 删除文件或者目录
* @param hdfs
* @param fullNasme
* @throws Exception
*/
private static void rmdir(String hdfs, String fullNasme) throws Exception {
FileSystem fileSystem = getFileSystem();
boolean res = fileSystem.delete(new Path(hdfs + "/" + fullNasme));
if (res) {
System.out.println("------rmdir Success------" + fullNasme);
} else {
System.out.println("------rmdir Fail------" + fullNasme);
}
fileSystem.close();
}
/**
* 创建目录
* @param hdfs
* @param fullNasme
* @throws Exception
*/
private static void mkdir(String hdfs, String fullNasme) throws Exception {
FileSystem fileSystem = getFileSystem();
boolean res = fileSystem.mkdirs(new Path(hdfs + "/" + fullNasme));
if (res) {
System.out.println("-------mkdir Success------" + fullNasme);
} else {
System.out.println("-------mkdir Fail------" + fullNasme);
}
}
}
错误解决方案:
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
DFS[DFSClient[clientName=DFSClient_NONMAPREDUCE_242718953_1, ugi=Administrator (auth:SIMPLE)]]
Exception in thread "main" java.io.IOException: Failed to replace a bad datanode on the existing pipeline due to no more good datanodes being available to try. (Nodes: current=[DatanodeInfoWithStorage[192.168.1.111:50010,DS-c7e4fa47-633d-4d8b-aa09-c50b1e6a411a,DISK]], original=[DatanodeInfoWithStorage[192.168.1.111:50010,DS-c7e4fa47-633d-4d8b-aa09-c50b1e6a411a,DISK]]). The current failed datanode replacement policy is DEFAULT, and a client may configure this via 'dfs.client.block.write.replace-datanode-on-failure.policy' in its configuration.
at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.findNewDatanode(DFSOutputStream.java:929)
at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.addDatanode2ExistingPipeline(DFSOutputStream.java:992)
at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.setupPipelineForAppendOrRecovery(DFSOutputStream.java:1160)
at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.run(DFSOutputStream.java:455)
解决方案:
conf.set("dfs.client.block.write.replace-datanode-on-failure.policy","NEVER");
conf.set("dfs.client.block.write.replace-datanode-on-failure.enable","true");
java 追加写入hdfs_java操作之HDFS-创建-删除目录-读写文件-追加写文件相关推荐
- c#ftp操作全解:创建删除目录,上传下载文件,删除移动文件,文件改名,文件目录查询
全栈工程师开发手册 (作者:栾鹏) c#教程全解 c#实现ftp的操作.包括创建删除目录,上传下载文件,删除移动文件,文件改名,文件目录查询. 在调试ftp前,需要在目标主机上开启ftp功能.这里在本 ...
- Java Stream的流操作,居然让我的代码越写越丝滑?
概念 Stream将要处理的元素集合看作一种流,在流的过程中,借助Stream API对流中的元素进行操作,比如:筛选.排序.聚合等. Stream` 的操作符大体上分为两种:`中间操作符`和`终止操 ...
- java在文件尾部写文件_Java在文件尾部追加内容
import java.io.FileWriter; import java.io.IOException; import java.io.RandomAccessFile; /** * 将内容追加到 ...
- HDFS的读文件、写文件过程
目录 HDFS的重要特性 HDFS写数据分析 HDFS读数据分析 HDFS删除数据分析 NameNode元数据管理原理分析 HDFS的重要特性 HDFS是一个分布式文件系统,通过统一的命名空间(类似于 ...
- 操作系统之文件管理:6、文件的基本操作(创建文件、打开文件、删除文件、关闭文件、读文件、写文件)
5.文件的基本操作 思维导图 文件操作 创建文件 删除文件 读文件 写文件 打开文件 关闭文件 思维导图 文件操作 创建文件 删除文件 读文件 写文件 打开文件 1.在系统中有一张系统的文件打开表,用 ...
- Java打包后读文件和写文件操作方法
javaIO操作在打包后经常会报出文件找不到的错误: java.io.FileNotFoundException: D:\package\bbb\history.xml (系统找不到指定的文件.) 那 ...
- Java读文件和写文件
import java.io.*;public class FileTest {String path = "C:\\Users\\Desktop\\test.txt";File ...
- python读文件和写文件-python开发--从文件中读取数据和写入文件
#! /usr/bin/env python -*- coding:utf-8 -*- """ @Author:gcan @Email:1528667112@qq.com ...
- .dat文件写入byte类型数组_Go语言学习基础-读文件、写文件、行过滤器
Reading File 读文件 读写文件是许多Go程序所需的基本任务.首先,我们将看一些读取文件的示例.读取文件需要检查是否出现调用错误. 最基本的文件读取任务是将文件的全部内容读到内存中iouti ...
最新文章
- 计算机组成原理考研重点
- vSphere 6.5 新功能 (1) - 全功能 vCenter S
- 允许使用抽象类类型 isearchboxinfo 的对象_Java面向对象编程三大特征 - 多态
- FileUpload路径
- JVM(4)之 使用MAT排查堆溢出
- spring data jpa是什么?
- 如何用Java创建不可变的Map
- 张一鸣这一条微博,阿里P8的我,竟然想了一夜
- 那些年删过的库,跑过的路,你从中找到解决方法了吗?
- seaJS 模块加载过程分析
- 示波器触发功能中的释抑
- maven项目打包命令
- 一些常用的Maven命令的作用
- MacQQ消息防撤回
- java邮箱的表达式_Java邮箱正则表达式
- CentOS6与CentOS7的区别
- 陶哲轩实分析 习题6.3.3
- 无法打开匿名级安全令牌——CV明
- Linux 内核签名(签名内核模块)、linux 驱动签名
- Map.putAll() 的用法
热门文章
- mysql 表名 参数化_我可以在准备好的语句中参数化表名吗?
- Markdown编辑器的使用方法
- mysql封装成类_python操作mysql封装成类
- c实现三角形角度大于一个值_初中数学|高分必备!数学三角形相关知识点梳理汇总...
- allpairs使用方法_软件测试|正交试验测试用例设计方法
- html点击隐藏点击出现,点击按钮,内容隐藏,再点击一下,然后内容又显示了,这种效果怎么做?然后默认的是隐藏的...
- telnet 一直显示trying_Telnet背后的故事
- android发送json格式,Android---创建Json格式数据
- java实验四云南大学_云南大学JAVA程序设计实验四
- 每天一个linux命令(9):touch 命令