gpt4 book ai didi

java - Java中HierarchyStruc类中的ClassCastException如下

转载 作者:行者123 更新时间:2023-12-01 16:15:22 25 4
gpt4 key购买 nike

我在 Java 中遇到 ClassCastException 但无法解决。有人可以告诉我我在这里缺少什么吗?

这是我的类(class):

private List<Record> getGenericRecordsforHierarchy(int hID, Schema schema,List<HierarchyStruc> hs, int maxLevel) throws BookHierarchyException {        
List<GenericData.Record> recordList = new ArrayList<GenericData.Record>();
GenericData.Record record = new GenericData.Record(schema);
for (int i = 0; i < hs.size(); i++) {
record = new GenericData.Record(schema);
record.put("HierarchyId", hID);
for (int j = 0; j <= (maxLevel*3) && j < ((Constants.MAX_ALLOWED)*3); j++) {
int k=0;
record.put("Level" + (k+1) + "Id", hs.get(j));
record.put("Level" + (k+1) + "Desc", hs.get(j+1) );
record.put("Level" + (k+1) + "nodeId", hs.get(j+2) );
j= j+2;
k++;
if (j + 1 > (maxLevel*3) || null == hs.get(j+1)) {
record.put("parentNodeId", hs.get(i).getParentNodeId());
record.put("BookName", hs.get(i).getBookName());
record.put("HierarchyName", hs.get(i).getHierarchyName());
record.put("NodeDesc", hs.get(i).getNodeDesc());
break;
}
}
recordList.add(record);
}
return recordList;
}

我的 Generic.Record 类如下:

public static class Record implements GenericRecord, Comparable<Record> {
private final Schema schema;
private final Object[] values;
public Record(Schema schema) {
if (schema == null || !Type.RECORD.equals(schema.getType()))
throw new AvroRuntimeException("Not a record schema: "+schema);
this.schema = schema;
this.values = new Object[schema.getFields().size()];
}
public Record(Record other, boolean deepCopy) {
schema = other.schema;
values = new Object[schema.getFields().size()];
if (deepCopy) {
for (int ii = 0; ii < values.length; ii++) {
values[ii] = INSTANCE.deepCopy(
schema.getFields().get(ii).schema(), other.values[ii]);
}
}
else {
System.arraycopy(other.values, 0, values, 0, other.values.length);
}
}
@Override public Schema getSchema() { return schema; }
@Override public void put(String key, Object value) {
Schema.Field field = schema.getField(key);
if (field == null)
throw new AvroRuntimeException("Not a valid schema field: "+key);

values[field.pos()] = value;
}
@Override public void put(int i, Object v) { values[i] = v; }
@Override public Object get(String key) {
Field field = schema.getField(key);
if (field == null) return null;
return values[field.pos()];
}
@Override public Object get(int i) { return values[i]; }
@Override public boolean equals(Object o) {
if (o == this) return true; // identical object
if (!(o instanceof Record)) return false; // not a record
Record that = (Record)o;
if (!this.schema.equals(that.schema))
return false; // not the same schema
return GenericData.get().compare(this, that, schema, true) == 0;
}
@Override public int hashCode() {
return GenericData.get().hashCode(this, schema);
}
@Override public int compareTo(Record that) {
return GenericData.get().compare(this, that, schema);
}
@Override public String toString() {
return GenericData.get().toString(this);
}
}

以下方法出现错误:

private void writeToParquet(String hadoopPath, List<Record> recordList, Schema schema)
throws BookHierarchyException {
org.apache.hadoop.fs.Path path = new org.apache.hadoop.fs.Path(hadoopPath);
ParquetWriter<GenericData.Record> writer = null;

Configuration configuration = new Configuration(false);
configuration.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName());

try {
writer = AvroParquetWriter.<GenericData.Record>builder(path)
.withRowGroupSize(ParquetWriter.DEFAULT_BLOCK_SIZE).withPageSize(ParquetWriter.DEFAULT_PAGE_SIZE)
.withSchema(schema).withConf(new Configuration()).withCompressionCodec(CompressionCodecName.SNAPPY)
.withValidation(false).withDictionaryEncoding(false).build();

for (GenericData.Record record : recordList) {
writer.write(record);
}
log.info("File writing done. Closing file");
writer.close();
} catch (IOException e) {
log.error(e);
throw new BookHierarchyException("Error in file handling");
}
}

错误是:

java.lang.ClassCastException: HierarchyStruc cannot be cast to java.lang.CharSequence

有人可以让我知道我在这里做错了什么吗?我匹配了 HierarchyStruc 的架构类型和列类型,但它们也匹配。这似乎是一个不同的问题,我从早上开始就试图解决它。

java.lang.ClassCastException: com.package.beans.HierarchyStruc cannot be cast to java.lang.CharSequence
at org.apache.parquet.avro.AvroWriteSupport.fromAvroString(AvroWriteSupport.java:371)
at org.apache.parquet.avro.AvroWriteSupport.writeValueWithoutConversion(AvroWriteSupport.java:346)
at org.apache.parquet.avro.AvroWriteSupport.writeValue(AvroWriteSupport.java:278)
at org.apache.parquet.avro.AvroWriteSupport.writeRecordFields(AvroWriteSupport.java:191)
at org.apache.parquet.avro.AvroWriteSupport.write(AvroWriteSupport.java:165)
at org.apache.parquet.hadoop.InternalParquetRecordWriter.write(InternalParquetRecordWriter.java:128)
at org.apache.parquet.hadoop.ParquetWriter.write(ParquetWriter.java:299)
at com.package.utilities.ParquetFileHandler.writeToParquet(ParquetFileHandler.java:73)



public void persistHierarchyData(String dateYYYY_MM_DD, int hID, List<HierachyStruc> hs, int maxLevel)
throws BookHierarchyException {
Schema schema = parquetSchemaParser.parseSchema(avroSchemaName);
String hadoopPath = getUpdatedDatePattern(hadoopDirectory + hadoopFileName,dateYYYY_MM_DD);
hadoopPath = getUpdatedHierarchyPattern(hadoopPath, hID);
List<GenericData.Record> recordList = getGenericRecordsforHierarchy(hID, schema, hs,maxLevel);
log.info("recordList size is " + recordList.size());
parquetFileHandler.persist(schema, recordList, hadoopPath);
}

最佳答案

从你的代码中很难看出,因为它不完整。您提供了一个函数 getGenericRecordsforHierarchy,但没有在任何地方调用它。您尚未提供 ClassCastException 的堆栈跟踪,因此我们甚至不知道它发生在哪里。

也就是说,这很可疑:

record.put("Level" + (k+1) + "Id", hs.get(j));
record.put("Level" + (k+1) + "Desc", hs.get(j+1) );
record.put("Level" + (k+1) + "nodeId", hs.get(j+2) );

此处,您将整个 HierarchyStruc 实例添加到 GenericData.Record 中,但键表明它们应该是字符串。如果您将 Record 传递给期望字段为字符串的内容,那么它将抛出您所看到的异常。

如果您可以提供堆栈跟踪和跟踪中出现的类的源代码(如果可用),那么我可以给您更好的答案。

关于java - Java中HierarchyStruc类中的ClassCastException如下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62410042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com