From 22a32061919a1e583e27d7099f1c38c7036146e7 Mon Sep 17 00:00:00 2001 From: liuzhifei <2679431923@qq.com> Date: Thu, 1 Jul 2021 14:29:10 +0800 Subject: [PATCH 1/2] fix wrong word in the code --- .../src/main/java/com/demo/client/HbaseClient.java | 2 +- .../main/java/com/demo/map/ProductPortraitMapFunction.java | 4 ++-- .../src/main/java/com/demo/map/UserHistoryMapFunction.java | 4 ++-- .../com/demo/map/UserHistoryWithInterestMapFunction.java | 2 +- .../src/main/java/com/demo/map/UserPortraitMapFunction.java | 6 +++--- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/flink-2-hbase/src/main/java/com/demo/client/HbaseClient.java b/flink-2-hbase/src/main/java/com/demo/client/HbaseClient.java index 4448770..a4b5d24 100644 --- a/flink-2-hbase/src/main/java/com/demo/client/HbaseClient.java +++ b/flink-2-hbase/src/main/java/com/demo/client/HbaseClient.java @@ -116,7 +116,7 @@ public static void putData(String tablename, String rowkey, String famliyname,St * @param column 列名 * @throws Exception */ - public static void increamColumn(String tablename, String rowkey, String famliyname,String column) throws Exception { + public static void increaseColumn(String tablename, String rowkey, String famliyname,String column) throws Exception { String val = getData(tablename, rowkey, famliyname, column); int res = 1; if (val != null) { diff --git a/flink-2-hbase/src/main/java/com/demo/map/ProductPortraitMapFunction.java b/flink-2-hbase/src/main/java/com/demo/map/ProductPortraitMapFunction.java index 06ac65c..748b85f 100644 --- a/flink-2-hbase/src/main/java/com/demo/map/ProductPortraitMapFunction.java +++ b/flink-2-hbase/src/main/java/com/demo/map/ProductPortraitMapFunction.java @@ -21,9 +21,9 @@ public String map(String s) throws Exception { while (rst.next()){ String productId = String.valueOf(log.getProductId()); String sex = rst.getString("sex"); - HbaseClient.increamColumn("prod",productId,"sex",sex); + HbaseClient.increaseColumn("prod",productId,"sex",sex); String age = rst.getString("age"); - HbaseClient.increamColumn("prod",productId,"age", AgeUtil.getAgeType(age)); + HbaseClient.increaseColumn("prod",productId,"age", AgeUtil.getAgeType(age)); } } return null; diff --git a/flink-2-hbase/src/main/java/com/demo/map/UserHistoryMapFunction.java b/flink-2-hbase/src/main/java/com/demo/map/UserHistoryMapFunction.java index 173d65b..e054344 100644 --- a/flink-2-hbase/src/main/java/com/demo/map/UserHistoryMapFunction.java +++ b/flink-2-hbase/src/main/java/com/demo/map/UserHistoryMapFunction.java @@ -20,8 +20,8 @@ public class UserHistoryMapFunction implements MapFunction { public String map(String s) throws Exception { LogEntity log = LogToEntity.getLog(s); if (null != log){ - HbaseClient.increamColumn("u_history",String.valueOf(log.getUserId()),"p",String.valueOf(log.getProductId())); - HbaseClient.increamColumn("p_history",String.valueOf(log.getProductId()),"p",String.valueOf(log.getUserId())); + HbaseClient.increaseColumn("u_history",String.valueOf(log.getUserId()),"p",String.valueOf(log.getProductId())); + HbaseClient.increaseColumn("p_history",String.valueOf(log.getProductId()),"p",String.valueOf(log.getUserId())); } return ""; } diff --git a/flink-2-hbase/src/main/java/com/demo/map/UserHistoryWithInterestMapFunction.java b/flink-2-hbase/src/main/java/com/demo/map/UserHistoryWithInterestMapFunction.java index 9b0c4c9..44f5afe 100644 --- a/flink-2-hbase/src/main/java/com/demo/map/UserHistoryWithInterestMapFunction.java +++ b/flink-2-hbase/src/main/java/com/demo/map/UserHistoryWithInterestMapFunction.java @@ -71,7 +71,7 @@ private void saveToHBase(LogEntity log, int times) throws Exception { if (log != null){ for (int i = 0; i < times; i++) { - HbaseClient.increamColumn("u_interest",String.valueOf(log.getUserId()),"p",String.valueOf(log.getProductId())); + HbaseClient.increaseColumn("u_interest",String.valueOf(log.getUserId()),"p",String.valueOf(log.getProductId())); } } diff --git a/flink-2-hbase/src/main/java/com/demo/map/UserPortraitMapFunction.java b/flink-2-hbase/src/main/java/com/demo/map/UserPortraitMapFunction.java index d8765eb..b4d040f 100644 --- a/flink-2-hbase/src/main/java/com/demo/map/UserPortraitMapFunction.java +++ b/flink-2-hbase/src/main/java/com/demo/map/UserPortraitMapFunction.java @@ -21,11 +21,11 @@ public String map(String s) throws Exception { String userId = String.valueOf(log.getUserId()); String country = rst.getString("country"); - HbaseClient.increamColumn("user",userId,"country",country); + HbaseClient.increaseColumn("user",userId,"country",country); String color = rst.getString("color"); - HbaseClient.increamColumn("user",userId,"color",color); + HbaseClient.increaseColumn("user",userId,"color",color); String style = rst.getString("style"); - HbaseClient.increamColumn("user",userId,"style",style); + HbaseClient.increaseColumn("user",userId,"style",style); } } From fc1f6bb9f36989dfdcdd2bf2977d9c73412b20ff Mon Sep 17 00:00:00 2001 From: liuzhifei <2679431923@qq.com> Date: Fri, 2 Jul 2021 10:07:39 +0800 Subject: [PATCH 2/2] fix the UserHistoryWithInterestMapFunction bugs with state and saveToHbase two times --- .../com/demo/map/UserHistoryWithInterestMapFunction.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/flink-2-hbase/src/main/java/com/demo/map/UserHistoryWithInterestMapFunction.java b/flink-2-hbase/src/main/java/com/demo/map/UserHistoryWithInterestMapFunction.java index 44f5afe..35b3a31 100644 --- a/flink-2-hbase/src/main/java/com/demo/map/UserHistoryWithInterestMapFunction.java +++ b/flink-2-hbase/src/main/java/com/demo/map/UserHistoryWithInterestMapFunction.java @@ -38,12 +38,13 @@ public String map(LogEntity logEntity) throws Exception { int times = 1; // 如果用户没有操作 则为state创建值 if (actionLastTime == null) { - actionLastTime = actionThisTime; + //actionLastTime = actionThisTime; + state.update(actionThisTime); saveToHBase(logEntity, 1); }else{ times = getTimesByRule(actionLastTime, actionThisTime); + saveToHBase(logEntity, times); } - saveToHBase(logEntity, times); // 如果用户的操作为3(购物),则清除这个key的state if (actionThisTime.getType().equals("3")){