HDFS通过java接口实现读、写、删除文件、添加目录等案例

通过URL方式读取HDFS上的文件

package cn.homework;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.io.IOUtils;
public class URLcar {
static{
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory()); //要让Java程序能够识别Hadoop的hdfs URL,需要采用的方法是FsUrlStreamHandlerFactory实例调用URL中的setURLStreamHandlerFactory方法。
}
public static void main(String[] args) throws Exception {
InputStream in = null;
String url = args[0];//用第一个参数作为url
in = new URL(url).openStream();//打开一个输入流
IOUtils.copyBytes(in, System.out, 1024, true);//调用hadoop提供的工具类将输入流读到控制台
}

}

注意:这里使用url的方式读取hdfs上的文件,因此,参数要写成url即  hdfs://master:9000/...     而非uri形式。

Java访问接口——FileSystem来读取HDFS上的文件

有时无法在应用中设置URLStreamHandlarFactory实例,如不受你控制的第三方组件如果已经声明了一个URLStreamHandlerFactory实例,你将无法再使用上述方法从Hadoop中读取数据,这种情况下,需要使用FileSystem API来打开一个文件的输入流。

package cn.homework;
import java.io.InputStream;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

public class FileStreamCat {
public static void main(String[] args) throws Exception {
String uri = args[0];//这里用第一个参数作为uri的值
Configuration con = new Configuration();//加载配置文件,并放在堆空间中
FileSystem fs = FileSystem.get(URI.create(uri), con);//用FileSystem的get方法得到HDFS文件系统的实例对象。
InputStream in = null;
in = fs.open(new Path(uri));
IOUtils.copyBytes(in, System.out, 1024, true);
}

}

注意:这里使用uri的方式读取hdfs上的文件,因此,参数写成uri或url都可以  uri形式:/a.txt(相当于hdfs://master:9000/a.txt)

FSDatalnputStream对象读取HDFS上的文件

package cn.xdl.homework;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class FSDataInputStreamCat{
public static void main(String[] args)throws Exception{
String uri = args[0];
Configuration con = new Configuration();
FileSystem fs = FileSystem.get(URI.create(uri), con);
FSDataInputStream in =fs.open(new Path(uri));
byte buffer[] = new byte[256];
int byteRead = 0;
while((byteRead = in.read(buffer))>0){
System.out.write(buffer, 0, byteRead);
}
}
}

使用FileSystem写入数据(将linux上的一个文件写入HDFS中)

package cn.homework;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FSOutputSummer;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
public class FileCopy {
public static void main(String[] args) throws Exception{
String localSrc = args[0];//linux上的文件uri
String toWhereSrc = args[1];//到hdfs的目标uri
InputStream in = new BufferedInputStream(new FileInputStream(localSrc));//打开一个BufferedInputStream字节输入流
Configuration con = new Configuration();
FileSystem fs = FileSystem.get(URI.create(toWhereSrc), con);
FSDataOutputStream out = fs.create(new Path(toWhereSrc));//这里是creat()方法表示新创建一个文件,如果想在一个文件上追加,请用append()方法。
IOUtils.copyBytes(in, out, 1024, true);
}

}

使用FileSystem写入数据(将linux上的一个文件追加写入到HDFS中)

package cn.homework;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
public class Appendtest {
public static void main(String[] args)throws Exception {
InputStream in = new BufferedInputStream(new FileInputStream(args[0]));
Configuration con = new Configuration();
con.setBoolean("dfs.support.append", true);
con.set("dfs.client.block.write.replace-datanode-on-failure.policy", "NEVER");
con.set("dfs.client.block.write.replace-datanode-on-failure.enable", "true");
FileSystem fs = FileSystem.get(URI.create(args[1]), con);
FSDataOutputStream out =fs.append(new Path(args[1]));
IOUtils.copyBytes(in, out, 1024, true);
}
}

创建目录

package cn.homework;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class MkdirTest {
public static void main(String[] args)throws Exception {
Configuration con = new Configuration();
FileSystem fs = FileSystem.get(URI.create(args[0]), con);
fs.mkdirs(new Path(args[0]));//mkdirs方法可以创建多层目录
}

}

查看HDFS目录下的文件

package cn.homework;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileStatus;
public class FileStatusTest {
public static void main(String[] args)throws Exception {
Configuration con = new Configuration();
FileSystem fs = FileSystem.get(URI.create(args[0]), con);
Path paths[] = new Path[args.length]; //准备一个数组用于存放目录下所有文件的源信息
for(int i = 0;i paths[i]=new Path(args[i]);
}
FileStatus[] status = fs.listStatus(paths); //listStatus方法用于列出目录下的所有文件(文件的地址源信息)
Path[] listedPaths = FileUtil.stat2Paths(status); //用FileUtil工具类将FileSatus类型的数组转换为Path类型的数组
for (Path path : listedPaths) {
System.out.println(path); //遍历数组,打印目录下的所有文件
}
}

}

批量写入文件到HDFS中

package cn.homework;
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.Path;
import org.apache.hadoop.util.Progressable;
public class CopyFileTest {
public static void main(String[] args) throws Exception{
Configuration con = new Configuration();
FileSystem local = FileSystem.getLocal(con);
FileSystem hdfs = FileSystem.get(con);
Path localdir = new Path(args[0]);
Path hdfsdir = new Path(args[1]);
try{
FileStatus[] inputFiles = local.listStatus(localdir); //准备一个FileStatus类型的数组用来存放读取的本地文件源信息
FSDataOutputStream out = hdfs.create(hdfsdir, new Progressable() {
@Override
public void progress() {                        //这里是一个回掉函数,程序执行后执行该方法
// TODO Auto-generated method stub
System.out.println("=========================");
}
});
for(int i = 0 ; i System.out.println(inputFiles[i].getPath().getName());
FSDataInputStream in = local.open(inputFiles[i].getPath());//读取该目录下的所有文件路径
byte[] bytes = new byte[512];
int bytesReader = 0;
while((bytesReader = in.read(bytes))>0){
out.write(bytes, 0, bytesReader);
}
in.close();
}
out.close();

}catch(Exception e){

                e.printStackTrace();

}
}
}

删除文件(两种方式)

使用FileSystem的delete()方法可以永久性删除文件或目录(方法过时)
public boolean delete(Path f, boolean recursive) throws IOException

如果f是一个文件或空目录,那么recursive(递归)的值就会被忽略。只有在recruslve值为true时,一个非空目录及其内容才会被删除(否则会抛出IOException异常)

package cn.homework;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class DeleteFileTest {
public static void main(String[] args)throws Exception {
Configuration con = new Configuration();
FileSystem fs = FileSystem.get(URI.create(args[0]), con);
//fs.delete(new Path(args[0]));//可以删除一切目录,此方法过时了,这里我们将他注掉
fs.delete(new Path(args[0]), false);//当一个目录下有其他文件或目录时,当这里是false时,我们就不可以删除该目录,改成true就可以忽略这个条件
}
}


你可能感兴趣的:(hadoop)