Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.savedata;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.commons.io.IOUtils;
- import org.apache.hadoop.hbase.client.Put;
- import org.apache.hadoop.hbase.util.Bytes;
- import com.alibaba.fastjson.JSONObject;
- import com.entity.Hotel;
- import com.entity.HotelComment;
- import com.util.HBaseUtil;
- public class SaveData {
- /**
- * 获取并保存酒店和城市数据
- */
- public static void saveCityAndHotelInfo() {
- /********** Begin **********/
- try {
- HBaseUtil.createTable("t_city_hotels_info", new String[] { "cityInfo", "hotel_info" });
- } catch (Exception e) {
- // 创建表失败
- e.printStackTrace();
- }
- List<Put> puts = new ArrayList<>();
- // 添加数据
- try {
- InputStream resourceAsStream = SaveData.class.getClassLoader().getResourceAsStream("aomen.txt");
- String readFileToString = IOUtils.toString(resourceAsStream, "UTF-8");
- List<Hotel> parseArray = JSONObject.parseArray(readFileToString, Hotel.class);
- String hongkong = IOUtils.toString(SaveData.class.getClassLoader().getResourceAsStream("hongkong.txt"),
- "UTF-8");
- List<Hotel> hongkongHotel = JSONObject.parseArray(hongkong, Hotel.class);
- parseArray.addAll(hongkongHotel);
- for (Hotel hotel : parseArray) {
- String cityId = hotel.getCity_id();
- String hotelId = hotel.getId();
- Put put = new Put(Bytes.toBytes(cityId + "_" + hotelId));
- // 添加city数据
- put.addColumn(Bytes.toBytes("cityInfo"), Bytes.toBytes("cityId"), Bytes.toBytes(cityId));
- put.addColumn(Bytes.toBytes("cityInfo"), Bytes.toBytes("cityName"),
- Bytes.toBytes(hotel.getCity_name()));
- put.addColumn(Bytes.toBytes("cityInfo"), Bytes.toBytes("pinyin"), Bytes.toBytes(hotel.getPinyin()));
- put.addColumn(Bytes.toBytes("cityInfo"), Bytes.toBytes("collectionTime"),
- Bytes.toBytes(hotel.getCollectionTime()));
- // 添加hotel数据
- put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("id"), Bytes.toBytes(hotel.getId()));
- put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("name"), Bytes.toBytes(hotel.getName()));
- put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("price"), Bytes.toBytes(String.valueOf(hotel.getPrice())));
- put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("lon"), Bytes.toBytes(String.valueOf(hotel.getLon())));
- put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("url"), Bytes.toBytes(hotel.getUrl()));
- put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("img"), Bytes.toBytes(hotel.getImg()));
- put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("address"), Bytes.toBytes(hotel.getAddress()));
- put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("score"), Bytes.toBytes(String.valueOf(hotel.getScore())));
- put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("dpscore"), Bytes.toBytes(String.valueOf(hotel.getDpscore())));
- put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("dpcount"), Bytes.toBytes(String.valueOf(hotel.getDpcount())));
- put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("star"), Bytes.toBytes(hotel.getStar()));
- put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("stardesc"),
- Bytes.toBytes(hotel.getStardesc()));
- put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("shortName"),
- Bytes.toBytes(hotel.getShortName()));
- put.addColumn(Bytes.toBytes("hotel_info"), Bytes.toBytes("isSingleRec"),
- Bytes.toBytes(hotel.getIsSingleRec()));
- puts.add(put);
- }
- // 批量保存数据
- HBaseUtil.putByTable("t_city_hotels_info", puts);
- } catch (Exception e) {
- e.printStackTrace();
- }
- /********** End **********/
- }
- /**
- * 获取和保存酒店的评论数据
- */
- public static void saveCommentInfo() {
- /********** Begin **********/
- /********** End **********/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement