如何用cassandra表列Mapjavabean字段?

gg58donl  于 2021-06-10  发布在  Cassandra
关注(0)|答案(0)|浏览(304)

我使用的是spark-sql.2.4.1v、datastax-java-cassandra-connector_2.11-2.4.1.jar和java8。
我有Cassandra的table

create company(company_id int PRIMARY_KEY, company_name text);

javabean如下

@Table(name = "company")
class CompanyRecord(
 @PartitionKey(0)
 @Column(name="company_id")
 Integer companyId;
@Column(name="company_name")
 String companyName;
//getter and setters
//default & parametarized constructors
)

我有下面的Spark代码保存到Cassandra表的数据。

Dataset<Row> latestUpdatedDs = joinUpdatedRecordsDs.select("company_id", "company_name"); /// select from other source like xls sheet

Encoder<CompanyRecord> comanyEncoder =  Encoders.bean(CompanyRecord.class);         
Dataset<CompanyRecord> inputDs = latestUpdatedDs.as(comanyEncoder );

 inputDs 
        .write()
        .format("org.apache.spark.sql.cassandra")
        .option("table","company")
        .option("keyspace",  "ks_one")
        .mode(SaveMode.Append)
        .save();

给出如下错误

ERROR org.apache.spark.sql.catalyst.expressions.codegen.CodeGenerator - failed to compile: org.codehaus.commons.compiler.CompileException: File 'generated.java', Line 176, Column 75: A method named "toString" is not declared in any enclosing class nor any supertype, nor through a static import
org.codehaus.commons.compiler.CompileException: File 'generated.java', Line 176, Column 75: A method named "toString" is not declared in any enclosing class nor any supertype, nor through a static import
    at org.codehaus.janino.UnitCompiler.compileError(UnitCompiler.java:12124)

 Exception in thread "main" java.util.NoSuchElementException: Columns not found in table ks_one.company: 
 companyId, companyName
    at com.datastax.spark.connector.SomeColumns.selectFrom(ColumnSelector.scala:44)
    at com.datastax.spark.connector.writer.TableWriter$.apply(TableWriter.scala:385)

问题:
即使我使用注解进行Map,为什么会出现错误?如何在不改变javabean字段名(即从companyid到companyid)的情况下解决这个问题?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题