利用Java API对HDFS文件系统进行操作

前期配置好centos的namenode和datanode之后,能利用start-dfs.sh正常启动后可用Eclipse编写程序对hdfs文件系统进行相应的增删改查等操作。

  • 首先需要配置hadoop的window环境变量,否则不能进行对hdfs进行正常操作。

打开我的电脑中的属性,环境变量,点新建添加HADOOP_HOME值为你的hadoop所在的目录(注意不要有中文),最后再把hadoop中的bin配置到Path中即可。

利用Java API对HDFS文件系统进行操作_第1张图片

  • 然后时jar包的导入,打开hadoop2.8.1(版本可能不同)文件夹—》share—》hadoop,将hdfs中的hadoop-hdfs-2.8.1.jar和hadoop-hdfs-client-2.8.1.jar和lib文件夹(依赖包)中所有jar包,以及common中hadoop-commom-2.8.1.jar和lib中所有jar包都同意导入到eclipse中的lib中,并且全部都add to build path。
  • 注意:在进行文件上传的时候,需要hadoop2.8.1中bin目录中的某个.exe文件,需要c编译后导入到bin中。因为读取本地的文件需要用到这个.exe文件。

利用Java API对HDFS文件系统进行操作_第2张图片利用Java API对HDFS文件系统进行操作_第3张图片

package hdfs;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.spec.DSAGenParameterSpec;
import java.util.Arrays;

import org.apache.commons.lang.ObjectUtils.Null;
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.junit.Before;
import org.junit.Test;


/**
 * @author THY
 *javaAPI对hdfs的简单操作 
 *fs.copyFromLocalFile(src, dst);本地拷贝到hdfs
 *fs.copyToLocalFile(dst,src);hdfs拷贝到本地
 */
 
public class HdfsClientDemo {

	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		conf.set("dfs.replication", "2");
		conf.set("dfs.blocksize", "64m");
		FileSystem fileSystem = FileSystem.get(new URI("hdfs://hdp-01:9000"), conf, "root");
		
		fileSystem.copyFromLocalFile(new Path("E:\\[neubt]vladstudio_aflyingtree2_2560x1600.jpg"), new Path("/"));
		fileSystem.close();
	}
	
	FileSystem fs = null;
	
	@Before
	public void init() throws Exception {
		Configuration conf = new Configuration();
		conf.set("dfs.replication", "2");
		conf.set("dfs.blocksize", "64m");
		conf.setBoolean("fs.hdfs.impl.disable.cache", true);
		fs = FileSystem.get(new URI("hdfs://hdp-01:9000"), conf, "root");
	}
	
	@Test
	public void testGet() throws IllegalArgumentException, IOException{
		fs.copyToLocalFile(new Path("/hadoop-2.8.1-windows.zip"), new Path("e:/"));
		fs.close();
	}
	
	/**
	 * 将hdfs文件移动到另一个目录/并改名
	 */
	@Test
	public void testCopy() throws IllegalArgumentException, IOException {
		fs.rename(new Path("/hadoop-2.8.1-windows.zip"), new Path("/aaa/hdp-2.8.1-win.zip"));
		fs.close();
	}
	/**
	 * 建立目录/多级目录
	 */
	@Test
	public void testMkdir() throws IllegalArgumentException, IOException {
		fs.mkdirs(new Path("/xx/yy/zz"));
		fs.close();
	}
	/**
	 * 删除文件/目录,
	 */
	@Test
	public void testDeldir() throws IllegalArgumentException, IOException {
		boolean delete = fs.delete(new Path("/[neubt]vladstudio_aflyingtree2_2560x1600.jpg"), true);
		if (delete) {
			System.out.println("文件已经删除");
		}
		fs.close();
	}
	/**
	 * 查询hdfs文件系统,迭代形式显示文件或目录
	 */
	@Test
	public void testLs() throws IllegalArgumentException, IOException {
		RemoteIterator listFiles = fs.listFiles(new Path("/"), true);
		while (listFiles.hasNext()) {
			LocatedFileStatus status = listFiles.next();
			System.out.println("路径:"+status.getPath());
			System.out.println("块大小:"+status.getBlockSize());
			System.out.println("文件长度:"+status.getLen());
			System.out.println("副本数:"+status.getReplication());
			System.out.println("块的位置信息:"+Arrays.toString(status.getBlockLocations())+"\n");
		}
		fs.close();
	}
	/**
	 * 查询hdfs文件,返回文件数组的形式 
	 */
	@Test
	public void testLs2() throws FileNotFoundException, IllegalArgumentException, IOException {
		//���ص������飬fs.listFiles(new Path("/"), true);���ص��ǵ���������Ϊ�ݹ顣
		FileStatus[] listStatus = fs.listStatus(new Path("/aaa"));
		for(FileStatus status:listStatus) {
			System.out.println(status.isDirectory()?"是目录":"不是目录");
			System.out.println("路径:"+status.getPath());
			System.out.println("块大小:"+status.getBlockSize());
			System.out.println("文件长度:"+status.getLen());
			System.out.println("副本数:"+status.getReplication());
			System.out.println("--------------------------------------");
		}
		fs.close();
	}
	
	/**
	 * 读取hdfs中文件的内容,
	 */
	@Test
	public void testReadData() throws IOException {
		FSDataInputStream in = fs.open(new Path("/test.txt"));//hdfs自带流打开文件
		BufferedReader br = new BufferedReader(new InputStreamReader(in,"utf-8"));//读入流并放在缓冲区
		String line=null;
		while ((line=br.readLine())!=null) {
			System.out.println(line);
		}
		
		in.close();
		br.close();
		fs.close();
	}
	
	/**
	 * 读取hdfs中文件中指定偏移的内容,
	 */
	@Test
	public void testRandomReadData() throws IOException {
		FSDataInputStream in = fs.open(new Path("/test.txt"));
		in.seek(12);//定位到12位置开始读
		byte[] buf=new byte[16];//往后读取16个位
		in.read(buf);//ba流读到buf中
		System.out.println(new String(buf));
		in.close();
		fs.close();
	}
	
	/**
	 * 将数据写到hdfs中
	 */
	@Test
	public void testWriteData() throws IOException {
		FSDataOutputStream out = fs.create(new Path("/yy.jpg"),false);
		FileInputStream in = new FileInputStream("E:\\wechatpic_20190309221605.jpg");
		byte[] buf=new byte[1024];
		int read = 0;
		while((read=in.read(buf))!=-1) {
			out.write(buf,0,read);
		}
		out.close();
		fs.close();
	}

}

 

你可能感兴趣的:(Hadoop,etc.)