Eclipse将Hadoop上运行的结果读取并写入mysql数据库

运行的程序: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("");
				}		
			}
		}

}

请添加图片描述
请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值