栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

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

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

服务器ip:192.168.66.128 测试主机能否连接虚拟机
ping 192.168.66.128
修改虚拟机hadoop中core-site.xml
vim ./etc/hadoop/core-site.xml

    fs.defaultFS
    hdfs://192.168.66.128:9000

启动hadoop集群
/usr/local/hadoop/hadoop-3.1.3/sbin/start-all.sh
在windows下的idea创建maven项目,添加下面依赖(对应自己的hadoop版本)

    org.apache.hadoop
    hadoop-client
    3.1.3

将虚拟机下的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;
    }
}

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();
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/673859.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号