How to read data from mariadb using Spark java(如何使用 Spark java 从 mariadb 读取数据)
问题描述
我需要使用 Spark 和 Java 从 MariaDB 读取一个表.
I need to read a table from MariaDB by using Spark and Java.
我写了一个从数据库读取表数据的Java代码.连接建立成功,但是读取数据时出错.我正在尝试将表数据作为数据框读取.但是列名在结果中显示为列值.找到下面给出的代码:
I wrote a Java code for read table data from database.The connection is established successfully but it produces an error while reading the data. I am trying to read the table data as a dataframe. But the column name is shown as column value in result. find the code given below:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import static org.apache.spark.sql.functions.col;
public class mariadb_to_csv {
public static void main(String[] args) {
Properties prop = new Properties();
String resourceName = "config.properties";
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try(InputStream resourceStream = loader.getResourceAsStream(resourceName)) {
prop.load(resourceStream);
} catch (IOException e) {
e.printStackTrace();
}
SparkSession spark = SparkSession.builder()
.appName("Java Spark SQL basic example")
.config("spark.some.config.option", "some-value").getOrCreate();
Dataset<Row> jdbcDF = spark.read().format("jdbc")
.option("url","url_address")
.option("driver", "org.mariadb.jdbc.Driver")
.option("dbtable", "source_table")
.option("user", "username")
.option("password", "password")
.load();
jdbcDF.select(col("code"), col("name"), col("isActive"), col("createdByUser"), col("modifiedByUser")).show();
}
}
结果,列值在列名中重复.
In result, the column value is duplicated in column name.
这有什么问题?
推荐答案
好像maridb"连接器有问题.将主机 URL 从jdbc:mariadb://${Hostname}:${Port}/${Database}"更改为jdbc:mysql://${Hostname}:${Port}/${Database}" 为我解决了这个问题.
Seems there is a problem with "maridb" connector. Changing the host url from "jdbc:mariadb://${Hostname}:${Port}/${Database}" to "jdbc:mysql://${Hostname}:${Port}/${Database}" solved the problem for me.
MariaDB 和 Databricks 也使用jdbc"作为连接 url 来解释如何使用 Spark 从 Mariadb 读取数据.
MariaDB and Databricks also used "jdbc" as connection url to explain how to read data from Mariadb using Spark.
https://mariadb.com/kb/en/library/mariadb-columnstore-with-spark/#usage
https://docs.databricks.com/spark/latest/data-sources/sql-databases.html
这篇关于如何使用 Spark java 从 mariadb 读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Spark java 从 mariadb 读取数据
基础教程推荐
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01