如何用Casssandra表列映射JavaBean字段?

时间:2019-10-28 13:46:53

标签: cassandra apache-spark-sql datastax datastax-enterprise datastax-java-driver

我正在使用spark-sql.2.4.1v,datastax-java-cassandra-connector_2.11-2.4.1.jar和java8。

  

我有类似cassandra的桌子

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
)
  

我在下面有火花代码,将数据保存到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)

>问题:

即使我使用注解进行映射,为什么也会出错? 如何在不更改Java Bean字段名称的情况下解决此问题(即,从companyId更改为company_id)?

0 个答案:

没有答案