gpt4 book ai didi

java - 如何在数据框中动态地从列表中选择列加上固定列

转载 作者:行者123 更新时间:2023-12-02 00:59:04 26 4
gpt4 key购买 nike

我正在使用带有 java8 的 Spark-sql-2.4.1v。

我有动态列列表被传递到我的函数中。

List<String> cols = Arrays.asList("col_1","col_2","col_3","col_4");
Dataset<Row> df = //which has above columns plus "id" ,"name" plus many other columns;

需要选择 cols + "id"+ "name"

我正在做如下

Dataset<Row> res_df = df.select("id", "name", cols.stream().toArray( String[]::new)); 

this is giving compilation error. so how to handle this use-case.

尝试过:

当我执行如下操作时:

List<String> cols = new ArrayList<>(Arrays.asList("col_1","col_2","col_3","col_4"));
cols.add("id");
cols.add("name");

给出错误

Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)

最佳答案

您可以创建列数组并将其传递给 select 语句。

import org.apache.spark.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

List<String> cols = new ArrayList<>(Arrays.asList("col_1","col_2","col_3","col_4"));
cols.add("id");
cols.add("name");
Column[] cols2 = cols.stream()
.map(s->new Column(s)).collect(Collectors.toList())
.toArray(new Column[0]);

settingsDataset.select(cols2).show();

关于java - 如何在数据框中动态地从列表中选择列加上固定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60932179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com