加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码门户网 (https://www.92codes.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 网站设计 > 教程 > 正文

Hadoop大数据通用处理平台

发布时间:2019-03-17 16:14:13 所属栏目:教程 来源:Java的小本家
导读:副标题#e# Hadoop是一款开源的大数据通用处理平台,其提供了分布式存储和分布式离线计算,,适合大规模数据、流式数据(写一次,读多次),不适合低延时的访问、大量的小文件以及频繁修改的文件。 *Hadoop由HDFS、YARN、MapReduce组成。 如果想学习Java工程化

启动YARN后,将会启动ResourceManager以及NodeManager进程,可以通过jps命令进行查看。

当YARN启动完毕后,可以访问http://localhost:8088进入YARN的可视化管理界面,可以在此页面中查看任务的执行情况以及资源的分配。

Hadoop大数据通用处理平台

3.5 使用Shell命令操作HDFS

HDFS中的文件系统与Linux类似,由/代表根目录。

如果想学习Java工程化、高性能及分布式、深入浅出。微服务、Spring,MyBatis,Netty源码分析的朋友可以加我的Java高级交流:854630135,群里有阿里大牛直播讲解技术,以及Java大型互联网技术的视频免费分享给大家。

  1. hadoop fs -cat <src>:显示文件中的内容。 
  2. hadoop fs -copyFromLocal <localsrc> <dst>:将本地中的文件上传到HDFS。 
  3. hadoop fs -copyToLocal <src> <localdst>:将HDFS中的文件下载到本地。 
  4. hadoop fs -count <path>:查询指定路径下文件的个数。 
  5. hadoop fs -cp <src> <dst>:在HDFS内对文件进行复制。 
  6. hadoop fs -get <src> <localdst>:将HDFS中的文件下载到本地。 
  7. hadoop fs -ls <path>:显示指定目录下的内容。 
  8. hadoop fs -mkdir <path>:创建目录。 
  9. hadoop fs -moveFromLocal <localsrc> <dst>:将本地中的文件剪切到HDFS中。 
  10. hadoop fs -moveToLocal <src> <localdst> :将HDFS中的文件剪切到本地中。 
  11. hadoop fs -mv <src> <dst> :在HDFS内对文件进行移动。 
  12. hadoop fs -put <localsrc> <dst>:将本地中的文件上传到HDFS。 
  13. hadoop fs -rm <src>:删除HDFS中的文件。 

3.6 JAVA中操作HDFS

  1. /** 
  2.  * @Auther: ZHUANGHAOTANG 
  3.  * @Date: 2018/11/6 11:49 
  4.  * @Description: 
  5.  */ 
  6. public class HDFSUtils { 
  7.  private static Logger logger = LoggerFactory.getLogger(HDFSUtils.class); 
  8.  /** 
  9.  * NameNode URL 
  10.  */ 
  11.  private static final String NAMENODE_URL = "192.168.1.80:8020"; 
  12.  /** 
  13.  * HDFS文件系统连接对象 
  14.  */ 
  15.  private static FileSystem fs = null; 
  16.  static { 
  17.  Configuration conf = new Configuration(); 
  18.  try { 
  19.  fs = FileSystem.get(URI.create(NAMENODE_URL), conf); 
  20.  } catch (IOException e) { 
  21.  logger.info("初始化HDFS连接失败:{}", e); 
  22.  } 
  23.  } 
  24.  /** 
  25.  * 创建目录 
  26.  */ 
  27.  public static void mkdir(String dir) throws Exception { 
  28.  dir = NAMENODE_URL + dir; 
  29.  if (!fs.exists(new Path(dir))) { 
  30.  fs.mkdirs(new Path(dir)); 
  31.  } 
  32.  } 
  33.  /** 
  34.  * 删除目录或文件 
  35.  */ 
  36.  public static void delete(String dir) throws Exception { 
  37.  dir = NAMENODE_URL + dir; 
  38.  fs.delete(new Path(dir), true); 
  39.  } 
  40.  /** 
  41.  * 遍历指定路径下的目录和文件 
  42.  */ 
  43.  public static List<String> listAll(String dir) throws Exception { 
  44.  List<String> names = new ArrayList<>(); 
  45.  dir = NAMENODE_URL + dir; 
  46.  FileStatus[] files = fs.listStatus(new Path(dir)); 
  47.  for (FileStatus file : files) { 
  48.  if (file.isFile()) { //文件 
  49.  names.add(file.getPath().toString()); 
  50.  } else if (file.isDirectory()) { //目录 
  51.  names.add(file.getPath().toString()); 
  52.  } else if (file.isSymlink()) { //软或硬链接 
  53.  names.add(file.getPath().toString()); 
  54.  } 
  55.  } 
  56.  return names; 
  57.  } 
  58.  /** 
  59.  * 上传当前服务器的文件到HDFS中 
  60.  */ 
  61.  public static void uploadLocalFileToHDFS(String localFile, String hdfsFile) throws Exception { 
  62.  hdfsFile = NAMENODE_URL + hdfsFile; 
  63.  Path src = new Path(localFile); 
  64.  Path dst = new Path(hdfsFile); 
  65.  fs.copyFromLocalFile(src, dst); 
  66.  } 
  67.  /** 
  68.  * 通过流上传文件 
  69.  */ 
  70.  public static void uploadFile(String hdfsPath, InputStream inputStream) throws Exception { 
  71.  hdfsPath = NAMENODE_URL + hdfsPath; 
  72.  FSDataOutputStream os = fs.create(new Path(hdfsPath)); 
  73.  BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); 
  74.  byte[] data = new byte[1024]; 
  75.  int len; 
  76.  while ((len = bufferedInputStream.read(data)) != -1) { 
  77.  if (len == data.length) { 
  78.  os.write(data); 
  79.  } else { //最后一次读取 
  80.  byte[] lastData = new byte[len]; 
  81.  System.arraycopy(data, 0, lastData, 0, len); 
  82.  os.write(lastData); 
  83.  } 
  84.  } 
  85.  inputStream.close(); 
  86.  bufferedInputStream.close(); 
  87.  os.close(); 
  88.  } 
  89.  /** 
  90.  * 从HDFS中下载文件 
  91.  */ 
  92.  public static byte[] readFile(String hdfsFile) throws Exception { 
  93.  hdfsFile = NAMENODE_URL + hdfsFile; 
  94.  Path path = new Path(hdfsFile); 
  95.  if (fs.exists(path)) { 
  96.  FSDataInputStream is = fs.open(path); 
  97.  FileStatus stat = fs.getFileStatus(path); 
  98.  byte[] data = new byte[(int) stat.getLen()]; 
  99.  is.readFully(0, data); 
  100.  is.close(); 
  101.  return data; 
  102.  } else { 
  103.  throw new Exception("File Not Found In HDFS"); 
  104.  } 
  105.  } 

3.7 执行一个MapReduce任务

(编辑:源码门户网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读