当前位置:   article > 正文

hdfs写入MySQL_java读取Mysql数据库写入hdfs

java实现mysql数据写入hdfs

package com.Test;

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.UnsupportedEncodingException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.DriverPropertyInfo;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.SQLFeatureNotSupportedException;

import java.sql.Statement;

import java.util.Properties;

import java.util.logging.Logger;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.FSDataOutputStream;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

public class Mysql {

String url = "jdbc:mysql://192.168.0.44:3306/mysql";

String user = "root";

String password = "mysql";

public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {

String driver = "com.mysql.jdbc.Driver";

Class.forName(driver);

Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.0.44:3306/mysql", "root", "mysql");

PreparedStatement state = conn.prepareStatement("select * from aa");

ResultSet rs = state.executeQuery();

String str = "";

while (rs.next()) {

str += rs.getString(1)+ " " + rs.getString(2) + "\n";

}

System.out.print(str);

InputStream in = new ByteArrayInputStream(str.getBytes("UTF-8"));

Configuration conf = new Configuration();

FileSystem fs = FileSystem.get(conf);

String hdfs = "/user/input/1.txt";

FSDataOutputStream fo = fs.create(new Path(hdfs));

int len = 0;

byte[] b = new byte[1024];

while ((len = in.read(b)) > -1) {

fo.write(b, 0, len);

}

conn.close();

in.close();

fo.close();

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/519505
推荐阅读
相关标签
  

闽ICP备14008679号