My below program attempts to print the price of the most expensive house, given a CSV file containing information regarding the houses. Unexpectedly, I receive 0 as the max price instead of a non-zero integer (which I have confirmed the existence of, for example the first entry in the CSV file)?
Program
public class SparkWordCounter {
public static void main(String[] args) {
SparkSession sparkSession = SparkSession.builder().appName("WordCounter").config("spark.master", "local").getOrCreate();
String fileName = SparkWordCounter.class.getResource("/Sacramentorealestatetransactions.csv").toString();
StructField[] structFields = {
DataTypes.createStructField("street", DataTypes.StringType, false),
DataTypes.createStructField("city", DataTypes.StringType, false),
DataTypes.createStructField("zip", DataTypes.StringType, false),
DataTypes.createStructField("state", DataTypes.StringType, false),
DataTypes.createStructField("beds", DataTypes.ByteType, false),
DataTypes.createStructField("baths", DataTypes.ByteType, false),
DataTypes.createStructField("sqFt", DataTypes.ShortType, false),
DataTypes.createStructField("type", DataTypes.StringType, false),
DataTypes.createStructField("sale_data", DataTypes.StringType, false),
DataTypes.createStructField("price", DataTypes.IntegerType, false),
DataTypes.createStructField("latitude", DataTypes.StringType, false),
DataTypes.createStructField("longitude", DataTypes.StringType, false)
};
StructType structType = DataTypes.createStructType(structFields);
Dataset<Row> dataset = sparkSession.read().option("header", "true").schema(structType).csv(fileName);
Dataset<Building> buildingDataset = dataset.as(Encoders.bean(Building.class));
long price = buildingDataset
.map(building -> building.price, Encoders.INT())
.reduce(Integer::max);
System.out.println("Price: " + price);
}
public static class Building implements Serializable {
public String street;
public String city;
public String zip;
public String state;
public byte beds;
public byte baths;
public short sqFt;
public String type;
public String sale_date;
public int price;
public String latitude;
public String longitude;
}
}
CSV first entry (985 total)
street,city,zip,state,beds,baths,sqFt,type,sale_date,price,latitude,longitude
---,---,---,---,2,1,836,Residential,Wed May 21 00:00:00 EDT 2008,59222,38.---,---
---
represents information revealing location
The prices are guaranteed to be integers, thus an int
is used (byte
and short
are used for values with smaller ranges).
Why is 0 being the calculated as the max price?