前言
最近学的东西有些杂乱无章,用到什么就要学习什么,简单记录一下所学的东西,方便后面的巩固学习。
pyspark简单查询数据库的一些信息
程序
- 导入环境设置
from pyspark.sql import SparkSession, Row
from pyspark import sqlContext
from pyspark.sql.functions import udf, col, explode, collect_set, get_json_object, concat_ws, split
from pyspark.sql.types import StringType, IntegerType, StructType, StructField, ArrayType, MapType
# from offline_verification_func import *
spark = SparkSession \
.builder.master("local[50]") \
.config("spark.executor.memory", "10g")\
.config("spark.driver.memory", "20g")\
.config("spark.driver.maxResultSize","4g")\
.appName("test") \
.enableHiveSupport() \
.getorCreate()
- 查询信息1
spark.sql("""
select id, name, age
from students
where age > 14
order by age
""").show()
- 将查询信息转为Pandas格式
df = spark.sql("""
select id, name, age
from students
where age > 14
order by age
""")
# df.repartition(1).write.mode("overwrite").format('csv').save("dfr.csv")
df.toPanads().to_csv("df.csv")