我是要將rdd轉換成dataframe,如果是Person 型別代碼能執行,但是我本身想用map或者json來封裝資料,不想使用具體型別
但是改成map之后提示例外,我想問下用什么方式能夠使用Map格式的資料。
java.lang.ClassCastException: org.apache.spark.sql.types.MapType cannot be cast to org.apache.spark.sql.types.StructType
我的程式代碼
JavaRDD<Map<String, String>> personsRDD = myRDD.map(new Function<Tuple2<ImmutableBytesWritable, Result>, Map<String, String>>() {
@Override
public Map<String, String> call(Tuple2<ImmutableBytesWritable, Result> tuple) throws Exception {
// TODO Auto-generated method stub
Result result = tuple._2();
String rowkey = Bytes.toString(result.getRow());
String name = Bytes.toString(result.getValue(Bytes.toBytes("data"), Bytes.toBytes("name")));
// String type = Bytes.toString(result.getValue(Bytes.toBytes("data"), Bytes.toBytes("type")));
String age = Bytes.toString(result.getValue(Bytes.toBytes("data"), Bytes.toBytes("age")));
// 這一點可以直接轉化為row型別
// Person p = new Person();
// p.setId(rowkey);
// p.setName(name);
// p.setAge(age);
// return p;
Map<String, String> map = new HashMap<String, String>();
map.put("id", rowkey);
map.put("name", name);
map.put("age", age);
return map;
}
});
DataFrame df = sc.createDataFrame(personsRDD, Map.class);
uj5u.com熱心網友回復:
要創建一個結構描述資訊物件StructType,不能只用Map。轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/53463.html
標籤:Spark
