Cannot reference field by field expression on GenericType<XXXX> and maven project setting log4j info level (level) and pojo example


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

solution:

①Create a new folder resources under src/main

② Create a new file log4j.properties in resources

③Add in 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 

④Add in 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.

Go to the POJO class under your project, modify it according to the above INFO information, and rerun it to solve the problem.

 

Pay attention, the above setting method is actually suitable for other maven projects, it may not necessarily be flink

Ok, finally give a POJO look (Flink approved pojo class)

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;
    }




}

 

Reference:

[1] Detailed explanation of org.apache.log4j.Logger


 

Guess you like

Origin blog.csdn.net/appleyuchi/article/details/108905419