Spark 中Java实现数据库Row转Rating

Dataset<Row> ratings = mlsc.sql("SELECT user,movie,rating FROM data");
JavaRDD<Row> rowJavaRDD = ratings.javaRDD();

JavaRDD<String[]> stringJavaRDD = rowJavaRDD.map(new Function<Row, String[]>() {
@Override
public String[] call(Row row) throws Exception {
String[] st = new String[3];
st[0] = row.getString(0);
st[1] = row.getString(1);
st[2] = row.getString(2);
return st;
}
});

JavaRDD<Rating> ratingJavaRDD = stringJavaRDD.map(new Function<String[], Rating>() {
@Override
public Rating call(String[] s) throws Exception {
int user = Integer.parseInt(s[0]);
int movie = Integer.parseInt(s[1]);
Double rating = Double.parseDouble(s[2]);
return new Rating(user,movie,rating);
}
});

posted on 2017-03-20 20:49  BruceSue  阅读(3578)  评论(0编辑  收藏  举报

导航