赞
踩
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();
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。