一、从主节点中查看要插入的文件在HDFS上的元信息
- 数据并没有存放在主节点中,主节点用于维护文件系统,存放元信息(文件的属性,路径,大小,在DN上的位置信息等)
二、在MYSQL中建好表用于插入数据
三、编写代码
java中要有Hadoop和数据库的依赖包
package hdfs; 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 java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; import java.net.URISyntaxException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class HdfsMysql { public static void main(String[] args) throws URISyntaxException, IOException, ClassNotFoundException, SQLException { /** * 创建filesystem对象,连接hdfs */ URI uri = new URI("hdfs://192.168.154.110:9000"); Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(uri, conf); /** * 连接数据库 */ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://master/dadada","root","123456"); PreparedStatement ps = conn.prepareStatement("insert into score(id,sub_id,grade) values (?,?,?)"); /** * 从hdfs中读取数据,切分数据,插入MySQL */ FSDataInputStream in = fs.open(new Path("/data/test/score.txt")); BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8")); String line=null; while ((line=br.readLine())!=null){ String[] split = line.split(","); ps.setString(1,split[0]); ps.setString(2,split[1]); ps.setInt(3, Integer.parseInt(split[2])); ps.executeUpdate(); } ps.close(); in.close(); br.close(); } }
四、结果显示
转载于:https://www.freesion.com/article/88331378080/
END