尚硅谷大数据技术之电信客服

10) 创建类:CountDurationValue

package com.atguigu.analysis.kv.impl;

import com.atguigu.analysis.kv.base.BaseValue;

import java.io.DataInput;

import java.io.DataOutput;

import java.io.IOException;

public class CountDurationValue extends BaseValue {

    //某个维度通话次数总和

    private int callSum;

    //某个维度通话时间总和

    private int callDurationSum;

    public CountDurationValue() {

        super();

    }

    public CountDurationValue(int callSum, int callDurationSum) {

        super();

        this.callSum = callSum;

        this.callDurationSum = callDurationSum;

    }

    public int getCallSum() {

        return callSum;

    }

    public void setCallSum(int callSum) {

        this.callSum = callSum;

    }

    public int getCallDurationSum() {

        return callDurationSum;

    }

    public void setCallDurationSum(int callDurationSum) {

        this.callDurationSum = callDurationSum;

    }

    @Override

    public void write(DataOutput dataOutput) throws IOException {

        dataOutput.writeInt(this.callSum);

        dataOutput.writeInt(this.callDurationSum);

    }

    @Override

    public void readFields(DataInput dataInput) throws IOException {

        this.callSum = dataInput.readInt();

        this.callDurationSum = dataInput.readInt();

    }

}

11) 创建类:JDBCUtil

package com.atguigu.utils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import java.sql.*;

public class JDBCUtil {

    private static final Logger logger = LoggerFactory.getLogger(JDBCUtil.class);

    private static final String MYSQL_DRIVER_CLASS = "com.mysql.jdbc.Driver";

    private static final String MYSQL_URL = "jdbc:mysql://linux01:3306/db_telecom?useUnicode=true&characterEncoding=UTF-8";

    private static final String MYSQL_USERNAME = "root";

    private static final String MYSQL_PASSWORD = "000000";

    /**

     * 获取Mysql数据库的连接

     * @return

     */

    public static Connection getConnection() throws SQLException {

        try {

            Class.forName(MYSQL_DRIVER_CLASS);

        } catch (ClassNotFoundException e) {

            e.printStackTrace();

        }

        return DriverManager.getConnection(MYSQL_URL, MYSQL_USERNAME, MYSQL_PASSWORD);

    }

    /**

     * 关闭数据库连接释放资源

     * @param connection

     * @param statement

     * @param resultSet

     */

    public static void close(Connection connection, Statement statement, ResultSet resultSet){

        if(resultSet != null) try {

            resultSet.close();

        } catch (SQLException e) {

        }

        if(statement != null) try {

            statement.close();

        } catch (SQLException e) {

            e.printStackTrace();

        }

        if(connection != null) try {

            connection.close();

        } catch (SQLException e) {

            e.printStackTrace();

        }

    }

}

12) 创建类:JDBCCacheBean

package com.atguigu.utils;

import java.sql.Connection;

import java.sql.SQLException;

public class JDBCCacheBean {

    private static Connection conn = null;

    private JDBCCacheBean(){}

    public static Connection getInstance(){

        try {

            if(conn == null || conn.isClosed() || conn.isValid(3)){

                conn = JDBCUtil.getConnection();

            }

        } catch (SQLException e) {

            e.printStackTrace();

        }

        return conn;

    }

}