java - 如何使用带有“java/eclipse”的 lambda 函数将 csv 文件加载到 postgres 数据库
问题描述
我必须在 java 代码的帮助下使用 AWS lambda 函数将我的 CSV 文件加载到 PostgreSQL 数据库中。我尝试过同样的 java 代码,不知道如何使用 lambda 函数。
下面是我使用的java代码 -
package net.codejava;
import java.io.*;
import java.sql.*;
public class SimpleCsv2DbInserter {
public static void main(String[] args) {
String jdbcURL = "jdbc:mysql://localhost:3306/sales";
String username = "user";
String password = "password";
String csvFilePath = "Reviews-simple.csv";
int batchSize = 20;
Connection connection = null;
try {
connection = DriverManager.getConnection(jdbcURL, username, password);
connection.setAutoCommit(false);
String sql = "INSERT INTO review (course_name, student_name, timestamp, rating, comment) VALUES (?, ?, ?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(sql);
BufferedReader lineReader = new BufferedReader(new FileReader(csvFilePath));
String lineText = null;
int count = 0;
lineReader.readLine(); // skip header line
while ((lineText = lineReader.readLine()) != null) {
String[] data = lineText.split(",");
String courseName = data[0];
String studentName = data[1];
String timestamp = data[2];
String rating = data[3];
String comment = data.length == 5 ? data[4] : "";
statement.setString(1, courseName);
statement.setString(2, studentName);
Timestamp sqlTimestamp = Timestamp.valueOf(timestamp);
statement.setTimestamp(3, sqlTimestamp);
Float fRating = Float.parseFloat(rating);
statement.setFloat(4, fRating);
statement.setString(5, comment);
statement.addBatch();
if (count % batchSize == 0) {
statement.executeBatch();
}
}
lineReader.close();
// execute the remaining queries
statement.executeBatch();
connection.commit();
connection.close();
} catch (IOException ex) {
System.err.println(ex);
} catch (SQLException ex) {
ex.printStackTrace();
try {
connection.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
解决方案
推荐阅读
- c# - SQL 如何替换一个值
- python - 使用 python 和请求,如何进行正确的 POST 调用以及在哪里找到标头?
- android - FirebaseAppDistribution:appDistributionUpload gradle 命令中缺少应用程序 ID
- mysql - 跨列和行计算值的实例
- javascript - 使用 redux 钩子时使用 redux 操作的最佳选择是什么?
- sql - Impala 2.11:AnalysisException:选择列表中不支持子查询
- swift - 在 SwiftUI 中获取当前的经纬度
- c++ - 是否可以禁止一个类被动态转换为?
- git - Git - 将发布分支合并到主分支
- tensorflow - 任何想法如何解决激活函数的问题?