Asl*_*lan 6 java-8 apache-spark apache-spark-sql
我正在尝试将 DataSet 转换为 java 对象。架构就像
root
|-- deptId: long (nullable = true)
|-- depNameName: string (nullable = true)
|-- employee: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- firstName: string (nullable = true)
| | |-- lastName: string (nullable = true)
| | |-- phno: Long (nullable = true)
| | | |-- element: integer (containsNull = true)
Run Code Online (Sandbox Code Playgroud)
我创建了 pojo 类 Like。
class Department {
private Long deptId;
private String depName;
private List<Employee> employess;
//with getter setters and no argument constructor
}
class Employee {
private String firstName;
private String lastName;
private List<Long> phno;
//With getter setter and no argument constructor
}
Run Code Online (Sandbox Code Playgroud)
现在这是我正在尝试进行转换的代码。
Dataset<Row> ds = this.spark.read().parquet(Parquet file path);
Dataset<Department> departmentDataset =
ds.as(Encoders.bean(Department.class));
JavaRDD<String> rdd =
departmentDataset.toJavaRDD().map((Function<Department, String>) v -> {
StringBuilder sb = new StringBuilder();
sb.append("deptId").append(v.getDeptID());
if(!CollectionUtil.isListNullOrEmpty(v.employee))
sb.append("FirstName").append(v.getEmployee().get(0).getName);
if(!CollectionUtil.isListNullOrEmpty(v.getEmployee().getPhno()))
sb.append("Ph
number").append(v.getEmployee().getPhno().get(0));
return sb.toString();
});
Run Code Online (Sandbox Code Playgroud)
但是这段代码不起作用。它失败了org.apache.hadoop.yarn.server.nodemanager.containermanager.runtime.ContainerExecutionException。但是我可以使用基于行的构造函数来转换它,我需要对列名进行硬编码。喜欢
public Department(Row row)
{
this.employees = new ArrayList<Employee>
this.deptaID = (Long)row.getAs("deptId");
List rowList = (List)row.getList(row.fieldIndex("employee"));
if (rowList!=null) {
for (Row r : rowList) {
Employee obj = new Employee(r);
employees.add(obj);
}
}
public Employee(Row row)
{
this.phno = new ArrayList<Long>
this.firstName = (Long)row.getAs("firstName");
List rowList = (List)row.getList(row.fieldIndex("phno"));
if (rowList!=null) {
for (Row r : rowList) {
phno.add(r);
}
}
JavaRDD<Department> rdd = ds.toJavaRDD().map(Department::new);
JavaRDD<String> rdd = rdd.map((Function<Department, String>) v -> {
StringBuilder sb = new StringBuilder();
sb.append("deptId").append(v.getDeptID());
if(!CollectionUtil.isListNullOrEmpty(v.employee))
sb.append("FirstName").append(v.getEmployee().get(0).getName);
if(!CollectionUtil.isListNullOrEmpty(v.getEmployee().getPhno()))
sb.append("Ph
number").append(v.getEmployee().getPhno().get(0));
return sb.toString();
});
Run Code Online (Sandbox Code Playgroud)
通过这种方法,我获得了成功。但是它包含了很多 Schema 名称和所有的硬编码。所以寻找更优雅的解决方案。
请建议此问题的最佳解决方案。
| 归档时间: |
|
| 查看次数: |
4911 次 |
| 最近记录: |