springboot JAVA API 操作hadoop伪分布式集群

虚拟机(192.168.74.114)中搭建了hadoop伪分布式集群,查看集群运行状态

  • springboot maven依赖
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-hdfs</artifactId>
			<version>2.7.4</version>
			<exclusions>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-log4j12</artifactId>
				</exclusion>
				<exclusion>
					<groupId>log4j</groupId>
					<artifactId>log4j</artifactId>
				</exclusion>
				<exclusion>
					<groupId>javax.servlet</groupId>
					<artifactId>servlet-api</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-common</artifactId>
			<version>2.7.4</version>
			<exclusions>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-log4j12</artifactId>
				</exclusion>
				<exclusion>
					<groupId>javax.servlet</groupId>
					<artifactId>servlet-api</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-client</artifactId>
			<version>2.7.4</version>
			<exclusions>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-log4j12</artifactId>
				</exclusion>
				<exclusion>
					<groupId>log4j</groupId>
					<artifactId>log4j</artifactId>
				</exclusion>
				<exclusion>
					<groupId>javax.servlet</groupId>
					<artifactId>servlet-api</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>
  • java代码
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Before;
import org.junit.Test;

import java.net.URI;

public class HDFSUtils {
    public static final String HDFS_PATH = "hdfs://192.168.74.114:9000";
    FileSystem filesystem = null;
    Configuration configuration = null;

    @Before
    public void init() throws Exception{
        configuration = new Configuration();
        filesystem = FileSystem.get(new URI(HDFS_PATH),configuration,"root");
    }

//    上传文件
    @Test
    public void testUpload() throws Exception {
        Thread.sleep(2000);
        filesystem.copyFromLocalFile(new Path("D:/access.log"), new Path("/data/temp/access.log"));
        filesystem.close();
    }

//    新建文件路径
    @Test
    public void makeDir()throws Exception{
        filesystem.mkdirs(new Path("/data/temp"));
        filesystem.close();
    }

//    删除文件
    @Test
    public void delFile()throws Exception{
        filesystem.deleteOnExit(new Path("/data/temp/access.log"));
        filesystem.close();
    }

}
  • hdfs shell命令查看(也可通过页面查看)
ls查看文件:
    hdfs dfs -ls 路径               
cat输出文件:
    hdfs dfs -cat 文件名

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值