Advertisement
FancyKing

第2关:保存酒店评论信息

Apr 3rd, 2020
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.02 KB | None | 0 0
  1. package com.savedata;
  2.  
  3. import java.io.InputStream;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import org.apache.commons.io.IOUtils;
  7. import org.apache.hadoop.hbase.client.Put;
  8. import org.apache.hadoop.hbase.util.Bytes;
  9. import com.alibaba.fastjson.JSONObject;
  10. import com.entity.Hotel;
  11. import com.entity.HotelComment;
  12. import com.util.HBaseUtil;
  13.  
  14. public class SaveData {
  15.     /**
  16.      * 获取并保存酒店和城市数据
  17.      */
  18.     public static void saveCityAndHotelInfo() {
  19.         /**********   Begin   **********/      
  20.  
  21.         try {
  22.             HBaseUtil.createTable("t_city_hotels_info", new String[] { "cityInfo", "hotel_info" });
  23.         } catch (Exception e) {
  24.             // 创建表失败
  25.             e.printStackTrace();
  26.         }
  27.         List<Put> puts = new ArrayList<>();
  28.         // 添加数据
  29.         try {
  30.             InputStream resourceAsStream = SaveData.class.getClassLoader().getResourceAsStream("aomen.txt");
  31.             String readFileToString = IOUtils.toString(resourceAsStream, "UTF-8");
  32.             List<Hotel> parseArray = JSONObject.parseArray(readFileToString, Hotel.class);
  33.             String hongkong = IOUtils.toString(SaveData.class.getClassLoader().getResourceAsStream("hongkong.txt"),
  34.                     "UTF-8");
  35.             List<Hotel> hongkongHotel = JSONObject.parseArray(hongkong, Hotel.class);
  36.             parseArray.addAll(hongkongHotel);
  37.             for (Hotel hotel : parseArray) {
  38.                 String cityId = hotel.getCity_id();
  39.                 String hotelId = hotel.getId();
  40.                 Put put = new Put(Bytes.toBytes(cityId + "_" + hotelId));
  41.                 // 添加city数据
  42.                 put.addColumn(Bytes.toBytes("cityInfo"), Bytes.toBytes("cityId"), Bytes.toBytes(cityId));
  43.                 put.addColumn(Bytes.toBytes("cityInfo"), Bytes.toBytes("cityName"),
  44.                         Bytes.toBytes(hotel.getCity_name()));
  45.                 put.addColumn(Bytes.toBytes("cityInfo"), Bytes.toBytes("pinyin"), Bytes.toBytes(hotel.getPinyin()));
  46.                 put.addColumn(Bytes.toBytes("cityInfo"), Bytes.toBytes("collectionTime"),
  47.                         Bytes.toBytes(hotel.getCollectionTime()));
  48.                 // 添加hotel数据
  49.                 put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("id"), Bytes.toBytes(hotel.getId()));
  50.                 put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("name"), Bytes.toBytes(hotel.getName()));
  51.                 put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("price"), Bytes.toBytes(String.valueOf(hotel.getPrice())));
  52.                 put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("lon"), Bytes.toBytes(String.valueOf(hotel.getLon())));
  53.                 put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("url"), Bytes.toBytes(hotel.getUrl()));
  54.                 put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("img"), Bytes.toBytes(hotel.getImg()));
  55.                 put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("address"), Bytes.toBytes(hotel.getAddress()));
  56.                 put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("score"), Bytes.toBytes(String.valueOf(hotel.getScore())));
  57.                 put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("dpscore"), Bytes.toBytes(String.valueOf(hotel.getDpscore())));
  58.                 put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("dpcount"), Bytes.toBytes(String.valueOf(hotel.getDpcount())));
  59.                 put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("star"), Bytes.toBytes(hotel.getStar()));
  60.                 put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("stardesc"),
  61.                         Bytes.toBytes(hotel.getStardesc()));
  62.                 put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("shortName"),
  63.                         Bytes.toBytes(hotel.getShortName()));
  64.                 put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("isSingleRec"),
  65.                         Bytes.toBytes(hotel.getIsSingleRec()));
  66.                 puts.add(put);
  67.             }
  68.             // 批量保存数据
  69.             HBaseUtil.putByTable("t_city_hotels_info", puts);
  70.         } catch (Exception e) {
  71.             e.printStackTrace();
  72.         }
  73.        
  74.        
  75.         /**********   End   **********/      
  76.     }
  77.    
  78.     /**
  79.      * 获取和保存酒店的评论数据
  80.      */
  81.     public static void saveCommentInfo() {
  82.         /**********   Begin   **********/
  83.          try {
  84.             HBaseUtil.createTable("t_hotel_comment", new String[] { "hotel_info", "comment_info" });
  85.         } catch (Exception e) {
  86.             // 创建表失败
  87.             e.printStackTrace();
  88.         }
  89.         InputStream resourceAsStream = SaveData.class.getClassLoader().getResourceAsStream("comment.txt");
  90.         try {
  91.         String readFileToString = IOUtils.toString(resourceAsStream, "UTF-8");
  92.         List<HotelComment> otherCommentListByPage = JSONObject.parseArray(readFileToString, HotelComment.class);
  93.         // 获取数据
  94.         List<Put> puts = new ArrayList<>();
  95.         // 定义Put对象
  96.         for (HotelComment comment : otherCommentListByPage) {
  97.             Put put = new Put((comment.getHotel_id()  + "_" + comment.getId()).getBytes());
  98.             put.addColumn("hotel_info".getBytes(), "hotel_name".getBytes(),
  99.                     comment.getHotel_name().getBytes());
  100.             put.addColumn("hotel_info".getBytes(), "hotel_id".getBytes(), comment.getHotel_id().getBytes());
  101.             // 数据量很大在这里只保存用作分析的数据
  102.             put.addColumn("comment_info".getBytes(), "id".getBytes(), Bytes.toBytes(String.valueOf(comment.getId())));
  103.             put.addColumn("comment_info".getBytes(), "baseRoomId".getBytes(), Bytes.toBytes(String.valueOf(comment.getBaseRoomId())));
  104.             if (comment.getBaseRoomId() != -1 && comment.getBaseRoomName() != null) {
  105.                 put.addColumn("comment_info".getBytes(), "baseRoomName".getBytes(),
  106.                         Bytes.toBytes(comment.getBaseRoomName()));
  107.             }
  108.             put.addColumn("comment_info".getBytes(), "checkInDate".getBytes(), Bytes.toBytes(comment.getCheckInDate()));
  109.             put.addColumn("comment_info".getBytes(), "postDate".getBytes(), Bytes.toBytes(comment.getPostDate()));
  110.             put.addColumn("comment_info".getBytes(), "content".getBytes(), Bytes.toBytes(comment.getContent()));
  111.             put.addColumn("comment_info".getBytes(), "highlightPosition".getBytes(),
  112.                     Bytes.toBytes(comment.getHighlightPosition()));
  113.             put.addColumn("comment_info".getBytes(), "hasHotelFeedback".getBytes(),
  114.                     Bytes.toBytes(String.valueOf(comment.getHasHotelFeedback())));
  115.             put.addColumn("comment_info".getBytes(), "userNickName".getBytes(),
  116.                     Bytes.toBytes(comment.getUserNickName()));
  117.             puts.add(put);
  118.         }
  119.             // 上传数据
  120.             HBaseUtil.putByTable("t_hotel_comment", puts);
  121.         } catch (Exception e) {
  122.             e.printStackTrace();
  123.         }
  124.          
  125.          
  126.          
  127.         /**********   End   **********/
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement