Apache spark 从Spark Streaming中的字符串创建StructType

Apache spark 从Spark Streaming中的字符串创建StructType,apache-spark,apache-spark-sql,spark-dataframe,spark-streaming,Apache Spark,Apache Spark Sql,Spark Dataframe,Spark Streaming,在Spark structured Streaming中,我想从字符串创建StructType 在下面的示例中,spark read方法只接受模式的“Struct Type”,如何从字符串创建StructType。我想将employeeSchema字符串转换为StructType public static void main(String[] args) throws AnalysisException { String master = "local[*]"; SparkS

在Spark structured Streaming中,我想从字符串创建StructType

在下面的示例中,spark read方法只接受模式的“Struct Type”,如何从字符串创建StructType。我想将employeeSchema字符串转换为StructType

public static void main(String[] args) throws AnalysisException {
    String master = "local[*]";

    SparkSession sparkSession = SparkSession
            .builder().appName(EmployeeSchemaLoader.class.getName())
            .master(master).getOrCreate();

    String employeeSchema = "StructType(\n" +
            "StructField(firstName,StringType,true),\n" +
            "StructField(lastName,StringType,true),\n" +
            "StructField(addresses,\n" +
            "ArrayType(\n" +
            "StructType(\n" +
            "StructField(city,StringType,true), \n" +
            "StructField(state,StringType,true)\n" +
            "),\n" +
            "true),\n" +
            "true) \n" +
            ")";

    SparkContext context = sparkSession.sparkContext();
    context.setLogLevel("ERROR");
    SQLContext sqlCtx = sparkSession.sqlContext();
    Dataset<Row> employeeDataset = sparkSession.read()
            //.schema(employeeSchema)  // Accepts only Struct Type
            .json("simple_employees.json");

    employeeDataset.printSchema();
    employeeDataset.createOrReplaceTempView("employeeView");

    sparkSession.catalog().listTables().show();

    sqlCtx.sql("select * from employeeView").show();
publicstaticvoidmain(字符串[]args)引发异常{
字符串master=“local[*]”;
SparkSession SparkSession=SparkSession
.builder().appName(EmployeeSchemaLoader.class.getName())
.master(master).getOrCreate();
字符串employeeSchema=“StructType(\n”+
StructField(firstName,StringType,true),\n+
StructField(lastName,StringType,true),\n+
结构域(地址,\n+
“ArrayType(\n”+
“结构类型(\n”+
StructField(城市,StringType,true),\n+
StructField(状态,StringType,true)\n+
“”,\n“+
“正确),\n”+
“true)\n”+
")";
SparkContext上下文=sparkSession.SparkContext();
context.setLogLevel(“错误”);
SQLContext sqlCtx=sparkSession.SQLContext();
数据集employeeDataset=sparkSession.read()
//.schema(employeeSchema)//只接受结构类型
.json(“simple_employees.json”);
employeeDataset.printSchema();
employeeDataset.createOrReplaceTempView(“employeeView”);
sparkSession.catalog().listTables().show();
sql(“从employeeView中选择*).show();

我不知道您为什么要这样做。与其将employeeSchema设置为字符串,不如将其设置为StructType?如下所示:

StructType employeeSchema = StructType(
    StructField(firstName,StringType,true),
    StructField(lastName,StringType,true),
    StructField(addresses, ArrayType(StructType(
            StructField(city,StringType,true), 
            StructField(state,StringType,true)
    ), true), true) 

我不知道您为什么要这样做。与其将employeeSchema设置为字符串,不如将其设置为StructType?如下所示:

StructType employeeSchema = StructType(
    StructField(firstName,StringType,true),
    StructField(lastName,StringType,true),
    StructField(addresses, ArrayType(StructType(
            StructField(city,StringType,true), 
            StructField(state,StringType,true)
    ), true), true) 

如果它是字符串,那么它可以在程序启动时读取。如果你想这样做,那么创建一个数组[string]使用列名。然后,在数组中循环并将其转换为StructField,最后将其包装为StructType。这应该可以做到!我已经用Java编写了一个自定义的JSON到StructType程序…将在一段时间内将其放入Git…@Manjesh您可以共享到上面提到的GitHub页面的链接吗?谢谢!如果它是字符串,那么可以在程序启动时读取。如果要这样做,请创建一个数组[String]使用列名。然后,在数组中循环并将其转换为StructField,最后将其包装为StructType。这应该可以做到!我已经用Java编写了一个自定义JSON到StructType程序…将在一段时间内将其放入Git…@Manjesh你能分享到上面提到的GitHub页面的链接吗?谢谢!需要口头解释一个口头解释通常是有帮助的