windows下搭建maven环境,并建立maven项目用java操作hdfs

为什么要用maven:

如果建立大一点的java项目的话,需要的jar和各jar包之间的依赖关系会很多,需要自己手动找jar包和jar依赖导入项目中,

maven就是来解决这个问题的,用来管理java项目的jar包依赖和项目构建操作。

安装配置:

maven官网下载zip压缩包,解压到自己存放的目录,目录名之间最好不要有空格。

配置系统环境变量:

添加MAVEN_HOME=安装目录

测试:--------------------cmd下 输入 echo $MAVEN_HOME ,输出安装目录即可。

现在给intellij idea整合   maven环境:

在settings ---------------》》maven ------------------------》》有个directory:选中安装目录即可。----------------》》第二项:sitting file:选中maven下的setting.xml, 并在里面配置自己本地仓库的位置:1.localRepository(本地仓库) :  2.mirror(私服)  ------  在线仓库(可先不配置)。

以上配置完成。

新建项目:

创建maven项目------------》maven--------------------》写ANG信息-----------------》next ------------>next----------->ok。

接下来在 pom.xml添加内容:  引入hadoop需要的jar包和依赖(只需要写配置信息即可,包和依赖是自动导入)。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.judy</groupId>
    <artifactId>MyMaven</artifactId>
    <version>1.0</version>

    <!--配置项目中某些jar包的版本-->
    <properties>
        <hadoop.version>2.7.1</hadoop.version>
    </properties>

    <!--在指定的项目中指定私服配置-->
    <repositories>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://10.0.88.249:8081/nexus/content/repositories/public/</url>
        </repository>
    </repositories>

    <!--项目jar包的依赖配置-->
    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
    </dependencies>


</project>

在这引入的是hadoop2.7.1的jar包依赖(因为这个私服地址2.9.1的jar包不全,所以只能用2.7的),建议用2.9.1的。

配置之后idea会自动import。

现在可以写java代码操作hdfs了。

hdfs的相关包和类和用法,详见官网API:http://hadoop.apache.org/docs/stable/api/index.html

运行操作之前,别忘了先确认自己的hdfs相关服务是否开启,

package com.judy.first;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
//import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class HdfsTest {
    public static void main(String[] args) {
//        String hdfsPath="/score.txt";
//        copyHdfsFileToConsole(hdfsPath);
        String hdfsfilePath = "/test/bb.txt";
        copyLocalFiletoHdfs(hdfsfilePath);

    }

    /**
     * 读取hdfs的文件并且显示在控制台
     *
     * @param hdfsPath hdfs的文件路径
     */
    public static void copyHdfsFileToConsole(String hdfsPath) {
        FileSystem fileSystem = null;
        FSDataInputStream inputStream = null;


        try {
            //1.获取
            Configuration conf = new Configuration();
            //2.配置hdfs的访问入口  (配置文件core-site.xml放到同包的resources下)

            //3.获取hdfs对象
            fileSystem = FileSystem.newInstance(conf);
            //4.对hdfs对象进行操作
            inputStream = fileSystem.open(new Path(hdfsPath));
            IOUtils.copyBytes(inputStream, System.out, 4096, true);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 上传windows本地文件到hdfs
     *
     * @param hdfsFilePath
     */
    public static void copyLocalFiletoHdfs(String hdfsFilePath) {
        FileSystem fileSystem = null;
        FSDataOutputStream outputStream = null;
        FileInputStream fileInputStream = null;
        Configuration conf = new Configuration();
        /*File file = new File(hdfsFilePath);
        if (!file.exists()){
            file.mkdirs();
        }*/
        try {
            fileSystem = FileSystem.newInstance(conf);
//            DistributedFileSystem dfs = null;
//            outputStream = dfs.create(new Path(filePath));
            outputStream = fileSystem.create(new Path(hdfsFilePath), true);
            fileInputStream = new FileInputStream(new File("D://aa.txt"));
            IOUtils.copyBytes(fileInputStream, outputStream, 4096, true);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

在web查看是否成功呀:http://cent01:50700   --------------我的主机呀

ok,成功 !

我就测试一遍就过了,无压力啊 /笑哭~~~~

果然java代码只要有oop思想,能看懂API就毫无压力的,写项目是无压力了,不是说原生java写算法哈,这就很不一样了哈哈。

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个关于使用Java操作HDFS的问题。首先,你需要安装Hadoop并启动HDFS服务。然后,你可以使用Hadoop提供的Java API来操作HDFS。下面是一个简单的示例代码,它可以删除HDFS上的文件: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class HdfsDeleteExample { public static void main(String[] args) throws Exception { String hdfsUri = "hdfs://localhost:9000"; String filePath = "/path/to/file"; Configuration conf = new Configuration(); conf.set("fs.defaultFS", hdfsUri); FileSystem fs = FileSystem.get(conf); Path path = new Path(filePath); boolean result = fs.delete(path, true); if(result) { System.out.println("File deleted successfully."); } else { System.out.println("File deletion failed."); } } } ``` 你只需要将 `hdfsUri` 和 `filePath` 替换成你实际使用的HDFS地址和文件路径即可。 如果你需要下载HDFS上的文件,可以使用以下代码: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class HdfsDownloadExample { public static void main(String[] args) throws Exception { String hdfsUri = "hdfs://localhost:9000"; String srcFilePath = "/path/to/src/file"; String destFilePath = "/path/to/dest/file"; Configuration conf = new Configuration(); conf.set("fs.defaultFS", hdfsUri); FileSystem fs = FileSystem.get(conf); Path srcPath = new Path(srcFilePath); Path destPath = new Path(destFilePath); fs.copyToLocalFile(srcPath, destPath); System.out.println("File downloaded successfully."); } } ``` 同样,你只需要将 `hdfsUri`、`srcFilePath` 和 `destFilePath` 替换成你实际使用的HDFS地址和文件路径即可。 希望这些示例代码能够帮助你完成作业。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值