eclipse创建maven工程读取hdfs文件发送邮件

本文介绍了如何在Eclipse中创建一个Maven工程,该工程能够读取HDFS上的文件并发送邮件。通过配置pom.xml,打包过程中包含了所有依赖的jar包,以及hdfs-site.xml和core-site.xml配置文件,确保了在YARN上运行时能正常执行。
摘要由CSDN通过智能技术生成

1、在maven下新建maven工程

2、编写代码

package WorkFlow.Mail;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class MailSend {

	public static void main(String[] args) throws IOException{
		MailSend mail = new MailSend();
		mail.readFile();
		mail.sendmail();
	}
	private void readFile(){
		try {
			Configuration conf = new Configuration();
			FileSystem file = FileSystem.get(conf);
			String path ="/tmp/daily_mail/CN/sql/";
			String Outputpath ="/tmp/daily_mail/CN/hql/";
			FileStatus[] lstStatus = file.listStatus(new Path(path));
			for (FileStatus status : lstStatus) {
				FSDataInputStream inputStream = file.open(status.getPath());
				BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
				String sql = "";
				String line = null;
				while (null != (line = br.readLine())) {
					sql += line;
					sql += " ";
				}
				System.out.println(sql);
				String name = Outputpath + status.getPath().getName();
				FileSystem OutPutfile = FileSystem.get(conf);
				OutPutfile.deleteOnExit(new Path(name));
				OutPutfile.createNewFile(new Path(name));
				FSDataOutputStream Outputfs = OutPutfile.append(new Path(name));
				Outputfs.write(sql.getBytes());
				Outputfs.flush();
				Outputfs.close();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	private void sendmail() throws IOException{
		Configuration conf = new Configuration();
		FileSystem file = FileSystem.get(conf);
		String path ="/tmp/daily_mail/CN/sql/";
		String Outputpath ="/tmp/daily_mail/CN/hql/";
		String sql = "";
		FileStatus[] lstStatus = file.listStatus(new Path(path));
		for (FileStatus status : lstStatus) {
			FSDataInputStream inputStream = file.open(status.getPath());
			BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));			
			String line = null;
			while (null != (line = br.readLine())) {
				sql += line;
				sql += " ";
			}
		}
		Mail.send("Work Flow send mail Test", sql);
	}
}

Mail.class

package WorkFlow.Mail;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class Mail {
	private MimeMessage mimeMsg;
	private Session session;
	private Properties props;
	private String username;
	private String password;
	private Multipart mp;
	public Mail(String 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值