目录
UserVisitSessionAnalyzeSpark.java
本片文章记录用户访问session分析-top10热门品类之获取top10品类并写入MySQL。
代码
创建top10_category表的实体类以及工厂方法
domain
Top10Category.java
package graduation.java.domain; /** * FileName: Top10Category * Author: hadoop * Email: 3165845957@qq.com * Date: 19-3-23 上午10:34 * Description: * * top10品类实例 */ public class Top10Category { private long taskid; private long categoryid; private long clickCount; private long orderCount; private long payCount; public long getTaskid() { return taskid; } public void setTaskid(long taskid) { this.taskid = taskid; } public long getCategoryid() { return categoryid; } public void setCategoryid(long categoryid) { this.categoryid = categoryid; } public long getClickCount() { return clickCount; } public void setClickCount(long clickCount) { this.clickCount = clickCount; } public long getOrderCount() { return orderCount; } public void setOrderCount(long orderCount) { this.orderCount = orderCount; } public long getPayCount() { return payCount; } public void setPayCount(long payCount) { this.payCount = payCount; } @Override public String toString() { return "Top10Category{" + "taskid=" + taskid + ", categoryid=" + categoryid + ", clickCount=" + clickCount + ", orderCount=" + orderCount + ", payCount=" + payCount + '}'; } }
dao
ITop10CategoryDAO.java
package graduation.java.dao; import graduation.java.domain.Top10Category; /** * FileName: ITop10CategoryDAO * Author: hadoop * Email: 3165845957@qq.com * Date: 19-3-23 上午10:38 * Description: * * Top10品类DAO * */ public interface ITop10CategoryDAO { /** * 插入点击、下单、支付前十的品类 * @param top10Category */ void insert(Top10Category top10Category); }
impl
Top10CategoryDAOImpl.java
package graduation.java.impl; import graduation.java.dao.ITop10CategoryDAO; import graduation.java.domain.Top10Category; import graduation.java.jdbc.JDBCHelper; /** * FileName: Top10CategoryImpl * Author: hadoop * Email: 3165845957@qq.com * Date: 19-3-23 上午10:41 * Description: * * Top10品类DAO接口实现 */ public class Top10CategoryDAOImpl implements ITop10CategoryDAO { /** * 插入点击、下单、支付前十名的品类 * @param top10Category */ @Override public void insert(Top10Category top10Category) { String sql = "insert into top10_category values(?,?,?,?,?)"; Object[] param = new Object[]{ top10Category.getTaskid(), top10Category.getCategoryid(), top10Category.getClickCount(), top10Category.getOrderCount(), top10Category.getPayCount() }; JDBCHelper jdbcHelper = JDBCHelper.getInstance(); jdbcHelper.executeUpdate(sql,param); } }
factory
DAOFactory.java
/** * 获取top10品类管理DAO * @return */ public static Top10CategoryDAOImpl getTop10CategoryDAO(){ return new Top10CategoryDAOImpl(); }
test
Top10CategoryTest.java
package graduation.java.test; import graduation.java.dao.ITop10CategoryDAO; import graduation.java.domain.Top10Category; import graduation.java.factory.DAOFactory; /** * FileName: Top10CategoryTest * Author: hadoop * Email: 3165845957@qq.com * Date: 19-3-23 上午10:48 * Description: */ public class Top10CategoryTest { public static void main(String[] args){ Top10Category top10Category = new Top10Category(); ITop10CategoryDAO top10CategoryDAO = DAOFactory.getTop10CategoryDAO(); long taskid = 3L; long categoryid = 132L; long clickCount = 1000L; long orderCount = 100L; long payCount = 19L; top10Category.setTaskid(taskid); top10Category.setCategoryid(categoryid); top10Category.setClickCount(clickCount); top10Category.setOrderCount(orderCount); top10Category.setPayCount(payCount); top10CategoryDAO.insert(top10Category); } }
测试结果
插入Top10品类
spark
UserVisitSessionAnalyzeSpark.java
/** * 第六步:用take(10)取出top10热门品类,并写入MySQL */ ITop10CategoryDAO top10CategoryDAO = DAOFactory.getTop10CategoryDAO(); List<Tuple2<CategorySortKey,String>> top10CategoryList = sortedCategoryCountRDD.take(10); for (Tuple2<CategorySortKey,String> tuple : top10CategoryList){ String countInfo = tuple._2; long categoryid = Long.valueOf(StringUtils.getFieldFromConcatString(countInfo,"\\|", Constants.FIELD_CATEGORY_ID)); long clickCount = Long.valueOf(StringUtils.getFieldFromConcatString(countInfo,"\\|", Constants.FIELD_CLICK_COUNT)); long orderCount = Long.valueOf(StringUtils.getFieldFromConcatString(countInfo,"\\|", Constants.FIELD_ORDER_COUNT)); long payCount = Long.valueOf(StringUtils.getFieldFromConcatString(countInfo,"\\|", Constants.FIELD_PAY_COUNT)); Top10Category top10Category = new Top10Category(); top10Category.setTaskid(taskid); top10Category.setCategoryid(categoryid); top10Category.setClickCount(clickCount); top10Category.setOrderCount(orderCount); top10Category.setPayCount(payCount); top10CategoryDAO.insert(top10Category); }