用java写一个从数据库提取数据到大数据平台的代码
时间: 2023-06-13 13:04:02 浏览: 84
假设你要从MySQL数据库中提取数据到Hadoop平台,可以使用以下Java代码:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
public class MySQLToHadoop {
// MySQL数据库连接信息
private static final String MYSQL_URL = "jdbc:mysql://localhost:3306/mydb";
private static final String MYSQL_USER = "username";
private static final String MYSQL_PASSWORD = "password";
// HDFS存储路径
private static final String HDFS_PATH = "hdfs://localhost:9000/user/hadoop/data";
public static void main(String[] args) throws Exception {
// 连接MySQL数据库
Connection conn = DriverManager.getConnection(MYSQL_URL, MYSQL_USER, MYSQL_PASSWORD);
// 查询数据
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM mytable");
// 定义Hadoop文件系统
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
// 创建SequenceFile输出流
Path outputPath = new Path(HDFS_PATH + "/data.seq");
Text key = new Text();
Text value = new Text();
SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, outputPath, key.getClass(), value.getClass());
// 遍历ResultSet并写入SequenceFile
while (rs.next()) {
key.set(rs.getString("id"));
value.set(rs.getString("data"));
writer.append(key, value);
}
// 关闭输出流和ResultSet
writer.close();
rs.close();
stmt.close();
conn.close();
}
}
```
这段代码使用JDBC连接MySQL数据库,并执行一个SELECT语句来查询数据。然后,它使用Hadoop的SequenceFile格式将数据写入HDFS中。你需要将代码中的MySQL连接信息和HDFS存储路径替换为你自己的。
阅读全文