如何将Spark Row的数据集转换为字符串? [英] How to convert the datasets of Spark Row into string?

查看:1399
本文介绍了如何将Spark Row的数据集转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了使用SparkSQL访问Hive表的代码.这是代码:

I have written the code to access the Hive table using SparkSQL. Here is the code:

SparkSession spark = SparkSession
        .builder()
        .appName("Java Spark Hive Example")
        .master("local[*]")
        .config("hive.metastore.uris", "thrift://localhost:9083")
        .enableHiveSupport()
        .getOrCreate();
Dataset<Row> df =  spark.sql("select survey_response_value from health").toDF();
df.show();

我想知道如何将完整的输出转换为String或String数组?当我尝试与另一个只能传递String或String type Array值的模块一起使用时.
我尝试了其他方法,例如.toString或类型转换为String值.但是对我没有用.
请让我知道如何将DataSet值转换为String?

I would like to know how I can convert the complete output to String or String array? As I am trying to work with another module where only I can pass String or String type Array values.
I have tried other methods like .toString or typecast to String values. But did not worked for me.
Kindly let me know how I can convert the DataSet values to String?

推荐答案

以下是Java中的示例代码.

Here is the sample code in Java.

public class SparkSample {
    public static void main(String[] args) {
        SparkSession spark = SparkSession
            .builder()
            .appName("SparkSample")
            .master("local[*]")
            .getOrCreate();
    //create df
    List<String> myList = Arrays.asList("one", "two", "three", "four", "five");
    Dataset<Row> df = spark.createDataset(myList, Encoders.STRING()).toDF();
    df.show();
    //using df.as
    List<String> listOne = df.as(Encoders.STRING()).collectAsList();
    System.out.println(listOne);
    //using df.map
    List<String> listTwo = df.map(row -> row.mkString(), Encoders.STRING()).collectAsList();
    System.out.println(listTwo);
  }
}

行"是Java 8 lambda参数.请检查 developer.com/java/start-using-java-lambda -expressions.html

"row" is java 8 lambda parameter. Please check developer.com/java/start-using-java-lambda-expressions.html

这篇关于如何将Spark Row的数据集转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆