window本机远程连接虚拟机的hdfs

服务器ip:192.168.66.128

测试主机能否连接虚拟机

ping 192.168.66.128

修改虚拟机hadoop中core-site.xml

vim ./etc/hadoop/core-site.xml
<property>
    <name>fs.defaultFS</name>
    <value>hdfs://192.168.66.128:9000</value>
</property>

启动hadoop集群

/usr/local/hadoop/hadoop-3.1.3/sbin/start-all.sh

在windows下的idea创建maven项目,添加下面依赖(对应自己的hadoop版本)

<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>3.1.3</version>
</dependency>

将虚拟机下的core-site.xml和hdfs-site.xml拖到maven项目中。

注意:core-site.xml中的ip也要改为服务器的ip
在这里插入图片描述

在虚拟机下的hdfs中创建下面文件

cd /usr/local/hadoop/hadoop-3.1.3/sbin
./bin/hdfs dfs -put 你要复制的文件 复制到hdfs对应的目录

在这里插入图片描述

删除output文件

./bin/hdfs dfs -put -rm -r /output

复制下面代码

注意:ip地址和文件路径要改为自己的

import java.io.IOException;
import java.io.PrintStream;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.log4j.BasicConfigurator;

/**
 * 过滤掉文件名满足特定条件的文件
 */
class MyPathFilter implements PathFilter {
    String reg = null;
    MyPathFilter(String reg) {
        this.reg = reg;
    }
    public boolean accept(Path path) {
        if (!(path.toString().matches(reg)))
            return true;
        return false;
    }
}
/***
 * 利用FSDataOutputStream和FSDataInputStream合并HDFS中的文件
 */
public class MergeFile {
    Path inputPath = null; //待合并的文件所在的目录的路径
    Path outputPath = null; //输出文件的路径
    public MergeFile(String input, String output) {
        this.inputPath = new Path(input);
        this.outputPath = new Path(output);
    }
    public void doMerge() throws IOException {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://192.168.66.128:9000");
        conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
        FileSystem fsSource = FileSystem.get(URI.create(inputPath.toString()), conf);
        FileSystem fsDst = FileSystem.get(URI.create(outputPath.toString()), conf);
        //下面过滤掉输入目录中后缀为.abc的文件
        FileStatus[] sourceStatus = fsSource.listStatus(inputPath,
                new MyPathFilter(".*\\.abc"));
        FSDataOutputStream fsdos = fsDst.create(outputPath);
        PrintStream ps = new PrintStream(System.out);
        //下面分别读取过滤之后的每个文件的内容,并输出到同一个文件中
        for (FileStatus sta : sourceStatus) {
            //下面打印后缀不为.abc的文件的路径、文件大小
            System.out.print("路径:" + sta.getPath() + "    文件大小:" + sta.getLen()
                    + "   权限:" + sta.getPermission() + "   内容:");
            FSDataInputStream fsdis = fsSource.open(sta.getPath());
            byte[] data = new byte[1024];
            int read = -1;

            while ((read = fsdis.read(data)) > 0) {
                ps.write(data, 0, read);
                fsdos.write(data, 0, read);
            }
            fsdis.close();
        }
        ps.close();
        fsdos.close();
    }
    public static void main(String[] args) throws IOException {
        BasicConfigurator.configure();//自动快速地使用缺省Log4j环境。
        MergeFile merge = new MergeFile(
                "hdfs://192.168.66.128:9000/user/root/",
                "hdfs://192.168.66.128:9000/user/root/merge.txt");
        merge.doMerge();
    }
}

可能出现的问题:ermission denied: user=e5bb96, access=WRITE, inode="/user/root":root:supergroup:drwxr-xr-x

IDEA 操作虚拟机中 HDFS 提示 Permission denied

解决

在代码中设置系统变量,需在加载配置类创建 fileSystem 对象前

System.setProperty("HADOOP_USER_NAME", "root");
Configuration configuration = new Configuration();
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

细水长流永不粹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值