HDFS文件的基本操作:

package wjn;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Scanner; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.io.IOUtils;
import org.apache.log4j.BasicConfigurator; public class ww { public static Scanner sc = new Scanner(System.in);
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub main9();
} public static void main10() throws IOException{
String p1 = "/user/hadoop/text/text1";
String p2 = "/user/hadoop";
if(mv(p1,p2)){
System.out.println("文件移动成功");
}else{
System.out.println("文件移动失败");
}
} public static void main9() throws IOException{
String hdfspath = "/user/hadoop/text2"; if(deleteFileFromHDFS(hdfspath)){
System.out.println("已删除文件"+hdfspath);
}else{
System.out.println("删除文件失败");
}
} public static void main7() throws IOException{ String hdfspath1 = "/user/hadoop";
boolean forcedelete = false;
if(!ifex(hdfspath1)){
mkdir(hdfspath1);
System.out.println("创建目录"+hdfspath1);
}else{
if(isempty(hdfspath1)||forcedelete){
rmDir(hdfspath1);
System.out.println("删除目录"+hdfspath1);
}else{
System.out.println("目录不为空,不删除");
}
} } public static void main6() throws IOException{ String hdfspath = "/user/hadoop/text2";
String hdfspath1 = "/user/hadoop";
if(ifex(hdfspath)){
deleteFileFromHDFS(hdfspath);
System.out.println("该路径存在,删除路径"+hdfspath);
}else{
if(!ifex(hdfspath1)){
mkdir(hdfspath1);
System.out.println("创建文件夹"+hdfspath1);
}
touchz(hdfspath);
System.out.println("创建路径"+hdfspath);
}
} public static void main5() throws IOException{ String hdfspath = "/user/hadoop";
System.out.println("所有文件信息如下");
lsDir(hdfspath);
} public static void main4() throws IOException{
String hdfspath = "/user/hadoop/text2";
System.out.println("文件信息如下");
ls(hdfspath);
} public static void main3() throws IOException{
String hdfspath = "/user/hadoop/text2";
cat(hdfspath);
System.out.println("读取完成");
} public static void main2() throws IOException{
String localpath = "/home/hadoop/1234.txt";
String hdfspath = "/user/hadoop/text2";
download(hdfspath,localpath);
System.out.println("文件下载成功");
} public static void main1() throws IOException{
String localpath = "/home/hadoop/123.txt";
String hdfspath = "/user/hadoop/text2";
if(ifex(hdfspath)){
System.out.println("文件存在,请选择追加(1)还是覆盖(2)");
int i = sc.nextInt();
if(i==1){
appendFileToHDFS(hdfspath,localpath);
System.out.println("文件追加成功");
}else if(i==2){
deleteFileFromHDFS(hdfspath);
update(localpath,hdfspath);
System.out.println("文件覆盖成功");
}else{
System.out.println("输入有误");
} }else{
update(localpath,hdfspath);
System.out.println("文件不存在,上传成功");
}
} public static void update(String localpath , String hdfspath) throws IOException{ InputStream in = new BufferedInputStream(new FileInputStream(localpath));
FileSystem fileSystem = FileSystem.get(URI.create(hdfspath), new Configuration());
OutputStream out = fileSystem.create(new Path(hdfspath)); IOUtils.copyBytes(in, out, 4096, true);
fileSystem.close(); } //判断hdfs中文件是否存在
public static boolean ifex(String hdfspath) throws IOException{ Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://localhost:9000");
conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
FileSystem fs = FileSystem.get(conf);
if (fs.exists(new Path(hdfspath))){
fs.close();
return true;
}else{
fs.close();
return false;
}
} //创建目录
public static void mkdir(String hdfspath) throws IOException{ FileSystem fileSystem = FileSystem.get(URI.create(hdfspath), new Configuration());
Path dirPath = new Path(hdfspath);
fileSystem.mkdirs(dirPath);
fileSystem.close();
} //创建文件
public static void touchz(String hdfspath) throws IOException{ FileSystem fileSystem = FileSystem.get(URI.create(hdfspath), new Configuration());
Path dirPath = new Path(hdfspath);
FSDataOutputStream outputStream = fileSystem.create(dirPath);
outputStream.close();
fileSystem.close();
} public static void appendFileToHDFS(String hdfsPath, String localFilePath) throws IOException {
Configuration config = new Configuration();
config.set("dfs.client.block.write.replace-datanode-on-failure.policy", "NEVER");
config.set("dfs.client.block.write.replace-datanode-on-failure.enable", "true"); FileSystem fileSystem = FileSystem.get(URI.create(hdfsPath), config); InputStream in = new BufferedInputStream(new FileInputStream(localFilePath));
FSDataOutputStream out = fileSystem.append(new Path(hdfsPath)); IOUtils.copyBytes(in, out, 4096, true);
fileSystem.close();
} //删除文件
public static boolean deleteFileFromHDFS(String hdfsPath) throws IOException {
FileSystem fileSystem = FileSystem.get(URI.create(hdfsPath), new Configuration());
boolean result = fileSystem.deleteOnExit(new Path(hdfsPath));
fileSystem.close();
return result;
} //删除目录
public static void rmDir(String hdfspath) throws IOException{ FileSystem fileSystem = FileSystem.get(URI.create(hdfspath), new Configuration());
fileSystem.delete(new Path(hdfspath),true);
fileSystem.close();
} //判断目录是否为空
public static boolean isempty(String hdfspath) throws IOException{ FileSystem fileSystem = FileSystem.get(URI.create(hdfspath), new Configuration());
RemoteIterator<LocatedFileStatus> remoteIterator = fileSystem.listFiles(new Path(hdfspath), true);
return !remoteIterator.hasNext();
} public static void download (String hdfsPath, String localPath) throws IOException {
FileSystem fileSystem = FileSystem.get(URI.create(hdfsPath), new Configuration()); FSDataInputStream in = fileSystem.open(new Path(hdfsPath));
OutputStream out = new FileOutputStream(localPath); IOUtils.copyBytes(in, out, 4096, true);
fileSystem.close();
} //根据hdfs路径输出其内容到终端
public static void cat(String hdfspath) throws IOException{ FileSystem fileSystem = FileSystem.get(URI.create(hdfspath), new Configuration()); FSDataInputStream in = fileSystem.open(new Path(hdfspath)); BufferedReader d = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = d.readLine())!=null){
System.out.println(line);
}
d.close();
in.close();
} //显示hdfs中指定文件的信息
public static void ls(String hdfspath) throws IOException{ FileSystem fileSystem = FileSystem.get(URI.create(hdfspath), new Configuration());
Path path = new Path(hdfspath);
FileStatus[] fileStatus = fileSystem.listStatus(path);
for (FileStatus s : fileStatus){
System.out.println("路径:"+s.getPath().toString());
System.out.println("权限:"+s.getPermission().toString());
System.out.println("大小:"+s.getLen());
Long time = s.getModificationTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = format.format(time);
System.out.println("时间:"+date);
}
} //显示文件夹下所有文件的信息
public static void lsDir(String hdfspath) throws IOException{ FileSystem fileSystem = FileSystem.get(URI.create(hdfspath), new Configuration());
Path dirPath = new Path(hdfspath);
RemoteIterator<LocatedFileStatus> remoteIterator = fileSystem.listFiles(dirPath, true);
while(remoteIterator.hasNext()){ FileStatus s = remoteIterator.next();
System.out.println("路径:"+s.getPath().toString());
System.out.println("权限:"+s.getPermission().toString());
System.out.println("大小:"+s.getLen());
Long time = s.getModificationTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = format.format(time);
System.out.println("时间:"+date);
System.out.println();
}
fileSystem.close();
} //移动文件
public static boolean mv(String path1,String path2) throws IOException{ FileSystem fileSystem = FileSystem.get(URI.create(path1), new Configuration()); Path p1 = new Path(path1);
Path p2 = new Path(path2);
boolean result = fileSystem.rename(p1, p2);
fileSystem.close();
return result;
} }

HDFS文件的基本操作的更多相关文章

  1. [bigdata] 使用Flume hdfs sink, hdfs文件未关闭的问题

    现象: 执行mapreduce任务时失败 通过hadoop fsck -openforwrite命令查看发现有文件没有关闭. [root@com ~]# hadoop fsck -openforwri ...

  2. HDFS文件和HIVE表的一些操作

    1. hadoop fs -ls  可以查看HDFS文件 后面不加目录参数的话,默认当前用户的目录./user/当前用户 $ hadoop fs -ls 16/05/19 10:40:10 WARN ...

  3. Hadoop之HDFS文件操作常有两种方式(转载)

    摘要:Hadoop之HDFS文件操作常有两种方式,命令行方式和JavaAPI方式.本文介绍如何利用这两种方式对HDFS文件进行操作. 关键词:HDFS文件    命令行     Java API HD ...

  4. Spark读取HDFS文件,文件格式为GB2312,转换为UTF-8

    package iie.udps.example.operator.spark; import scala.Tuple2; import org.apache.hadoop.conf.Configur ...

  5. spark读hdfs文件实现wordcount并将结果存回hdfs

    package iie.udps.example.operator.spark; import scala.Tuple2; import org.apache.spark.SparkConf; imp ...

  6. HDFS 文件读写过程

    HDFS 文件读写过程 HDFS 文件读取剖析 客户端通过调用FileSystem对象的open()来读取希望打开的文件.对于HDFS来说,这个对象是分布式文件系统的一个实例. Distributed ...

  7. Hadoop HDFS文件常用操作及注意事项

    Hadoop HDFS文件常用操作及注意事项 1.Copy a file from the local file system to HDFS The srcFile variable needs t ...

  8. Hadoop HDFS文件常用操作及注意事项(更新)

    1.Copy a file from the local file system to HDFS The srcFile variable needs to contain the full name ...

  9. hadoop的hdfs文件操作实现上传文件到hdfs

    这篇文章主要介绍了使用hadoop的API对HDFS上的文件访问,其中包括上传文件到HDFS上.从HDFS上下载文件和删除HDFS上的文件,需要的朋友可以参考下hdfs文件操作操作示例,包括上传文件到 ...

随机推荐

  1. 常见前端面试题CSS部分

    1.盒模型 IE 盒子模型:IE的width部分包含了 border 和 pading; 标准 W3C 盒子模型: width/height+border+padding+margin; 2.清除浮动 ...

  2. RabbitMQ ——整体架构

    一 .概述 从整体上讲Rabbitmq就是一个生产者消费者的模型. 我们将中间的整个broker就当做是一个消息中间件的实体就可以了. 单从这个方面上讲,生产者发送消息到broker上面,然后消费者从 ...

  3. 几种激活Profile的方式

    方法一: 选择spring.profiles.active spring.profiles.active=prodspring.profiles.active=dev 方法二: 选择spring.pr ...

  4. 第十五章 Kubernetes调度器

    一.简介 Scheduler 是 kubernetes 的调度器,主要的任务是把定义的 pod 分配到集群的节点上.听起来非常简单,但有很多要考虑的问题: ① 公平:如何保证每个节点都能被分配资源 ② ...

  5. maven clean后 编译报错

    <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <config ...

  6. JS中JSON.stringify()方法,将js对象(json串)转换成字符串,传入服务器

    JSON 通常用于与服务端交换数据. 在向服务器发送数据时一般是字符串. 我们可以使用 JSON.stringify() 方法将 JavaScript 对象转换为字符串. 语法 JSON.string ...

  7. Java线程通信-生产者消费者问题

    线程通信示例——生产者消费者问题 这类问题描述了一种情况,假设仓库中只能存放一件产品,生产者将生产出来的产品放入仓库,消费者将仓库中的产品取走消费.假设仓库中没有产品,则生产者可以将 产品放入仓库,有 ...

  8. SCJP读书之知识点:

    1:实例变量和局部变量 实例变量:是在类中进行声明的,可以有public,private等修饰符进行修饰. 局部变量:在方法中进行声明,生命周期就是方法开始,到方法结束.但是可以进行对象的引用来调用. ...

  9. Python构造器及析构器:__init__与__new__及__del__

    __init__与__new__这两个魔法方法组成了Python类对象的构造器,在Python类实例化时,其实最先调用的不是__init__而是__new__.__new__是负责实例化对象的,而__ ...

  10. python 简单抓取网页并写入excel实例

    # -*- coding: UTF-8 -*- import requests from bs4 import BeautifulSoup import xlwt import time #获取第一页 ...