我正在嘗試將hadoop代碼遷移到spark中。我已經有了一些預定義的函數,我應該可以在spark中重用,因爲它們僅僅是java代碼,沒有太多的hadoop依賴性。我有一個函數接受文本格式的輸入(空間數據 - 經度,緯度)並將它們轉換爲形狀(多邊形,線流等)。當我嘗試在Spark中讀取它時,我首先以String的形式讀取每行文件。然後將它們轉換爲文本,以便我可以使用我以前創建的函數。但是我有兩個疑問,首先看起來JavaRDD沒有使用文本,並且我正在收到一些問題。其次,將文本轉換爲形狀的功能不會返回任何內容。但我無法使用flatMap或任何其他映射技術。我甚至不確定我的方法是否正確。在JavaRDD中使用文本數據類型並在FlatMap中返回void
這裏是我的代碼模型:
/*function for converting Text to Shape*/
public interface TextSerializable {
public Text toText(Text text);
public void fromText(Text text);
* Retrieve information from the given text.
* @param text The text to parse
*/
}
/*Shape Class looks something like this*/
public interface Shape extends Writable, Cloneable, TextSerializable {
/
* Returns minimum bounding rectangle for this shape.
* @return The minimum bounding rectangle for this shape
*/
public Rectangle getMBR();
/**
* Gets the distance of this shape to the given point.
* @param x The x-coordinate of the point to compute the distance to
* @param y The y-coordinate of the point to compute the distance to
* @return The Euclidean distance between this object and the given point
*/
......
......
......*/
/*My code structure*/
SparkConf conf = new SparkConf().setAppName("XYZ").setMaster("local");
JavaSparkContext sc =new JavaSparkContext(conf);
final Text text=new Text();
JavaRDD<String> lines = sc.textFile("ABC.csv");
lines.foreach(new VoidFunction<String>(){
public void call(String lines){
text.set(lines);
System.out.println(text);
}
});
/*Problem*/
text.flatMap(new FlatMapFunction<Text>(){
public Iterable<Shape> call(Shape s){
s.fromText(text);
//return void;
}
代碼的最後一行是錯誤的,但我不知道如何解決它。 JavaRDD可以與用戶定義的類一起使用(根據我的知識)。我甚至不確定我是否已經將字符串行轉換爲文本文本(如果RDD中允許的話)。我在Spark中是全新的。任何形式的幫助都會很棒。
非常感謝這些鏈接。我有一個想法如何去做。 – SGh