我不确定流媒体部分,但spark可以高效地处理大文件,并且存储到db表中是并行的,因此在不了解您的详细信息的情况下,如果您的服务器上有上载的文件,我会说:
如果我想在表中保存一个像csv这样的大型结构化文件,我会这样开始:# start with some basic spark configuration, e.g. we want the timezone to be UTC
conf = SparkConf()
conf.set('spark.sql.session.timeZone', 'UTC')
# this is important: you need to have the mysql connector jar for the right mysql version:
conf.set('jars', 'path to mysql connector jar you can download from here: https://dev.mysql.com/downloads/connector/odbc/')
# instantiate a spark session: the first time it will take a few seconds
spark = SparkSession.builder \
.config(conf=conf) \
.appName('Huge File uploader') \
.getOrCreate()
# read the file first as a dataframe
df = spark.read.csv('path to 7GB/ huge csv file')
# optionally, add a filename column
from pyspark.sql import functions as F
df = df.withColumn('filename', F.lit('thecurrentfilename'))
# write it to the table
df.write.format('jdbc').options(
url='e.g. localhost:port',
driver='com.mysql.cj.jdbc.Driver', # the driver for MySQL
dbtable='the table name to save to',
user='user',
password='secret',
).mode('append').save()
注意这里的“append”模式:这里的要点是spark不能对表执行更新,它要么追加新行,要么替换表中的内容。在
所以,如果你的csv是这样的:
^{pr2}$
您将得到一个具有相同字段的表。在
这是我能想到的最基本的例子,这样你就可以从spark开始,而不考虑spark集群或其他相关的东西。我建议你试试看,看看这是否适合你的需要:)
另外,请记住,这可能需要几秒钟或更长的时间,这取决于您的数据、数据库所在的位置、您的计算机和您的数据库负载,因此最好保持与您的api异步,同样,我不知道您的任何其他细节。在
希望这有帮助。祝你好运!在