【大数据基础】hdfs中client是什么

2020-12-27 15:55发布

17条回答
不吃鱼的猫
2楼 · 2020-12-27 16:25

客户端

在HDFS之上将数据压缩好后,再存储到HDFS 2、在HDFS部支持数据dao压缩,这里又可以分为几种方法:

2.1、压缩工容作在DataNode上完成,这里又分两种方法: 

2.1.1、数据接收完后,再压缩 这个方法对HDFS的改动最小,但效果最低

我自己打call
4楼 · 2020-12-27 19:04
1、在HDFS之上将数据压缩好后,再存储到HDFS 2、在HDFS内部支持数据压缩,这里又可以分为几种方法: 2.1、压缩工作在DataNode上完成,这里又分两种方法: 2.1.1、数据接收完后,再压缩 这个方法对HDFS的改动最小,但效果最低,


无需指教
5楼 · 2020-12-28 08:41

1. hdfs定义

HDFS is the primary distributed storage used by Hadoop applications. A HDFS cluster primarily consists of a NameNode that manages the file system metadata and DataNodes that store the actual data.

2. hdfs架构


3. hdfs实例

  作为文件系统,文件的读写才是核心:

复制代码

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. */import java.io.File;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.FSDataInputStream;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.Path;public class HadoopDFSFileReadWrite {  
  static void usage () {
    System.out.println("Usage : HadoopDFSFileReadWrite  ");
    System.exit(1);
  }  
  static void printAndExit(String str) {
    System.err.println(str);
    System.exit(1);
  }  public static void main (String[] argv) throws IOException {
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(conf);    if (argv.length != 2)
      usage();    
    // Hadoop DFS deals with Path
    Path inFile = new Path(argv[0]);
    Path outFile = new Path(argv[1]);   
    // Check if input/output are valid
    if (!fs.exists(inFile))
      printAndExit("Input file not found");    if (!fs.isFile(inFile))
      printAndExit("Input should be a file");    if (fs.exists(outFile))
      printAndExit("Output already exists");    // Read from and write to new file
    FSDataInputStream in = fs.open(inFile);
    FSDataOutputStream out = fs.create(outFile);    byte buffer[] = new byte[256];    try {      int bytesRead = 0;      while ((bytesRead = in.read(buffer)) > 0) {
        out.write(buffer, 0, bytesRead);
      }
    } catch (IOException e) {
      System.out.println("Error while copying file");
    } finally {
      in.close();
      out.close();
    }
  }
}

复制代码

上述示例,将一个文件的内容复制到另一个文件中,具体步骤如下:

第一步:创建一个文件系统实例,给该实例传递新的配置。

Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);

第二步:获取文件路径

复制代码

// Hadoop DFS deals with Path
    Path inFile = new Path(argv[0]);
    Path outFile = new Path(argv[1]);   
    // Check if input/output are valid
    if (!fs.exists(inFile))
      printAndExit("Input file not found");    if (!fs.isFile(inFile))
      printAndExit("Input should be a file");    if (fs.exists(outFile))
      printAndExit("Output already exists");

复制代码

第三步:打开文件输入输出流,将输入流写到输出流中:

复制代码

    // Read from and write to new file
    FSDataInputStream in = fs.open(inFile);
    FSDataOutputStream out = fs.create(outFile);    byte buffer[] = new byte[256];    try {      int bytesRead = 0;      while ((bytesRead = in.read(buffer)) > 0) {
        out.write(buffer, 0, bytesRead);
      }
    } catch (IOException e) {
      System.out.println("Error while copying file");
    } finally {
      in.close();
      out.close();
    }

复制代码

上面文件读写功能涉及到了文件系统FileSystem、配置文件Configuration、输入流/输出流FSDataInputStream/FSDataOutputStream

 4. 基本概念分析

    4.1 文件系统

  文件系统的层次结构如下所示:

  文件系统有两个重要的分支,一个是分布式文件系统,另一个是“本地”(映射到本地连接的磁盘)文件系统,本地磁盘适用于比较少的hadoop实例和测试。绝大部分情况下使用分布式文件系统,hadoop 分布式文件系统使用多个机器的系统,但对用户来说只有一个磁盘。它的容错性和大容量性使它非常有用。

  4.2 配置文件

  配置文件的层次结构如下:

我们关注的是HdfsConfiguration,其涉及到的配置文件有hdfs-default.xml和hdfs-site.xml:

复制代码

  static {
    addDeprecatedKeys();    // adds the default resources
    Configuration.addDefaultResource("hdfs-default.xml");
    Configuration.addDefaultResource("hdfs-site.xml");

  }

复制代码

 

  4.3 输入/输出流

    输入/输出流和文件系统相对应,先看一下输入流:

  

其中,HdfsDataInputStream是FSDataInputStream的实现,其构造函数为:

  public HdfsDataInputStream(DFSInputStream in) throws IOException {    super(in);
  }
DFSInputStream层次结构如下图所示:

  在了解一下输出流:

  

其中,重点是HdfsDataOutputStream,其构造函数为:

  public HdfsDataOutputStream(DFSOutputStream out, FileSystem.Statistics stats,      long startPosition) throws IOException {    super(out, stats, startPosition);
  }
DFSOutputStream 的层次结构为:


在HDFS之上将数据压缩好后,再u存储到HDFS 2、在HDFS内部支持数据压缩,这里又可zhuan以分为几种方法: 2.1、压缩工shu作在DataNode上完成,这里又分两种方法:

 2.1.1、数据接收完后,再压缩 这个方法对HDFS的改动最小,但效果最低

小磊子
7楼 · 2020-12-28 09:26

1.构建环境

    去除编译文件share目录下所有xxxsources.jar和xxx tests.jar,将剩下的jar copy到eclispe中

2.配置hadoop环境变量

     注:测试hadoop-version报错,jdk为默认路径下,需修改hadoop-env.cmd下

           set JAVA_HOME=C:\PROGRA~1\Java\jdk1.8.0_201

3.代码操作hdfs

package com.zbb.hdfs;


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

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.fs.permission.FsPermission;
import org.junit.Test;

//hdfs客户端连接
public class HdfsClient {
    //上传系统文件
    public static void main(String[] args) throws IOException {
        //获取客户端
        Configuration conf = new Configuration();
        //设置客户端连接信息
        URI uri ;
        FileSystem fileSystem = null ;
        try {
            uri = new URI("hdfs://hadoop102:9000");
            fileSystem = FileSystem.get(uri, conf, "zbb");
            Path path1 = new Path("e://hello.txt");
            Path path2 = new Path("/hello5.txt");
            fileSystem.copyFromLocalFile(path1, path2);
            System.out.println("上传成功!");
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
                fileSystem.close();
                
        }        
    }
    @Test
    public void getConfiguration() throws IOException, InterruptedException, URISyntaxException {
        Configuration conf = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop102:9000"),conf,"zbb");
        System.out.println(fileSystem.toString());
    }
    @Test
    public void putFileToHdfs() throws IOException, InterruptedException, URISyntaxException {
        //获取客户端
        Configuration conf = new Configuration();
        //conf.set("dfs.replication", "2");
        //设置客户端信息
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop102:9000"), conf, "zbb");
        //上传文件
        fileSystem.copyFromLocalFile(new Path("e://hello.txt"), new Path("/hello2.txt"));
        //关闭资源
        fileSystem.close();
        //总结:参数配置优先级  代码>项目classpath下自定义配置文件>客户端配置文件>默认配置
    }
    @Test
    public void getFileFromHdfs() throws IOException, InterruptedException, URISyntaxException {
        //获取客户端信息
        Configuration conf = new Configuration();
        //设置客户端信息
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop102:9000"), conf, "zbb");
        //下载文件到本地
        fileSystem.copyToLocalFile(false, new Path("/hello.txt"), new Path("e://hello1.txt"), true);
        //关闭资源
        fileSystem.close();
        System.out.println("下载成功!");
    }
    @Test
    public void mkdirs() throws IOException, InterruptedException, URISyntaxException {
        Configuration conf = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop102:9000"), conf, "zbb");
        //创建目录
        fileSystem.mkdirs(new Path("/user/zbb/ouput/test"));
        fileSystem.close();
    }
    @Test
    public void del() throws IOException, InterruptedException, URISyntaxException {
        Configuration conf = new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop102:9000"), conf, "zbb");
        fileSystem.delete(new Path("/user/zbb/ouput"), true);
        fileSystem.close();
        
    }
    @Test
    public void rename() throws IOException, InterruptedException, URISyntaxException {
        Configuration configuration=new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "zbb");
        fileSystem.rename(new Path("/hello.txt"), new Path("/hello3.txt"));
        fileSystem.close();
    }
    @Test
    public void testListFiles() throws IOException, InterruptedException, URISyntaxException {
        Configuration configuration=new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "zbb");
        //查看所有目录
        RemoteIterator listFiles = fileSystem.listFiles(new Path("/"), true);
        while(listFiles.hasNext()) {
            LocatedFileStatus status= listFiles.next();
            //文件名
            String name = status.getPath().getName();
            System.out.println(name);
            //组名
            String group = status.getGroup();
            System.out.println(group);
            //权限
            FsPermission permission = status.getPermission();
            System.out.println(permission);
            //获取块的所有信息
            BlockLocation[] blockLocations = status.getBlockLocations();
            for (BlockLocation blockLocation : blockLocations) {
                String[] hosts = blockLocation.getHosts();
                for (String host : hosts) {
                    System.out.println(host);
                }
                
            }
            System.out.println("--------------------------------------------------");
        }
    }
    @Test
    public void testListStatus() throws IOException, InterruptedException, URISyntaxException {
        Configuration configuration=new Configuration();
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "zbb");
        FileStatus[] listStatus = fileSystem.listStatus(new Path("/"));
        for (FileStatus fileStatus : listStatus) {
            if(fileStatus.isFile()) {
                System.out.println("f---"+fileStatus.getPath().getName());
            }else {
                System.out.println("d---"+fileStatus.getPath().getName());
            }
        }
        fileSystem.close();
    }

}


魏魏姐
8楼 · 2020-12-28 10:31

hadoop1.0的大致原理 文件写入: 1、Client将文件切分 2、然后Client与Namenode交互,获取datanode的文件、地址信息 3、然后Client根据这些信息将文件写入到datanode,写到datanode之后,datanode会把自身地址和文件信息反馈给Namenode;

三岁奶猫
9楼 · 2020-12-28 11:12

在HDFS之上将数据压缩好后,再存储到HDFS 2、在HDFS部支持数据dao压缩,这里又可以分为几种方法:

2.1、压缩工容作在DataNode上完成,这里又分两种方法: 

2.1.1、数据接收完后,再压缩 这个方法对HDFS的改动最小,但效果最低


相关问题推荐

  • 什么是大数据时代?2021-01-13 21:23
    回答 100

    大数据(big data)一词越来越多地被提及,人们用它来描述和定义信息爆炸时代产生的海量数据,而这个海量数据的时代则被称为大数据时代。随着云时代的来临,大数据(Big data)也吸引了越来越多的关注。大数据(Big data)通常用来形容一个公司创造的大量非结...

  • 回答 84

    Java和大数据的关系:Java是计算机的一门编程语言;可以用来做很多工作,大数据开发属于其中一种;大数据属于互联网方向,就像现在建立在大数据基础上的AI方向一样,他两不是一个同类,但是属于包含和被包含的关系;Java可以用来做大数据工作,大数据开发或者...

  • 回答 52
    已采纳

    学完大数据可以从事很多工作,比如说:hadoop 研发工程师、大数据研发工程师、大数据分析工程师、数据库工程师、hadoop运维工程师、大数据运维工程师、java大数据工程师、spark工程师等等都是我们可以从事的工作岗位!不同的岗位,所具备的技术知识也是不一样...

  • 回答 29

    简言之,大数据是指大数据集,这些数据集经过计算分析可以用于揭示某个方面相关的模式和趋势。大数据技术的战略意义不在于掌握庞大的数据信息,而在于对这些含有意义的数据进行专业化处理。大数据的特点:数据量大、数据种类多、 要求实时性强、数据所蕴藏的...

  • 回答 14

    tail -f的时候,发现一个奇怪的现象,首先 我在一个窗口中 tail -f test.txt 然后在另一个窗口中用vim编辑这个文件,增加了几行字符,并保存,这个时候发现第一个窗口中并没有变化,没有将最新的内容显示出来。tail -F,重复上面的实验过程, 发现这次有变化了...

  • 回答 18

    您好针对您的问题,做出以下回答,希望有所帮助!1、大数据行业还是有非常大的人才需求的,对于就业也有不同的岗位可选,比如大数据工程师,大数据运维,大数据架构师,大数据分析师等等,就业难就难在能否找到适合的工作,能否与你的能力和就业预期匹配。2、...

  • 回答 17

    最小的基本单位是Byte应该没多少人不知道吧,下面先按顺序给出所有单位:Byte、KB、MB、GB、TB、PB、EB、ZB、YB、DB、NB,按照进率1024(2的十次方)计算:1Byte = 8 Bit1 KB = 1,024 Bytes 1 MB = 1,024 KB = 1,048,576 Bytes 1 GB = 1,024 MB = 1,048,576...

  • 回答 33

    大数据的定义。大数据,又称巨量资料,指的是所涉及的数据资料量规模巨大到无法通过人脑甚至主流软件工具,在合理时间内达到撷取、管理、处理、并整理成为帮助企业经营决策更积极目的的资讯。大数据是对大量、动态、能持续的数据,通过运用新系统、新工具、新...

  • 回答 5

    MySQL是一种关系型数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。MySQL的版本:针对不同的用户,MySQL分为两种不同的版本:MySQL Community Server社区版本,免费,但是Mysql不提供...

  • mysql安装步骤mysql 2022-05-07 18:01
    回答 2

    mysql安装需要先使用yum安装mysql数据库的软件包 ;然后启动数据库服务并运行mysql_secure_installation去除安全隐患,最后登录数据库,便可完成安装

  • 回答 5

    1.查看所有数据库showdatabases;2.查看当前使用的数据库selectdatabase();3.查看数据库使用端口showvariableslike'port';4.查看数据库编码showvariableslike‘%char%’;character_set_client 为客户端编码方式; character_set_connection 为建立连接...

  • 回答 5

    CREATE TABLE IF NOT EXISTS `runoob_tbl`(    `runoob_id` INT UNSIGNED AUTO_INCREMENT,    `runoob_title` VARCHAR(100) NOT NULL,    `runoob_author` VARCHAR(40) NOT NULL,    `submission_date` DATE,    PRI...

  • 回答 9

    学习多久,我觉得看你基础情况。1、如果原来什么语言也没有学过,也没有基础,那我觉得最基础的要先选择一种语言来学习,是VB,C..,pascal,看个人的喜好,一般情况下,选择C语言来学习。2、如果是有过语言的学习,我看应该一个星期差不多,因为语言的理念互通...

  • 回答 7

    添加语句 INSERT插入语句:INSERT INTO 表名 VALUES (‘xx’,‘xx’)不指定插入的列INSERT INTO table_name VALUES (值1, 值2,…)指定插入的列INSERT INTO table_name (列1, 列2,…) VALUES (值1, 值2,…)查询插入语句: INSERT INTO 插入表 SELECT * FROM 查...

  • 回答 5

    看你什么岗位吧。如果是后端,只会CRUD。应该是可以找到实习的,不过公司应该不会太好。如果是数据库开发岗位,那这应该是不会找到的。

  • 回答 7

    查找数据列 SELECT column1, column2, … FROM table_name; SELECT column_name(s) FROM table_name 

没有解决我的问题,去提问