【发布时间】:2018-08-02 09:58:22
【问题描述】:
所以,我有一个 CSV,其中包含空间(latitude、longitude)和时间(timestamp)数据。
为了对我们有用,我们将空间信息转换为“geohash”,将时间信息转换为“timehash”。
问题是,如何使用 spark 将 geohash 和 timehash 添加为 CSV 中每一行的字段(因为数据约为 200 GB)?
我们尝试使用 JavaPairRDD 和它的函数 mapTopair ,但问题仍然在于如何转换回 JavaRdd 然后转换为 CSV?所以我认为这是一个糟糕的解决方案,我要求一种简单的方法。
问题更新:
在@Alvaro 的帮助下,我创建了这个 java 类:
public class Hash {
public static SparkConf Spark_Config;
public static JavaSparkContext Spark_Context;
UDF2 geohashConverter = new UDF2<Long, Long, String>() {
public String call(Long latitude, Long longitude) throws Exception {
// convert here
return "calculate_hash";
}
};
UDF1 timehashConverter = new UDF1<Long, String>() {
public String call(Long timestamp) throws Exception {
// convert here
return "calculate_hash";
}
};
public Hash(String path) {
SparkSession spark = SparkSession
.builder()
.appName("Java Spark SQL Example")
.config("spark.master", "local")
.getOrCreate();
spark.udf().register("geohashConverter", geohashConverter, DataTypes.StringType);
spark.udf().register("timehashConverter", timehashConverter, DataTypes.StringType);
Dataset df=spark.read().csv(path)
.withColumn("geohash", callUDF("geohashConverter", col("_c6"), col("_c7")))
.withColumn("timehash", callUDF("timehashConverter", col("_c1")))
.write().csv("C:/Users/Ahmed/Desktop/preprocess2");
}
public static void main(String[] args) {
String path = "C:/Users/Ahmed/Desktop/cabs_trajectories/cabs_trajectories/green/2013";
Hash h = new Hash(path);
}
}
然后我得到序列化问题,当我删除write().csv()时它消失了
【问题讨论】:
-
您能否分享一些 sn-p 以便我们了解您当前如何加载 CSV?
-
你需要知道我是如何在 spark 中加载 csv 的??!!
-
我只是想看看您是使用 Datasets Spark API 还是只是创建和 RDD 逐行读取文件。
标签: java apache-spark apache-spark-sql rdd