概述
运行的程序:Hadoop编写MapReduce之统计学生平均成绩
将程序所得结果内容进行读取,然后写入数据库
Conn.java
package com.hadoop.ComputerScore;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Conn {
private Connection conn = null;
public Conn() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
try {
conn = (Connection)DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/sartin?useSSL=false&serverTimezone=UTC","root","123456");
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 插入
public int insert(String name, double score)
{
String sql = "insert into score values(?, ?)";
int result = 0;
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setDouble(2, score);
result = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}
readTomysql.java
package com.hadoop.ComputerScore;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;
public class readTomysql {
//集群信息 连接集群
public static FileSystem getCon() throws IOException, InterruptedException, URISyntaxException
{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.100.11:9000"), conf, "hadoop");
return fs;
}
@Test
public void read() throws IOException, InterruptedException, URISyntaxException
{
FileSystem fs = getCon();
FSDataInputStream his = fs.open(new Path("/score/out/part-r-00000"));
byte [] buffer = new byte[1024]; //1024个字节的读
int len = 0;
String str = "";
while((len=his.read(buffer))!=-1)
{
str = new String(buffer, 0, len);
}
String [] strs = str.split("n");
int time = 0;
for(String st : strs)
{
System.out.print(st+" ");
String [] s = st.split("t");
Conn con = new Conn();
con.insert(s[0], Double.parseDouble(s[1]));
System.out.println("len:" + s.length);
time++;
if(time % 1 == 0)
{
System.out.println("");
}
}
}
}
最后
以上就是友好美女为你收集整理的Eclipse将Hadoop上运行的结果读取并写入mysql数据库的全部内容,希望文章能够帮你解决Eclipse将Hadoop上运行的结果读取并写入mysql数据库所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复