本文整理汇总了Java中org.apache.spark.sql.DataFrame.collectAsList方法的典型用法代码示例。如果您正苦于以下问题:Java DataFrame.collectAsList方法的具体用法?Java DataFrame.collectAsList怎么用?Java DataFrame.collectAsList使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.spark.sql.DataFrame
的用法示例。
在下文中一共展示了DataFrame.collectAsList方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getMovieRecommendations
import org.apache.spark.sql.DataFrame; //导入方法依赖的package包/类
public static void getMovieRecommendations(int userId, int num, ResultSet[] resultSets) {
try {
JavaSparkContext spliceSparkContext = SpliceSpark.getContext();
SQLContext sqlContext = new SQLContext(spliceSparkContext);
Connection conn = DriverManager.getConnection("jdbc:default:connection");
// Read the latest models path form database
String modelPath = "tmp/movielensRecommender";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT model_path from MOVIELENS.MODEL ORDER BY create_date DESC {limit 1}");
if (rs.next()) {
modelPath = rs.getString(1);
}
rs.close();
stmt.close();
// Load the model
MatrixFactorizationModel sameModel = MatrixFactorizationModel.load(spliceSparkContext.sc(), modelPath);
// Get recommendation for the specified user and number of items
Rating[] recom = sameModel.recommendProducts(userId, num);
// Load the Movies table to get the details of the Movies
Map<String, String> options = new HashMap<String, String>();
options.put("driver", "com.splicemachine.db.jdbc.ClientDriver");
options.put( "url", "jdbc:splice://localhost:1527/splicedb;user=splice;password=admin;useSpark=true");
options.put("dbtable", "MOVIELENS.MOVIES");
DataFrame moviesDF = sqlContext.read().format("jdbc").options(options).load();
moviesDF.registerTempTable("TEMP_MOVIES");
// Collect the Movied Ids from Recommendations
StringBuffer sFilter = new StringBuffer();
for (Rating rate : recom) {
if (sFilter.length() > 0)
sFilter.append(", ");
sFilter.append(rate.product());
}
// Apply filter to select only the recommended Movies
DataFrame filteredMoviesDF = sqlContext.sql("Select * from TEMP_MOVIES where MOVIE_ID in (" + sFilter.toString() + ")");
List<Row> recMovieList = filteredMoviesDF.collectAsList();
// Collect the details to build Result Set to return with the
// details
int movId = 0;
String movTitle = "";
List<ExecRow> rows = new ArrayList();
for (Row movie : recMovieList) {
ExecRow row = new ValueRow(9);
row.setColumn(1, new SQLInteger(movie.getInt(0)));
row.setColumn(2, new SQLVarchar(movie.getString(1)));
rows.add(row);
}
// Convert the List of ExecRows to Result Set
Activation lastActivation = ((EmbedConnection) conn).getLanguageConnection().getLastActivation();
IteratorNoPutResultSet resultsToWrap = new IteratorNoPutResultSet(rows, MOVIE_RECOMMENDATIONS_COLUMNS, lastActivation);
resultsToWrap.openCore();
// Set the Return resultset
resultSets[0] = new EmbedResultSet40((EmbedConnection) conn, resultsToWrap, false, null, true);
} catch (StandardException e) {
LOG.error("Exception in getColumnStatistics", e);
e.printStackTrace();
} catch (SQLException sqle) {
LOG.error("Exception in getColumnStatistics", sqle);
sqle.printStackTrace();
}
}