Java操控集群

1.创建一个maven项目:

配置pom.xml文件

如下:

<dependencies>
	<!-- 配置缺少的toold.jar -->
		<dependency>
			<groupId>jdk.tools</groupId>
			<artifactId>jdk.tools</artifactId>
			<version>1.6</version>
			<scope>system</scope>
			<systemPath>C:\Program Files\Java\jdk1.8.0_131\lib\tools.jar</systemPath>
		</dependency>
		<!-- https://mvnrepository.com/artifact/junit/junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-common</artifactId>
			<version>2.6.4</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-hdfs</artifactId>
			<version>2.6.4</version>
		</dependency>

	</dependencies>

配置信信息在网址:

https://mvnrepository.com            分别查找:Hadoop HDFS 、Junit、Hadoop common

注:寻找自己对象的版本

public class Demo01 {
	@Test
	public void test01() throws URISyntaxException, IOException {
		//首先需要获取文件系统
		Configuration conf=new Configuration();
		URI uri=new URI("hdfs://hadoop:8020");
	
		FileSystem fs=FileSystem.get(uri,conf);
		//fs.copyFromLocalFile(new Path("C:\\Users\\12042\\Desktop\\a1.txt"), new Path("/"));
		RemoteIterator<LocatedFileStatus> it=fs.listFiles(new Path("/"), true);
		while(it.hasNext()) {
			LocatedFileStatus lfs=it.next();
			System.out.println(lfs.getPath());
			System.out.println("长度:"+lfs.getLen());
		}
		fs.close();
	}
	
	//文件的上传
	@Test
	public void uploadFiles() throws Exception {
		Configuration conf=new Configuration();
		URI uri=new URI("hdfs://hadoop:8020");
		FileSystem fs=FileSystem.get(uri,conf,"root");
		fs.copyFromLocalFile(true,new Path("C:\\Users\\12042\\Desktop\\1111.txt"), new Path("/"));
		fs.close();
	}
	//创建文件夹
	@Test
	public void mkdirs11() throws Exception {
		Configuration conf=new Configuration();
		URI uri=new URI("hdfs://hadoop:8020");
		FileSystem fs=FileSystem.get(uri,conf,"root");
		fs.mkdirs(new Path("/ddd"));
		RemoteIterator<LocatedFileStatus> it=fs.listFiles(new Path("/"), true);
		while(it.hasNext()) {
			LocatedFileStatus lfs=it.next();
			System.out.println(lfs.getPath());
			System.out.println("长度:"+lfs.getLen());
		}
		fs.close();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值