How to map JavaBean columsn with Casssandra table fields?

两盒软妹~` 提交于 2019-12-11 04:59:21

问题


I am using spark-sql.2.4.1v , datastax-java-cassandra-connector_2.11-2.4.1.jar and java8.

I have cassandra table like

create company(company_id int PRIMARY_KEY, company_name text);

JavaBean as below

@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
)

I have spark code below save the data into cassandra table.

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();

Giving error like below

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)

> Question:

Even though I am using annotation for mapping , why I am getting error ? How to fix this without changing the Java Bean field names ( i.e. from companyId to company_id) ?

来源:https://stackoverflow.com/questions/58592097/how-to-map-javabean-columsn-with-casssandra-table-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!