给我一个用Scala编写的复杂一点的和药相关的spark实例,包含代码和数据
时间: 2024-05-14 16:18:49 浏览: 231
以下是一个使用Scala编写的复杂一点的和药相关的Spark实例,该实例使用了医药数据集,并对其进行了一些数据处理和分析。该实例的数据集可以在Kaggle上找到。
代码:
```
import org.apache.spark.sql.functions._
import org.apache.spark.sql.types._
import org.apache.spark.sql.{DataFrame, SparkSession}
object DrugAnalysis {
def main(args: Array[String]): Unit = {
// Create a SparkSession
val spark = SparkSession.builder
.appName("Drug Analysis")
.getOrCreate()
// Read in the drug dataset
val drugDF = spark.read.format("csv")
.option("header", "true")
.option("inferSchema", "true")
.load("drug_data.csv")
// Filter out any rows where the drug name is null
val filteredDF = drugDF.filter(col("drugName").isNotNull)
// Calculate the number of reviews for each drug
val numReviewsDF = filteredDF.groupBy("drugName")
.agg(count("drugName").alias("numReviews"))
.orderBy(desc("numReviews"))
// Calculate the average rating for each drug
val avgRatingDF = filteredDF.groupBy("drugName")
.agg(avg("rating").alias("avgRating"))
.orderBy(desc("avgRating"))
// Join the number of reviews and average rating data
val joinedDF = numReviewsDF.join(avgRatingDF, Seq("drugName"))
// Calculate the top 10 drugs by number of reviews
val top10ReviewsDF = numReviewsDF.limit(10)
// Calculate the top 10 drugs by average rating
val top10RatingDF = avgRatingDF.limit(10)
// Print out the top 10 drugs by number of reviews
println("Top 10 Drugs by Number of Reviews:")
top10ReviewsDF.show(false)
// Print out the top 10 drugs by average rating
println("Top 10 Drugs by Average Rating:")
top10RatingDF.show(false)
// Stop the SparkSession
spark.stop()
}
}
```
数据:
drug_data.csv
```
patientId,drugName,condition,review,rating,date
206461,Valsartan,Left Ventricular Dysfunction,"""It has no side effect, I take it in combination of Bystolic 5 Mg and Fish Oil""",9,20-May-12
95260,Guanfacine,ADHD,"My son is halfway through his fourth week of Intuniv. We became concerned when he began this last week when he started taking the highest dose he will be on. We began to see him become agitated, aggressive and irritable on a daily basis. He had been on a lower dose last year and we had seen enormous improvements in his behavior. We spoke to the doctor and he told us to hang in there, that there is a ""break in period"" when dosage is increased. We are hoping that is what we are seeing now. So far he has not had any improvement in his ADHD symptoms but he is only taking 3mg in the morning and then another 3mg in the afternoon. It is becoming very difficult for us and for him in school and at home.",8,27-Apr-10
92703,Lybrel,Birth Control,"I used to take another oral contraceptive, which had 21 pills per cycle. This one has no break between cycles. I have not experienced any unwanted side effects. I am very happy with this product.",5,14-Dec-09
138000,Nexplanon,Birth Control,"I had been on the pill for many years. When my doctor told me about the implant, I agreed to it because I liked the idea of not having to remember to take a pill. I had it implanted in my arm in November of 2016. Since then, I have had two periods that were very heavy and lasted about 10 days each. I have also had spotting in between periods. I have gained weight and have been very moody. I have also had a lot of acne, which is something I never had before. I'm planning on having it removed soon.",8,08-Dec-16
```
阅读全文