Cannot reference field by field expression on GenericType<XXXX>与maven工程设置log4j的info等级(level)以及pojo样例

本文介绍了解决 Apache Flink 中因 POJO 类不符合规范而导致的 InvalidFieldReferenceException 的详细步骤。通过调整 POJO 类的设计及配置 log4j,确保所有字段都能正确访问。


Exception in thread "main" org.apache.flink.api.common.typeutils.CompositeType$InvalidFieldReferenceException: Cannot reference field by field expression on GenericType<UserActionLogPOJO>Field expressions are only supported on POJO types, tuples, and case classes. (See the Flink documentation on what is considered a POJO.)
    at org.apache.flink.streaming.util.typeutils.FieldAccessorFactory.getAccessor(FieldAccessorFactory.java:193)
    at org.apache.flink.streaming.api.functions.aggregation.ComparableAggregator.<init>(ComparableAggregator.java:67)
    at org.apache.flink.streaming.api.datastream.KeyedStream.max(KeyedStream.java:836)
    at Aggregate.main(Aggregate.java:52)

Process finished with exit code 1

解决方案:

①src/main下面新建文件夹resources

②resources中新建文件log4j.properties

③log4j.properties中加入:

# 可以设置级别: debug>info>error

#debug :显示 debug 、 info 、 error

#info :显示 info 、 error

#error :只 error

# 也就是说只显示比大于等于当前级别的信息

log4j.rootLogger=info,appender1  

#log4j.rootLogger=info,appender1

#log4j.rootLogger=error,appender1

# 输出到控制台

log4j.appender.appender1=org.apache.log4j.ConsoleAppender  

# 样式为 TTCCLayout

log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout 

④pom.xml中加入:

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
⑤重新运行代码,我们会看到:

[main] INFO org.apache.flink.api.java.typeutils.TypeExtractor - class UserActionLogPOJO does not contain a getter for field productPrice
[main] INFO org.apache.flink.api.java.typeutils.TypeExtractor - Class class UserActionLogPOJO cannot be used as a POJO type because not all fields are valid POJO fields, and must be processed as GenericType. Please read the Flink documentation on "Data Types & Serialization" for details of the effect on performance.

去你的工程下面的POJO类中,根据上述INFO信息进行修改,再重新运行即可解决该问题.

 

注意哈,上述设置办法其实也适合于其他maven工程,可以不一定是flink

好了,最后给个POJO的样子(Flink认可的pojo类)

例子①

import java.io.Serializable;



public class UserActionLogPOJO implements Serializable
{
    private String userId; //用户id
    private String itemId; //商品分类id
    private int productPrice; //商品价格


    public UserActionLogPOJO (String userId,String itemId,int productPrice)
    {
        this.userId=userId;
        this.itemId=itemId;
        this.productPrice=productPrice;
    }
    /** default constructor */
    public UserActionLogPOJO ()
    {
    }
    public String getUserID()
    {
        return this.userId;
    }




    public void setProductID(String itemId)
    {
        this.itemId=itemId;
    }



    //-----------------------------------
    public String getItemId() {
        return itemId;
    }

    public void setItemId(String itemId) {
        this.itemId = itemId;
    }

    //-----------------------------------


    public void setProductPrice(int price)
    {
        this.productPrice=productPrice;

    }


    public int getproductPrice() {
        return productPrice;
    }


//-----------------------------------



//-----------------------------------


    public void setUserId(String userId) {
        this.userId = userId;
    }



    public String getUserId() {
        return userId;
    }



    public String toString()
    {

        return "userId="+userId+","+"price="+productPrice;
    }




}

 

例子②

public class Sales {

 private String transacitonId;
 private Integer customerId;
 private Integer itemId;
 private Double amountPaid;


 public Sales(String transacitonId, Integer customerId, Integer itemId, Double amountPaid) {
  this.transacitonId = transacitonId;
  this.customerId = customerId;
  this.itemId = itemId;
  this.amountPaid = amountPaid;
 }


 public Sales ()
 {
 }


 public String getTransacitonId() {
  return transacitonId;
 }

 public void setTransacitonId(String transacitonId) {
  this.transacitonId = transacitonId;
 }

 public Integer getCustomerId() {
  return customerId;
 }

 public void setCustomerId(Integer customerId) {
  this.customerId = customerId;
 }

 public Integer getItemId() {
  return itemId;
 }

 public void setItemId(Integer itemId) {
  this.itemId = itemId;
 }

 public Double getAmountPaid() {
  return amountPaid;
 }

 public void setAmountPaid(Double amountPaid) {
  this.amountPaid = amountPaid;
 }

 public String toString()
 {

  return "transacitonId="+transacitonId+","+"customerId="+customerId;
 }

}

--------------------------------如果使用lombok------------------------------------------

例子 一、

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Data;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class abcd {

    private String xxxx;

}

例子二、

import lombok.Data;

@Data
public class xxxx
{
}

 

 

 

Reference:

[1]org.apache.log4j.Logger详解


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值