1

I want to create a StructType dynamically out of a json file. I iterate my fields and I want to find out how (if it is even possible) can I create some list with my iteration, and then to create a StructType from it.

The code I've tried:

List<StructField> structFieldList = new ArrayList<>();
for (String field : fields.values()) {
  StructField sf = Datatypes.createStructField(field, DataTypes.StringType, true);
  structFieldList.add(sf);
}
StructType structType = new StructType(structFieldList.toArray());

But this one is pretty impossible. Is there any way to figure this out?

1

1 Answer 1

1

Here you don't need to convert ArrayList to Scala Array, as StructType constructor takes java StructField[] array as argument.

Your code can be changed by setting type when calling .toArray() method in your last line of code snippet so it returns a StructField[] array instead of an Object[] array:

List<StructField> structFieldList = new ArrayList<>();
for (String field : fields.values()) {
  StructField sf = DataTypes.createStructField(field, DataTypes.StringType, true);
  structFieldList.add(sf);
}
StructType structType = new StructType(structFieldList.toArray(new StructField[0]));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.