有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    第一件事是我试图给出一个scala中的csv示例(不是java)

    您可以使用Spark csv api创建数据帧,并根据需要的任何列进行排序。 如果您有任何限制,请参阅下面的方法

    固定列数:

    从下面的固定列数示例开始。。 你可以遵循这个例子

    易趣的数据看起来像什么。csv:

    “8213034705,95,2.927373,jake7870,0,95,117.5,xbox,3”

    //  SQLContext entry point for working with structured data
    val sqlContext = new org.apache.spark.sql.SQLContext(sc)
    // this is used to implicitly convert an RDD to a DataFrame.
    import sqlContext.implicits._
    // Import Spark SQL data types and Row.
    import org.apache.spark.sql._
    
    //define the schema using a case class
    case class Auction(auctionid: String, bid: Float, bidtime: Float, bidder: String, bidderrate: Integer, openbid: Float, price: Float, item: String, daystolive: Integer)
    
    
     val auction = sc.textFile("ebay.csv").map(_.split(",")).map(p => 
    Auction(p(0),p(1).toFloat,p(2).toFloat,p(3),p(4).toInt,p(5).toFloat,p(6).toFloat,p(7),p(8).toInt )).toDF()
    
    // Display the top 20 rows of DataFrame 
    auction.show()
    // auctionid  bid   bidtime  bidder         bidderrate openbid price item daystolive
    // 8213034705 95.0  2.927373 jake7870       0          95.0    117.5 xbox 3
    // 8213034705 115.0 2.943484 davidbresler2  1          95.0    117.5 xbox 3 …
    
    
    // Return the schema of this DataFrame
    auction.printSchema()
    root
     |  auctionid: string (nullable = true)
     |  bid: float (nullable = false)
     |  bidtime: float (nullable = false)
     |  bidder: string (nullable = true)
     |  bidderrate: integer (nullable = true)
     |  openbid: float (nullable = false)
     |  price: float (nullable = false)
     |  item: string (nullable = true)
     |  daystolive: integer (nullable = true)
    
    auction.sort("auctionid") // this will sort first column i.e auctionid
    

    可变列数(since ^{} class with Array parameter is possible):

    您可以使用如下伪代码,其中前4个元素是固定的,其余所有元素都是可变数组

    由于插入的目的只是对第二列进行排序,所以这将得到解决,并且所有其他数据将在该特定行的数组中,以供以后使用

    case class Auction(auctionid: String, bid: Float, bidtime: Float, bidder: String, variablenumberofColumnsArray:String*)
    
     val auction = sc.textFile("ebay.csv").map(_.split(",")).map(p => 
    Auction(p(0),p(1).toFloat,p(2).toFloat,p(3),p(4).toInt, VariableNumberOfColumnsArray or any complex type like Map ).toDF()
    
        auction.sort("auctionid") // this will sort first column i.e auctionid