正常读取:
val spark = SparkSession.builder().master("local").appName("rank").getOrCreate() val sc: SparkContext = spark.sparkContext sc.setLogLevel("error") val lines = sc.textFile("/Users/documents/日志数据分析/20220215134411.csv") lines.take(2).foreach(println) //乱码问题
输出结果:
ord_complete_tm,user_log_acct,corp_name,parent_sale_ord_id,amount
2021-01,��ͨ���Լ����̳�,��ͨ���ֿƼ�����˾,131729320061,1119.0
解决中文乱码读取:
val spark = SparkSession.builder().master("local").appName("rank").getOrCreate() val sc: SparkContext = spark.sparkContext sc.setLogLevel("error") spark.read.option("header", false) .option("encoding","gbk") .option("delimiter", ",") .csv("/Users/limengdong/documents/日志数据分析/20220215134411.csv") .head(3) .foreach(println)
输出结果:
[ord_complete_tm,user_log_acct,corp_name,parent_sale_ord_id,amount]
[2021-01,联通,联通,131729,1119.0]
[2021-01,联通,联通,132049,19.0]