根据不同的操作系统读取配置文件/java读取属性文件代码

package cn.com.css.common.util;

/**
 * @brief OSEnum.java 操作系统的枚举
 * @attention
 * @author 涂作权
 * @date 2014年4月3日
 * @note begin modify by null
 */
public enum EOSPlatForm {
 Any("any"),
 Linux("Linux"),
 Mac_OS("Mac OS"),
 Mac_OS_X("Mac OS X"),
 Windows("Windows"),
 OS2("OS/2"),
 Solaris("Solaris"),
 SunOS("SunOS"),
 MPEiX("MPE/iX"),
 HP_UX("HP-UX"),
 AIX("AIX"),
 OS390("OS/390"),
 FreeBSD("FreeBSD"),
 Irix("Irix"),
 Digital_Unix("Digital Unix"),
 NetWare_411("NetWare"),
 OSF1("OSF1"),
 OpenVMS("OpenVMS"),
 Others("Others");

 /** 描述信息 **/
 private String description;

 /**
  * @param desc 描述信息
  */
 EOSPlatForm(String desc) {
  this.description = desc;
 }

 public String getDescription() {
  return description;
 }

 public void setDescription(String description) {
  this.description = description;
 }
}

 

package cn.com.css.common.util;

/**
 * @brief OSInfo.java 通过这个类获得操作信息信息
 * @attention
 * @author 涂作权
 * @date 2014年4月3日
 * @note begin modify by null
 */
public class OSInfo {
 /** 操作系统名称 **/
 private static String OS_NAME = System.getProperty("os.name").toLowerCase();
 private static OSInfo osInfoInstance = new OSInfo();
 private EOSPlatForm osPlatForm;

 private OSInfo() {
 }

 /**
  * \brief 判断是否是Linux操作系统
  *
  * @return
  * @attention
  * @author 涂作权
  * @date 2014年4月3日
  * @note begin modify by null
  */
 public static boolean isLinux() {
  return OS_NAME.indexOf("linux") >= 0;
 }

 /**
  * \brief 判断是否是MacOS操作系统
  *
  * @return
  * @attention
  * @author 涂作权
  * @date 2014年4月3日
  * @note begin modify by null
  */
 public static boolean isMacOS() {
  return OS_NAME.indexOf("mac") >= 0 && OS_NAME.indexOf("os") > 0
    && OS_NAME.indexOf("x") < 0;
 }

 /**
  * \brief 判断是否是MacOSX操作系统
  *
  * @return
  * @attention
  * @author 涂作权
  * @date 2014年4月3日
  * @note begin modify by null
  */
 public static boolean isMacOSX() {
  return OS_NAME.indexOf("mac") >= 0 && OS_NAME.indexOf("os") > 0
    && OS_NAME.indexOf("x") > 0;
 }

 /**
  * \brief 判断是否是windows操作系统
  *
  * @return
  * @attention
  * @author 涂作权
  * @date 2014年4月3日
  * @note begin modify by null
  */
 public static boolean isWindows() {
  return OS_NAME.indexOf("windows") >= 0;
 }

 /**
  * \brief 判断是否是OS2操作系统
  *
  * @return
  * @attention 方法的使用注意事项
  * @author Administrator
  * @date 2014-4-3
  * @note begin modify by 修改人 修改时间 修改内容摘要说明
  */
 public static boolean isOS2() {
  return OS_NAME.indexOf("os/2") >= 0;
 }

 public static boolean isSolaris() {
  return OS_NAME.indexOf("solaris") >= 0;
 }

 public static boolean isSunOS() {
  return OS_NAME.indexOf("sunos") >= 0;
 }

 public static boolean isMPEiX() {
  return OS_NAME.indexOf("mpe/ix") >= 0;
 }

 public static boolean isHPUX() {
  return OS_NAME.indexOf("hp-ux") >= 0;
 }

 public static boolean isAix() {
  return OS_NAME.indexOf("aix") >= 0;
 }

 public static boolean isOS390() {
  return OS_NAME.indexOf("os/390") >= 0;
 }

 public static boolean isFreeBSD() {
  return OS_NAME.indexOf("freebsd") >= 0;
 }

 public static boolean isIrix() {
  return OS_NAME.indexOf("irix") >= 0;
 }

 public static boolean isDigitalUnix() {
  return OS_NAME.indexOf("digital") >= 0 && OS_NAME.indexOf("unix") > 0;
 }

 public static boolean isNetWare() {
  return OS_NAME.indexOf("netware") >= 0;
 }

 public static boolean isOSF1() {
  return OS_NAME.indexOf("osf1") >= 0;
 }

 public static boolean isOpenVMS() {
  return OS_NAME.indexOf("openvms") >= 0;
 }

 /**
  * \brief 获得操作系统的名称
  *
  * @return
  * @attention
  * @author 涂作权
  * @date 2014年4月3日
  * @note begin modify by null
  */
 public static EOSPlatForm getOSName() {
  if (isAix()) {
   osInfoInstance.osPlatForm = EOSPlatForm.AIX;
  } else if (isDigitalUnix()) {
   osInfoInstance.osPlatForm = EOSPlatForm.Digital_Unix;
  } else if (isFreeBSD()) {
   osInfoInstance.osPlatForm = EOSPlatForm.FreeBSD;
  } else if (isHPUX()) {
   osInfoInstance.osPlatForm = EOSPlatForm.HP_UX;
  } else if (isIrix()) {
   osInfoInstance.osPlatForm = EOSPlatForm.Irix;
  } else if (isLinux()) {
   osInfoInstance.osPlatForm = EOSPlatForm.Linux;
  } else if (isMacOS()) {
   osInfoInstance.osPlatForm = EOSPlatForm.Mac_OS;
  } else if (isMacOSX()) {
   osInfoInstance.osPlatForm = EOSPlatForm.Mac_OS_X;
  } else if (isMPEiX()) {
   osInfoInstance.osPlatForm = EOSPlatForm.MPEiX;
  } else if (isNetWare()) {
   osInfoInstance.osPlatForm = EOSPlatForm.NetWare_411;
  } else if (isOpenVMS()) {
   osInfoInstance.osPlatForm = EOSPlatForm.OpenVMS;
  } else if (isOS2()) {
   osInfoInstance.osPlatForm = EOSPlatForm.OS2;
  } else if (isOS390()) {
   osInfoInstance.osPlatForm = EOSPlatForm.OS390;
  } else if (isOSF1()) {
   osInfoInstance.osPlatForm = EOSPlatForm.OSF1;
  } else if (isSolaris()) {
   osInfoInstance.osPlatForm = EOSPlatForm.Solaris;
  } else if (isSunOS()) {
   osInfoInstance.osPlatForm = EOSPlatForm.SunOS;
  } else if (isWindows()) {
   osInfoInstance.osPlatForm = EOSPlatForm.Windows;
  } else {
   osInfoInstance.osPlatForm = EOSPlatForm.Others;
  }
  return osInfoInstance.osPlatForm;
 }

// public static void main(String[] args) {
//  System.out.println(OSInfo.getOSName());
//  System.out.println(osInfoInstance.osPlatForm.getDescription());
//  System.out.println(System.getProperty("os.name"));
//  System.out.println(System.getProperty("os.version"));
//  System.out.println(System.getProperty("os.arch"));
// }
}

 

 

package cn.com.css.misps.graph.util;

import java.io.File;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import cn.com.css.common.util.OSInfo;

/**
 * @brief StoragePathUtils.java 图形产品存储相关的类
 * @attention 要注意的是:图形产品的存储路径要兼容Linux的。
 * @author 涂作权
 * @date 2013-9-23
 * @note begin modify by null
 */
public final class ProductsStorageUtils {
 
 public static Calendar calendar;

 // 图形产品对应的绝对路径
 public static String graphAbsolutePath;
 // 图形产品中对应的虚拟路径
 public static String graphVirtualPath;
 // 文字产品对应的绝对路径
 public static String wordAbsolutePath;
 // 文字产品对应的虚拟路径
 public static String wordVirtualPath;
 // micaps磁盘挂接过来的源文件的路径
 public static String micapsAbsolutePath;
 // micaps虚拟路径
 public static String micapsVirtualPath;

 // 图形产品今天的文件存储路径
 public static String graphTodayStoragePath;
 // 图形产品明天的文件存储路径
 public static String graphTomorrowStoragePath;
 // 图形产品文件存储的相对路径
 public static String graphRelativeStoragePath;

 // 文字产品今天的文件存储路径
 public static String wordTodayStoragePath;
 // 文字产品明天的文件存储路径
 public static String wordTomorrowStoragePath;
 // 文字产品文件存储的相对路径
 public static String wordRelativeStoragePath;
 // 认证文件存放的位置
 public static String authenticationPath;
 // 认证文件存放的相对路径
 public static String authenticationTodayPath;
 // 认证文件第二天要存放的位置
 public static String authenticationTomorrowPath;
 
 /** graphTemp文件存储的临时目录存储位置  **/
 public static String graphTempAbsolutePath;
 /** graphTemp对应的虚拟目录 **/
 public static String graphTempVirtualPath;
 /** 指定数据源时间存储的位置  **/
 public static String graphTempTodayStoragePath;
 /** 指定数据源第二天存储的位置 **/
 public static String graphTempTomorrowStoragePath;
 /** 在临时目录里的相对路径**/
 public static String graphTempRelativeStoragePath;

 public ProductsStorageUtils() {
 }
 
 /**
  * \brief 编写此方法的目的是获得指定时间的这些相应数据。
  *
  * @param date
  * @return
  * @attention 如果不调用这个方法,则表示当天对应的这些数据
  * @author 涂作权
  * @date 2014-5-23
  * @note begin modify by 修改人 修改时间 修改内容摘要说明
  */
 @SuppressWarnings("static-access")
 public static ProductsStorageUtils changeCalendar(Date date) {
  ProductsStorageUtils ps = new ProductsStorageUtils();
  ps.calendar.setTime(date);
  return ps;
 }

 /**
  * 静态代码块
  */
 static {
  try {
   // 使用默认时区和语言环境获得一个日历
   calendar = Calendar.getInstance();
   // 今年
   int year = calendar.get(Calendar.YEAR);
   // 当月
   int month = calendar.get(Calendar.MONTH) + 1;
   // 当天
   int day = calendar.get(Calendar.DAY_OF_MONTH);
   // 明天
   int tomorrow = day + 1;

   InputStream in = null;

   // 判断操作系统类型
   switch (OSInfo.getOSName()) {
   case Windows:
    // 读取配置文件,通过类加载的方式读取属性文件
    in = ProductsStorageUtils.class.getClassLoader()
      .getResourceAsStream("windows_storagepath.properties");
    break;
   case Linux:
    in = ProductsStorageUtils.class.getClassLoader()
      .getResourceAsStream("Linux_storagepath.properties");
    break;
   default:
    break;
   }

   Properties prop = new Properties();
   prop.load(in);

   // 图形产品对应的绝对路径
   graphAbsolutePath = prop.getProperty("productAbsolutePath")
     + File.separator + "graph";
   // 图形产品中对应的虚拟路径
   graphVirtualPath = prop.getProperty("graphVirtualPath");
   // 文字产品对应的绝对路径
   wordAbsolutePath = prop.getProperty("productAbsolutePath")
     + File.separator + "word";
   // 文字产品对应的虚拟路径
   wordVirtualPath = prop.getProperty("wordVirtualPath");
   // micaps磁盘挂接过来的源文件的路径
   micapsAbsolutePath = prop.getProperty("micapsAbsolutePath");
   // micaps虚拟路径
   micapsVirtualPath = prop.getProperty("micapsVirtualPath");
   // 图片临时目录存储位置
   graphTempAbsolutePath = prop.getProperty("graphTempAbsolutePath");
   graphTempVirtualPath = prop.getProperty("graphTempVirtualPath");

   // 获取图形产品文件存储的根路径
   graphTodayStoragePath = graphAbsolutePath + File.separator + year
     + File.separator + ((month > 9) ? month : "0" + month)
     + File.separator + ((day > 9) ? day : "0" + day);
   // 明天图形产品文件的存储路径
   graphTomorrowStoragePath = graphAbsolutePath + File.separator
     + year + File.separator
     + ((month > 9) ? month : "0" + month) + File.separator
     + ((tomorrow > 9) ? tomorrow : "0" + tomorrow);
   // 图形产品文件存储的相对路径
   graphRelativeStoragePath = "/" + year + "/"
     + ((month > 9) ? month : "0" + month) + "/"
     + ((day > 9) ? day : "0" + day);
   
   // 获取临时图形产品文件存储的根路径
   graphTempTodayStoragePath = graphTempAbsolutePath + File.separator + year
     + File.separator + ((month > 9) ? month : "0" + month)
     + File.separator + ((day > 9) ? day : "0" + day);
   // 明天图形产品文件的存储路径
   graphTempTomorrowStoragePath = graphTempAbsolutePath + File.separator
     + year + File.separator
     + ((month > 9) ? month : "0" + month) + File.separator
     + ((tomorrow > 9) ? tomorrow : "0" + tomorrow);
   // 图形产品文件存储的相对路径
   graphTempRelativeStoragePath = "/" + year + "/"
     + ((month > 9) ? month : "0" + month) + "/"
     + ((day > 9) ? day : "0" + day);

   // 获取文字产品文件存储的根路径
   wordTodayStoragePath = wordAbsolutePath + File.separator + year
     + File.separator + ((month > 9) ? month : "0" + month)
     + File.separator + ((day > 9) ? day : "0" + day);
   // 明天文字产品文件的存储路径
   wordTomorrowStoragePath = wordAbsolutePath + File.separator + year
     + File.separator + ((month > 9) ? month : "0" + month)
     + File.separator
     + ((tomorrow > 9) ? tomorrow : "0" + tomorrow);
   // 文字产品文件相对路径
   wordRelativeStoragePath = "/" + year + "/"
     + ((month > 9) ? month : "0" + month) + "/"
     + ((day > 9) ? day : "0" + day);

   // 认证文件存放的位置
   authenticationPath = prop.getProperty("authenticationPath");
   // 认证文件当天存放的位置
   authenticationTodayPath = authenticationPath + File.separator
     + year + File.separator
     + ((month > 9) ? month : "0" + month) + File.separator
     + ((day > 9) ? day : "0" + day);
   // 认证文件明天存放的位置
   authenticationTomorrowPath = authenticationPath + File.separator
     + year + File.separator
     + ((month > 9) ? month : "0" + month) + File.separator
     + ((tomorrow > 9) ? tomorrow : "0" + tomorrow);

   // 关闭流
   in.close();
   in = null;
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 /**
  * \brief 创建图形产品明天文件存储的文件目录
  *
  * @attention
  *
  * @author 涂作权
  * @date 2013-10-6
  * @note begin modify by 涂作权 2014-02-13
  */
 public static void createGraphTomorrowStorageFolder() {
  // 判断该文件夹是否存在,如果存在就不需要创建,如果不存在就创建
  File storageFolder = new File(graphTomorrowStoragePath);
  if (storageFolder.exists()) {
   return;
  } else {
   // 创建文件夹
   storageFolder.mkdirs();
   return;
  }
 }

 /**
  * \brief 创建指定数据源时间的图形产品明天文件存储的文件目录
  *
  * @param date:指定的数据源时间
  *
  * @attention
  *
  * @author 涂作权
  * @date 2014-5-23
  * @note begin modify by 涂作权
  */
 @SuppressWarnings("static-access")
 public static void createGraphTomorrowStorageFolder(Date date) {
  // 判断该文件夹是否存在,如果存在就不需要创建,如果不存在就创建
  File storageFolder = new File(changeCalendar(date).graphTomorrowStoragePath);
  if (storageFolder.exists()) {
   return;
  } else {
   // 创建文件夹
   storageFolder.mkdirs();
   return;
  }
 }

 /**
  * \brief 创建今天的文件存储路径
  *
  * @attention
  * @author 涂作权
  * @date 2014-5-23
  * @note begin modify by 涂作权
  */
 public static void createGraphTodayStorageFolder() {
  // 判断该文件夹是否存在,如果存在就不需要创建,如果不存在就创建
  File storageFolder = new File(graphTodayStoragePath);
  if (storageFolder.exists()) {
   return;
  } else {
   // 创建文件夹
   storageFolder.mkdirs();
   return;
  }
 }

 /**
  * \brief 创建指定的数据源时间的那天的文件存储路径
  *
  * @param date:指定的数据源时间
  * @attention
  * @author 涂作权
  * @date 2014-5-23
  * @note begin modify by 涂作权
  */
 @SuppressWarnings("static-access")
 public static void createGraphTodayStorageFolder(Date date) {
  // 判断该文件夹是否存在,如果存在就不需要创建,如果不存在就创建
  File storageFolder = new File(
    changeCalendar(date).graphTodayStoragePath);
  if (storageFolder.exists()) {
   return;
  } else {
   // 创建文件夹
   storageFolder.mkdirs();
   return;
  }
 }
 
 /**
  * \brief 创建图形产品明天文件存储的文件目录
  *
  * @attention
  *
  * @author 涂作权
  * @date 2013-10-6
  * @note begin modify by 涂作权 2014-02-13
  */
 public static void createGraphTempTomorrowStorageFolder() {
  // 判断该文件夹是否存在,如果存在就不需要创建,如果不存在就创建
  File storageFolder = new File(graphTempTomorrowStoragePath);
  if (storageFolder.exists()) {
   return;
  } else {
   // 创建文件夹
   storageFolder.mkdirs();
   return;
  }
 }
 
 /**
  * \brief 创建指定数据源时间的图形产品明天文件存储的文件目录
  *
  * @param date:指定的数据源时间
  *
  * @attention
  *
  * @author 涂作权
  * @date 2014-5-23
  * @note begin modify by 涂作权
  */
 @SuppressWarnings("static-access")
 public static void createGraphTempTomorrowStorageFolder(Date date) {
  // 判断该文件夹是否存在,如果存在就不需要创建,如果不存在就创建
  File storageFolder = new File(changeCalendar(date).graphTempTomorrowStoragePath);
  if (storageFolder.exists()) {
   return;
  } else {
   // 创建文件夹
   storageFolder.mkdirs();
   return;
  }
 }
 
 /**
  * \brief 创建今天的文件存储路径
  *
  * @attention
  * @author 涂作权
  * @date 2014-5-23
  * @note begin modify by 涂作权
  */
 public static void createGraphTempTodayStorageFolder() {
  // 判断该文件夹是否存在,如果存在就不需要创建,如果不存在就创建
  File storageFolder = new File(graphTempTodayStoragePath);
  if (storageFolder.exists()) {
   return;
  } else {
   // 创建文件夹
   storageFolder.mkdirs();
   return;
  }
 }
 
 /**
  * \brief 创建指定的数据源时间的那天的文件存储路径
  *
  * @param date:指定的数据源时间
  * @attention
  * @author 涂作权
  * @date 2014-5-23
  * @note begin modify by 涂作权
  */
 @SuppressWarnings("static-access")
 public static void createGraphTempTodayStorageFolder(Date date) {
  // 判断该文件夹是否存在,如果存在就不需要创建,如果不存在就创建
  File storageFolder = new File(changeCalendar(date).graphTempTodayStoragePath);
  if (storageFolder.exists()) {
   return;
  } else {
   // 创建文件夹
   storageFolder.mkdirs();
   return;
  }
 }

 /**
  * \brief 创建文字产品明天文件存储的文件目录
  *
  * @attention
  *
  * @author 涂作权
  * @date 2013-10-6
  * @note begin modify by 涂作权 2014-02-13
  */
 public static void createWordTomorrowStorageFolder() {
  // 判断该文件夹是否存在,如果存在就不需要创建,如果不存在就创建
  File storageFolder = new File(wordTomorrowStoragePath);
  if (storageFolder.exists()) {
   return;
  } else {
   // 创建文件夹
   storageFolder.mkdirs();
   return;
  }
 }

 /**
  * \brief 创建指定数据源时间的后一天的文件存储的文件目录
  *
  * @param date :指定的数据源时间
  *
  * @attention
  *
  * @author 涂作权
  * @date 2014-5-23
  * @note begin modify by 涂作权
  */
 @SuppressWarnings("static-access")
 public static void createWordTomorrowStorageFolder(Date date) {
  // 判断该文件夹是否存在,如果存在就不需要创建,如果不存在就创建
  File storageFolder = new File(
    changeCalendar(date).wordTomorrowStoragePath);
  if (storageFolder.exists()) {
   return;
  } else {
   // 创建文件夹
   storageFolder.mkdirs();
   return;
  }
 }

 /**
  * \brief 创建文字产品今天的文件存储路径
  *
  * @attention
  * @author 涂作权
  * @date 2013-10-6
  * @note begin modify by 涂作权 2014-02-13
  */
 public static void createWordTodayStorageFolder() {
  // 判断该文件夹是否存在,如果存在就不需要创建,如果不存在就创建
  File storageFolder = new File(wordTodayStoragePath);
  if (storageFolder.exists()) {
   return;
  } else {
   // 创建文件夹
   storageFolder.mkdirs();
   return;
  }
 }

 /**
  * \brief 创建指定数据源时间的文字产品所在的文件存储路径
  *
  * @attention
  * @author 涂作权
  * @date 2014-05-23
  * @note begin modify by 涂作权
  */
 @SuppressWarnings("static-access")
 public static void createWordTodayStorageFolder(Date date) {
  // 判断该文件夹是否存在,如果存在就不需要创建,如果不存在就创建
  File storageFolder = new File(changeCalendar(date).wordTodayStoragePath);
  if (storageFolder.exists()){
   return;
  } else {
   // 创建文件夹
   storageFolder.mkdirs();
   return;
  }
 }

 /**
  * \brief 创建认证文件当天存放文件的位置
  *
  * @attention
  * @author 涂作权
  * @date 2014-3-23
  * @note begin modify by null
  */
 public static void createAuthenticationTodayFolder() {
  // 判断该文件夹是否存在,如果存在就不需要创建,如果不存在就创建
  File storageFolder = new File(authenticationTodayPath);
  if (storageFolder.exists()) {
   return;
  } else {
   // 创建文件夹
   storageFolder.mkdirs();
   return;
  }
 }

 /**
  * \brief 创建认证文件当天存放文件的位置
  *
  * @attention
  * @author 涂作权
  * @date 2014-3-23
  * @note begin modify by null
  */
 @SuppressWarnings("static-access")
 public static void createAuthenticationTodayFolder(Date date) {
  // 判断该文件夹是否存在,如果存在就不需要创建,如果不存在就创建
  File storageFolder = new File(changeCalendar(date).authenticationTodayPath);
  if (storageFolder.exists()) {
   return;
  } else {
   // 创建文件夹
   storageFolder.mkdirs();
   return;
  }
 }

 /**
  * \brief 创建认证文件第二天应该应该存放的文件夹
  *
  * @attention
  * @author 涂作权
  * @date 2014-3-23
  * @note begin modify by null
  */
 public static void createAuthenticationTomorrowFolder() {
  File storageFolder = new File(authenticationTomorrowPath);
  if (storageFolder.exists()) {
   return;
  } else {
   // 创建文件夹
   storageFolder.mkdirs();
   return;
  }
 }

// @SuppressWarnings("static-access")
// public static void main(String[] args) throws IOException {
//  System.out.println(graphAbsolutePath);
//  System.out.println(graphVirtualPath);
//  System.out.println(graphRelativeStoragePath);
//  System.out.println(graphTodayStoragePath);
//  System.out.println(graphTomorrowStoragePath);
//  System.out.println(wordAbsolutePath);
//  System.out.println(wordVirtualPath);
//  System.out.println(wordRelativeStoragePath);
//  System.out.println(wordTodayStoragePath);
//  System.out.println(wordTomorrowStoragePath);
//  System.out.println(micapsAbsolutePath);
//  System.out.println(micapsVirtualPath);
//  System.out.println(authenticationPath);
//  System.out.println(authenticationTodayPath);
//  System.out.println(authenticationTomorrowPath);
//  createAuthenticationTodayFolder();
//  createAuthenticationTomorrowFolder();
//
//  System.out.println("--- test -- "
//    + changeCalendar(new Date()).graphRelativeStoragePath);
//  System.out.println(graphTempAbsolutePath);
//  System.out.println(graphTempVirtualPath);
//  System.out.println(graphTempTodayStoragePath);
//  System.out.println(graphTempTomorrowStoragePath);
//  System.out.println(graphTempRelativeStoragePath);
//  
//  createGraphTempTodayStorageFolder();
// }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 好的,以下是一个Java代码示例,可以使用Flink从HDFS读取多个文件: ``` import org.apache.flink.api.java.ExecutionEnvironment; import org.apache.flink.api.java.io.TextInputFormat; import org.apache.flink.api.java.tuple.Tuple2; import org.apache.flink.core.fs.Path; public class HDFSReader { public static void main(String[] args) throws Exception { // 创建Flink执行环境 ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); // 设置HDFS文件路径 Path filePath1 = new Path("hdfs://localhost:9000/path/to/first/file"); Path filePath2 = new Path("hdfs://localhost:9000/path/to/second/file"); // 使用TextInputFormat从HDFS读取文件内容 TextInputFormat inputFormat = new TextInputFormat(filePath1); TextInputFormat inputFormat2 = new TextInputFormat(filePath2); // 读取文件内容 env.readFile(inputFormat, filePath1.toString()) .union(env.readFile(inputFormat2, filePath2.toString())) .flatMap((String line, Collector<Tuple2<String, Integer>> out) -> { // 对每行数据进行处理 String[] tokens = line.toLowerCase().split("\\W+"); for (String token : tokens) { if (token.length() > 0) { out.collect(new Tuple2<>(token, 1)); } } }) .groupBy(0) .sum(1) .print(); } } ``` 在这个示例中,我们创建了一个`ExecutionEnvironment`对象,该对象是Flink程序的核心部分,用于执行计算。我们然后指定了多个HDFS文件的路径,并使用`TextInputFormat`来读取文件内容。然后我们使用`readFile`方法从每个文件读取数据,使用`union`方法将所有数据合并为一个数据集,然后使用`flatMap`方法将每行数据转换为一个元组,其中元组包含单词和计数器1。然后我们使用`groupBy`方法按单词对数据进行分组,并使用`sum`方法计算每个单词出现的次数。最后,我们使用`print`方法打印结果。 ### 回答2: 使用Flink读取HDFS上多个文件Java代码如下: ```java import org.apache.flink.api.java.ExecutionEnvironment; import org.apache.flink.core.fs.Path; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.api.java.DataSet; public class ReadMultipleFilesFromHDFS { public static void main(String[] args) throws Exception { // 创建环境 ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); // 设置Hadoop配置,指定HDFS路径 env.getConfig().setGlobalJobParameters(org.apache.flink.configuration.Configuration.fromArgs(args)); // 从HDFS上读取多个文件 DataSet<String> text = env.readTextFile("hdfs://path/to/directory/*.txt"); // 对读取的数据进行处理 DataSet<String> result = text.flatMap((String line, Collector<String> out) -> { String[] words = line.split(" "); for (String word : words) { out.collect(word); } }).groupBy("word").sum(1); // 打印结果 result.print(); } } ``` 在代码中,`ExecutionEnvironment`用于批处理,而`StreamExecutionEnvironment`用于流处理。根据实际需求选择适合的环境。`readTextFile`方法用于从HDFS上读取文本文件,可以使用通配符来处理多个文件读取文件内容会存储在`DataSet`中,按行处理后可以对数据进行各种操作,如拆分、过滤、聚合等。最后,通过调用`print`方法将结果输出。当需要将结果存储到HDFS中时,可以使用`writeAsTextFile`等方法。 ### 回答3: 使用Flink读取HDFS上的多个文件可以使用`TextInputFormat`和`readFile`方法来实现。下面是一个示例的Java代码: ``` import org.apache.flink.api.java.io.TextInputFormat; import org.apache.flink.core.fs.Path; import org.apache.flink.streaming.api.datastream.DataStream; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; public class FlinkReadHDFSFiles { public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); // 设置Hadoop配置,指定HDFS文件系统 env.getConfig().setBoolean("fs.hdfs.hadoopconf", true); // 设置要读取的HDFS文件路径 String hdfsPath = "hdfs://localhost:9000/path/to/files/"; // 创建TextInputFormat,并指定要读取文件路径 TextInputFormat inputFormat = new TextInputFormat(new Path(hdfsPath)); // 使用readFile方法读取HDFS上的多个文件,返回一个DataStream DataStream<String> dataStream = env.readFile(inputFormat, hdfsPath); // 对DataStream进行相应的操作,如打印结果等 dataStream.print(); // 执行Flink任务 env.execute("Flink Read HDFS Files"); } } ``` 注意事项: 1. 需要将`hadoop-common`和`hadoop-hdfs`的依赖添加到项目的`pom.xml`文件中。 2. 需要根据实际情况修改HDFS的配置信息,如HDFS的地址和待读取文件路径。 3. 可以根据具体需求对`dataStream`进行相应的操作,例如进一步处理数据或将结果输出到其他存储系统

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

涂作权的博客

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

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

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

打赏作者

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

抵扣说明:

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

余额充值