0
Spark構造化ストリーミングでSTRINGからStructTypeを作成したいとします。Spark StreamingでStringからStructTypeを作成
以下の例では、spark読み取りメソッドはスキーマの「構造型」のみを受け入れますが、どのようにしてStringからStructTypeを作成できますか。私はemployeeSchema Stringを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();
文字列内にある場合は、プログラムの起動時に読み取ることができます。 – Manjesh
このようにしたい場合は、列名で配列[String]を作成します。その後、配列をループしてStructFieldに変換し、最後にStructTypeでラップします。それはトリックを行う必要があります! –
JavaのStructTypeプログラムにカスタムJSONを書きました...しばらくのうちにGitに入れます – Manjesh