给定列名列表,如何选择数据集的多列?

时间:2018-12-04 17:33:23

标签: java apache-spark apache-spark-sql

如何通过传递列表参数在Spark 2.3 Java中选择数据集ds的多列?

例如,这可以正常工作:

ds.select("col1","col2","col3").show();

但是,这失败了:

List<String> columns = Arrays.toList("col1","col2","col3");
ds.select(columns.toString()).show()

2 个答案:

答案 0 :(得分:1)

您必须使用spark 2.4.0List<String>转换为Seq<String>,并在Spark文档中使用selectExpr

如果要使用select,则必须从列表中删除第一列,并将其作为参数添加到select

请找到两个版本:

假设您具有以下.csv文件:

InvoiceNo,StockCode,Description,Quantity,InvoiceDate,UnitPrice,CustomerID,Country
536365,85123A,WHITE HANGING HEART T-LIGHT HOLDER,6,2010-12-01 08:26:00,2.55,17850.0,United Kingdom
536365,71053,WHITE METAL LANTERN,6,2010-12-01 08:26:00,3.39,17850.0,United Kingdom
536365,84406B,CREAM CUPID HEARTS COAT HANGER,8,2010-12-01 08:26:00,2.75,17850.0,United Kingdom
536365,84029G,KNITTED UNION FLAG HOT WATER BOTTLE,6,2010-12-01 08:26:00,3.39,17850.0,United Kingdom
536365,84029E,RED WOOLLY HOTTIE WHITE HEART.,6,2010-12-01 08:26:00,3.39,17850.0,United Kingdom

您可以使用以下代码解决问题:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

import java.util.Arrays;
import java.util.List;
import scala.collection.JavaConverters;
import scala.collection.Seq;


public class SparkJavaTest {
    public static SparkSession spark = SparkSession
            .builder()
            .appName("JavaSparkTest")
            .master("local")
            .getOrCreate();

    public static Seq<String> convertListToSeq(List<String> inputList) {
        return JavaConverters.asScalaIteratorConverter(inputList.iterator()).asScala().toSeq();
    }

    public static void main(String[] args) {
        Dataset<Row> ds = spark.read().option("header",true).csv("spark-file.csv");

        List<String> columns = Arrays.asList("InvoiceNo","StockCode","Description");
        //using selectExpr
        ds.selectExpr(convertListToSeq(columns)).show(false);

        //using select => this first column will be added to select
        List<String> columns2 = Arrays.asList("StockCode","Description");

        ds.select("InvoiceNo", convertListToSeq(columns2)).show(false);

    }
}

希望它会有所帮助:)

答案 1 :(得分:0)

Either使用

Dataset<Row>  select(String col, scala.collection.Seq<String> cols)

Column column = "col1";
List<String> columns = Arrays.toList(""col2","col3");
ds.select(column, columns).show()

or

 Dataset<Row> select(String col, String... cols)

List<Column> columns = Arrays.toList(col("col1"),col("col2"),col("col3"));
ds.select(columns);