我正在尝试将数据插入数据库的空表中:
火花版本:2.3.0
否。要插入的记录数:1000万
方法1:
outputDataSet.write().format("jdbc").option("url", connection).option("dbtable", "XYZ").save();
输出:显示表已存在的错误。
方法2:
outputDataSet.write().insertInto("XYZ");
输出:错误,指出表或视图不存在。
我阅读了insertInto(String tableName)
的说明文件:
因为它将数据插入到现有表中,所以格式或选项将被忽略。
我不确定如何使用apache spark在现有数据库表中插入数据。
编辑
方法3:
outputDataSet.write().mode("append").format("jdbc").option("url", connection).option("dbtable", "XYZ").save();
输出:进程一直在运行,但是最后我没有杀死表,因为它没有停止,所以不得不终止该进程。
答案 0 :(得分:0)
我想您可以使用下面的代码。如果不存在该表,则会创建该表;如果该数据已存在,则会添加数据
if(isset($_POST['formHasbeenPosted'])){
if (empty($_POST["name2"])) {
$nameError = "Name is required";
echo $nameError;
} else {
$name = test_input($_POST["name2"]);
// check name for alphanumeric values
if (!preg_match("/^[a-zA-Z0-9]*$/",$name)) {
$nameError = "Only alphanumeric values allowed";
echo $nameError ;
}
}
if (empty($_POST["email2"])) {
$emailError = "Email is required";
echo $emailError ;
} else {
$email = test_input($_POST["email2"]);
// check if e-mail address syntax is valid or not
if (!preg_match("/([w-]+@[w-]+.[w-]+)/",$email)) {
$emailError = "Invalid email format";
echo $emailError ;
}
}
//Check if comment is empty
if (empty($_POST["comm"])) {
$comment = "";
} else {
$comment = test_input($_POST["comm"]);
}
//Check if gender is empty
if (empty($_POST["gn"])) {
$genderError = "Gender is required";
echo $genderError ;
} else {
$gender = test_input($_POST["gn"]);
}
if($nameError == "" && $emailError == "" && $genderError == ""){
echo "Data is valid, you can process";
}
}
您也可以尝试这个吗
outputDataSet.write.mode(“ append”)。saveAsTable(“ my_table”)
答案 1 :(得分:-1)
尝试
outputDataSet.write.format("jdbc").option("url", connection).option("dbtable", "XYZ").mode("overwrite").save();