更新hadoop fs 命令实现:

[ss@db csv]$ hadoop fs -count /my_rc/my_hive_db/*
18/01/14 15:40:19 INFO hdfs.PeerCache: SocketCache disabled.
3 2 0 /my_rc/my_hive_db/.hive-staging_hive_2017-08-19_16-52-39_153_7217997288202811839-170149
2 0 0 /my_rc/my_hive_db/.hive-staging_hive_2018-01-03_15-23-10_240_5147839610865108930-52517
1 0 0 /my_rc/my_hive_db/BusinessGtUser
4 1 321008 /my_rc/my_hive_db/ZJ2_SenseSta
1 1 143 /my_rc/my_hive_db/anthgain
1 1 27228 /my_rc/my_hive_db/anthgainpoint
1 1 70 /my_rc/my_hive_db/antvgain
1 1 27429 /my_rc/my_hive_db/antvgainpoint

通过hadoop fs -du 或者 hadoop fs -count只能统计指定的某个hdfs路径(hive表目录)的总文件个数及文件的大小,但是通过hadoop命令没有办法实现批量处理hive中多个表一次进行统计,如果一次性统计多个hive表目录的文件个数、文件总大小只能通过java程序使用hadoop api实现。

package com.my.hdfsopt;

import java.io.FileNotFoundException;
import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path; public class HdfsPathMonitor {
// submit shell
/*
* main类的路径不需要指定,否则会被认为是参数传递进入。
* yarn jar /app/m_user1/service/Hangzhou_HdfsFileMananger.jar /hive_tenant_account/hivedbname/
*/
public static void main(String[] args) throws Exception {
System.out.println("the args is " + String.join(",", args));
String dirPath = args[0]; Configuration conf = new Configuration();
/*
* <property> <name>fs.defaultFS</name> <value>hdfs://mycluster</value>
* </property>
*/
conf.set("fs.defaultFS", "hdfs://mycluster"); FileSystem fileSystem = FileSystem.get(conf);
Path path = new Path(dirPath); // 获取文件列表
FileStatus[] files = fileSystem.listStatus(path);
if (files == null || files.length == 0) {
throw new FileNotFoundException("Cannot access " + dirPath + ": No such file or directory.");
} System.out.println("dirpath \t total file size \t total file count");
for (int i = 0; i < files.length; i++) {
String pathStr = files[i].getPath().toString(); FileSystem fs = files[i].getPath().getFileSystem(conf);
long totalSize = fs.getContentSummary(files[i].getPath()).getLength();
long totalFileCount = listAll(conf, files[i].getPath());
fs.close(); System.out.println(("".equals(pathStr) ? "." : pathStr) + "\t" + totalSize + "\t" + totalFileCount);
}
} /**
* @Title: listAll @Description: 列出目录下所有文件 @return void 返回类型 @throws
*/
public static Long listAll(Configuration conf, Path path) throws IOException {
long totalFileCount = 0;
FileSystem fs = FileSystem.get(conf); if (fs.exists(path)) {
FileStatus[] stats = fs.listStatus(path);
for (int i = 0; i < stats.length; ++i) {
if (!stats[i].isDir()) {
// regular file
// System.out.println(stats[i].getPath().toString());
totalFileCount++;
} else {
// dir
// System.out.println(stats[i].getPath().toString());
totalFileCount += listAll(conf, stats[i].getPath());
}
}
}
fs.close(); return totalFileCount;
} }

执行命令:

yarn jar /app/m_user1/tommyduan_service/Hangzhou_HdfsFileMananger.jar /hive_tenant_account/hivedbname/

执行结果:

通过java api统计hive库下的所有表的文件个数、文件大小的更多相关文章

  1. mysql下批量清空某个库下的所有表(库不要删除,保留空库)

    总所周知,mysql下要想删除某个库下的某张表,只需要切换到该库下,执行语句"drop table tablename"即可删除!但若是该库下有成百上千张表,要是再这样一次次执行d ...

  2. mysql5.7 mysql库下面的user表没有password字段无法修改密码

    如题所述,mysql5.7  mysql库下面的user表没有password字段无法修改密码, 5.7版本已经不再使用password来作为密码的字段了  而改成了authentication_st ...

  3. Java API操作HA方式下的Hadoop

    通过java api连接Hadoop集群时,如果集群支持HA方式,那么可以通过如下方式设置来自动切换到活动的master节点上.其中,ClusterName 是可以任意指定的,跟集群配置无关,dfs. ...

  4. MySQL数据库中统计一个库中的所有表的行数?

    今天公司两个远端的数据库主从同步有点问题,查看下wordpress库下所有表的表的条目? mysql> use information_schema;Database changedmysql& ...

  5. Centos下查看当前目录大小及文件个数

    查看目录及其包含的文件的大小 du -ch directory 查看当前目录下文件的个数 ls -l | grep "^-" | wc -l 查看当前目录下以.jpg为后缀文件的个 ...

  6. 统计hive库表在具体下所有分区大小

    1 查询具体表分区大小,以字节展示 hadoop fs -du /user/hive/warehouse/treasury.db/dm_user_excercise > dm_user_exce ...

  7. 使用hive客户端java api读写hive集群上的信息

    上文介绍了hdfs集群信息的读取方式,本文说hive 1.先解决依赖 <properties> <hive.version>1.2.1</hive.version> ...

  8. Spark:java api读取hdfs目录下多个文件

    需求: 由于一个大文件,在spark中加载性能比较差.于是把一个大文件拆分为多个小文件后上传到hdfs,然而在spark2.2下如何加载某个目录下多个文件呢? public class SparkJo ...

  9. windows上使用metastore client java api链接hive metastore问题

    https://github.com/sdravida/hadoop2.6_Win_x64 下载winutils.exe 添加到path中

随机推荐

  1. 深入理解Java虚拟机(第2版) 笔记目录

    本篇为读深入理解Java虚拟机(第2版)一书的笔记目录. Java 运行期数据区 Java 垃圾回收算法 Java 内存分配策略 Java 类文件结构 Java 加载.链接.初始化 Java 类加载器

  2. Python 中 mySQL 中的语句

    class DeleteInventorybusiness(BaseBusiness): def DeleteInventory(self,Delete_goodsID): DeleteInvento ...

  3. Maven-02: 依赖

    其实一个依赖声明可以包含如下的一些元素: groupId,artifactId,version:依赖的基本坐标. type:依赖的类型,对应于项目坐标定义的packaging.大多数情况下,该元素不必 ...

  4. MySQL 中添加列、修改列以及删除列

    ALTER TABLE:添加,修改,删除表的列,约束等表的定义. 查看列:desc 表名; 修改表名:alter table t_book rename to bbb; 添加列:); 删除列:alte ...

  5. GDB 调试多线程多进程

    GDB是linux下的调试利器,在c/c++程序开发过程中必不可少的.这里总结一下多进程和多线程的调试方法和技巧. 多进程的调试: 如下示例 #include <sys/mman.h> # ...

  6. linux --> 计算机是如何启动的?

    计算机是如何启动的? 零.boot的含义 "启动"用英语怎么说?回答是boot.boot原来的意思是靴子,"启动"与靴子有什么关系呢? 原来,这里的boot是b ...

  7. kill-mysql-sleep.sh

    #!/bin/bash #while : #do n=`/usr/bin/mysqladmin -uroot -pXXXXX processlist | grep -i sleep | wc -l` ...

  8. 深入理解C++ new/delete, new []/delete[]动态内存管理

    在C语言中,我们写程序时,总是会有动态开辟内存的需求,每到这个时候我们就会想到用malloc/free 去从堆里面动态申请出来一段内存给我们用.但对这一块申请出来的内存,往往还需要我们对它进行稍许的“ ...

  9. java基础之类与对象

    [类 & 对象] 1.类:具有一系列相同属性(特征)和方法(行为)的个体的集合,称为类. 类是一个抽象的概念,只能说类具有哪些属性,而不能直接对属性进行赋值. 例如:人类有身高的属性,但是不能 ...

  10. C语言-学生博客汇总

    一.学生个人博客汇总 五班 学号 姓名 博客地址 4079 马天琦 http://www.cnblogs.com/simalang/ 4080 马宇欣 http://www.cnblogs.com/m ...