首页 新闻 赞助 找找看

利用spark读取csv文件乱码问题

0
悬赏园豆:30 [待解决问题]

先上scala代码

val conf = new SparkConf().setAppName("Test1").setMaster("local")
    val sparkContext = new SparkContext(conf)
    val context = new SQLContext(sparkContext)
    val fieldSchema = StructType(Array(
      StructField("area", StringType, true),
      StructField("province", StringType, true),
      StructField("type", StringType, true),
      StructField("name", StringType, true),
      StructField("year", StringType, true),
      StructField("quarter", StringType, true),
      StructField("big", StringType, true),
      StructField("pname", StringType, true),
      StructField("price", DoubleType, true)
    ))
 
    val taxiDF = context.read.format("com.databricks.spark.csv")
      .option("header", "false")
      .schema(fieldSchema)
      .load("f:/data/aa.csv")
 
 
    taxiDF.registerTempTable("t_test")
 
    val area = context.sql("select * from t_test")
 
    area.show()
 
    sparkContext.stop()

输出结果是

area|province| type| name|year|quarter| big| pname| price|
+----+--------+--------+------+----+-------+-----+---------+-------+
|����| ����|��������|������|2009|2009_Q1|����Ʒ|��Ÿ��ϴ��ˮ|44100.0|
|����| ����|��������| ����|2009|2009_Q1|����Ʒ|��Ÿ��ϴ��ˮ|55233.0|
|����| ����|��������| ����־|2009|2009_Q1|����Ʒ|��Ÿ��ϴ��ˮ|27088.0|
|����| ����| �����г�|������|2009|2009_Q1| ����| �ؼ�����|14907.0|
|����| ����| �����г�| ��԰|2009|2009_Q1|����Ʒ| ��ˮ����|10757.0|
|����| ����|��������|������|2009|2009_Q2|����Ʒ|��Ÿ��ϴ��ˮ|46916.0|
|����| ����|��������| ����|2009|2009_Q2|����Ʒ|��Ÿ��ϴ��ˮ|59874.0|
|����| ����|��������| ����־|2009|2009_Q2|����Ʒ|��Ÿ��ϴ��ˮ|27880.0|
|����| ����| �����г�|������|2009|2009_Q2| ����| �ؼ�����|17030.0|
|����| ����| �����г�| ��԰|2009|2009_Q2|����Ʒ| ��ˮ����|15303.0|
|����| ����|��������|������|2009|2009_Q3| ����| �ؼ���| 6870.0|
|����| ����|��������| ����|2009|2009_Q3|����Ʒ|��Ÿ��ϴ��ˮ|63150.0|
|����| ����|��������| ����־|2009|2009_Q3|����Ʒ|��Ÿ��ϴ��ˮ|31578.0|
|����| ����| �����г�|������|2009|2009_Q3| ����| �ؼ�����|19006.0|
|����| ����| �����г�| ��԰|2009|2009_Q3|����Ʒ| ��ˮ����|18703.0|
|����| ����|��������|������|2009|2009_Q4| ����| �ؼ���| 8985.0|
|����| ����|��������| ����|2009|2009_Q4|����Ʒ|��Ÿ��ϴ��ˮ|64467.0|
|����| ����|��������| ����־|2009|2009_Q4|����Ʒ|��Ÿ��ϴ��ˮ|32479.0|
|����| ����| �����г�|������|2009|2009_Q4| ����| �ؼ�����|22295.0|
|����| ����| �����г�| ��԰|2009|2009_Q4|����Ʒ| ��ˮ����|22220.0|

ShadowXCX的主页 ShadowXCX | 初学一级 | 园豆:76
提问于:2017-10-12 17:36
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册