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 smtp) {
setSmtpHost(smtp);
createMimeMessage();
}
public void setSmtpHost(String hostName) {
System.out.println("设置系统属性:mail.smtp.host=" + hostName);
if (props == null) {
props = System.getProperties();
}
props.put("mail.smtp.host", hostName);
}
public boolean createMimeMessage() {
try {
System.out.println("准备获取邮件会话对象!");
session = Session.getDefaultInstance(props, null);
} catch (Exception e) {
System.out.println("获取邮件会话错误!" + e);
return false;
}
System.out.println("准备创建MIME邮件对象!");
try {
mimeMsg = new MimeMessage(session);
mp = new MimeMultipart();
return true;
} catch (Exception e) {
System.out.println("创建MIME邮件对象失败!" + e);
return false;
}
}
/*定义SMTP是否需要验证*/
public void setNeedAuth(boolean need) {
System.out.println("设置smtp身份认证:mail.smtp.auth = " + need);
if (props == null)
props = System.getProperties();
if (need) {
props.put("mail.smtp.auth", "true");
} else {
props.put("mail.smtp.auth", "false");
}
}
public void setNamePass(String name, String pass) {
username = name;
password = pass;
}
/*定义邮件主题*/
public boolean setSubject(String mailSubject) {
System.out.println("定义邮件主题!");
try {
mimeMsg.setSubject(mailSubject);
return true;
} catch (Exception e) {
System.err.println("定义邮件主题发生错误!");
return false;
}
}
/*定义邮件正文*/
public boolean setBody(String mailBody) {
try {
BodyPart bp = new MimeBodyPart();
bp.setContent("" + mailBody, "text/html;charset=GBK");
mp.addBodyPart(bp);
return true;
} catch (Exception e) {
System.err.println("定义邮件正文时发生错误!" + e);
return false;
}
}
/*设置发信人*/
public boolean setFrom(String from) {
System.out.println("设置发信人!");
try {
mimeMsg.setFrom(new InternetAddress(from)); //发信人
return true;
} catch (Exception e) {
return false;
}
}
/*定义收信人*/
public boolean setTo(String to) {
if (to == null)
return false;
System.out.println("定义收信人!");
try {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
return true;
} catch (Exception e) {
return false;
}
}
/*定义抄送人*/
public boolean setCopyTo(String copyto) {
if (copyto == null)
return false;
try {
InternetAddress[] iaToList = new InternetAddress().parse(copyto);
mimeMsg.setRecipients(Message.RecipientType.CC, iaToList);
return true;
} catch (Exception e) {
return false;
}
}
/*发送邮件模块*/
public boolean sendOut() {
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
Session mailSession = Session.getInstance(props, null);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), username, password);
transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
System.out.println("发送成功!");
transport.close();
return true;
} catch (Exception e) {
System.err.println("邮件失败!" + e);
return false;
}
}
/*调用sendOut方法完成发送*/
private static boolean sendAndCc(String smtp, String from, String to, String copyto,
String subject, String content, String username, String password) {
Mail theMail = new Mail(smtp);
theMail.setNeedAuth(true); // 验证
if (!theMail.setSubject(subject))
return false;
if (!theMail.setBody(content))
return false;
if (!theMail.setTo(to))
return false;
if (!theMail.setCopyTo(copyto))
return false;
if (!theMail.setFrom(from))
return false;
theMail.setNamePass(username, password);
if (!theMail.sendOut())
return false;
return true;
}
public static void send(String title, String content) {
String smtp = "smtp.mxhichina.com";
String from = "lin@dewmobile.com";
String to = "lin@dewmobile.com";
String copyto = "lin@dewmobile.com";
String subject = title;
String username = "lin@dewmobile.com";
String password = "Lin198717";
sendAndCc(smtp, from, to, copyto, subject, content, username, password);
// the time of mail
Date d = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmsss");
System.out.println(df.format(d));
}
}
3、引用的第三方jar包
4、pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>WorkFlow</groupId>
<artifactId>Mail</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Mail</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.6</version>
<scope>system</scope>
<systemPath>C:/Program Files/Java/jdk1.8.0_73/lib/tools.jar</systemPath>
</dependency>
</dependencies>
</project>
5、maven clean,maven package
打包大小为8k,在hdfs中运行
yarn jar WorkFlow.Mail.MailSend
能够正常运行,并且发送邮件
6、打包所有的dependency jar包,在pom.xml文件中添加将jar包打入到jar包中
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>WorkFlow.Mail.MailSend</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
上传到hdfs服务器,运行java -jar Mail.jar,出现如下错误
使用yarn jar Mail-0.0.1.jar能够正常运行。
7、将hdfs-site.xml和core-site.xml文件打包到jar包中,修改pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>WorkFlow</groupId>
<artifactId>Mail</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Mail</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.6</version>
<scope>system</scope>
<systemPath>C:/Program Files/Java/jdk1.8.0_73/lib/tools.jar</systemPath>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>conf/</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>WorkFlow.Mail.MailSend</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
java -jar Mail-001.jar能够正常运行,yarn jar Mail-001.jar 也能够正常运行
并且在jar包中包含xml文件
8、将依赖jar包拷贝到工程的lib文件夹中
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>