我正在使用Java编写Spark应用程序,我想知道如何从文字值创建DataFrame和/或JavaRDD。
例如,我有3个整数,说(784512, 35, 40)
对应于字段/列(id, m_count, f_count)
。
答案 0 :(得分:2)
您希望SparkContext.parallelize(...)
创建JavaRDD并SQLContext.createDataFrame(...)
来创建数据帧。
JavaRDD rdd = sc.parallelize(Arrays.asList(1, 2, 3, 4));
如果您在创建具有三个值的对象的并行列表之后,则需要:
@Test
public void test() {
JavaSparkContext sc = ...
SQLContext sqlContext = new SQLContext(sc);
JavaRDD<Counter> counters = sc.parallelize(Arrays.asList(new Counter(784512, 35, 40)));
DataFrame countersDF = sqlContext.createDataFrame(counters, Counter.class);
System.out.println(counters.collect());
System.out.println(countersDF.collectAsList());
}
public static class Counter implements Serializable{
private final int id;
private final int m_count;
private final int f_count;
Counter(int id, int m_count, int f_count) {
this.id = id;
this.m_count = m_count;
this.f_count = f_count;
}
public String toString() {
return id + " " + m_count + " " + f_count;
}
// getters
}