一、访问hbase
package com.example.storm.utils;
import com.example.storm.domain.MapBean;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/** * HBase操作工具类:Java工具类建议采用单例模式封装 */
public class HBaseUtils {
HBaseAdmin admin = null;
Configuration configuration = null;
/** * 私有改造方法 */
private HBaseUtils(){
configuration = new Configuration();
configuration.set("hbase.zookeeper.quorum", "192.168.48.138:2181");
configuration.set("hbase.rootdir", "hdfs://192.168.48.138:8020/hbase");
try {
admin = new HBaseAdmin(configuration);
} catch (IOException e) {
e.printStackTrace();
}
}
private static HBaseUtils instance = null;
public static synchronized HBaseUtils getInstance() {
if(null == instance) {
instance = new HBaseUtils();
}
return instance;
}
/** * 根据表名获取到HTable实例 */
public HTable getTable(String tableName) {
HTable table = null;
try {
table = new HTable(configuration, tableName);
} catch (IOException e) {
e.printStackTrace();
}
return table;
}
/* * @author qianliu on 2019/3/27 20:50 * @param tablename 表名 * @return 返回列名的查询出来的结果,返回全表 * @Discription: */
public List<MapBean> query(String tablename) throws IOException {
List<MapBean> list = new ArrayList<MapBean>();
HTable table = getTable(tablename);
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
//mapBean的lng和lat
MapBean mapBean = new MapBean();
String local = Bytes.toString(result.getRow());
String split[] = local.split(",");
mapBean.setLng(Double.parseDouble(split[0]));
mapBean.setLat(Double.parseDouble(split[1]));
//mapBean的count
mapBean.setCount(Long.parseLong(Bytes.toString(result.getValue(Bytes.toBytes(Config.HBaseColumn1),Bytes.toBytes(Config.col1_info1))))*1000);
list.add(mapBean);
}
return list;
}
}
查询出来的事务处理
package com.example.storm.service;
import com.example.storm.domain.MapBean;
import com.example.storm.utils.Config;
import com.example.storm.utils.HBaseUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/* * 从hbase中访问数据,并且将其格式化为MapBean * */
@Service
public class MapService {
//查询数据
public List<MapBean> query() throws IOException {
List<MapBean> list = null;
//取hbase中获取day中的所有课程以及课程访问量
list = HBaseUtils.getInstance().query(Config.HBaseTableName);
return list;
}
@Test
public void query_test() throws IOException {
List<MapBean> list = null;
//取hbase中获取day中的所有课程以及课程访问量
list = HBaseUtils.getInstance().query(Config.HBaseTableName);
System.out.println(list.get(0).getCount());
}
}
页面控制跳转
package com.example.storm.controller;
import com.example.storm.domain.MapBean;
import com.example.storm.service.MapService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.io.IOException;
import java.util.List;
@RestController
public class StateApp {
@Autowired
MapService mapService;
@RequestMapping(value = "/map",method = RequestMethod.GET,produces="text/plain;charset=UTF-8")
public ModelAndView map(){
return new ModelAndView("map");
}
@RequestMapping(value = "/map_state",method = RequestMethod.GET,produces="text/plain;charset=UTF-8")
public ModelAndView map_state() throws IOException {
ModelAndView mapstate = new ModelAndView("map_state");
//得到查询数据的json串
List<MapBean> mapBeans = mapService.query();
JSONArray jsonArray = JSONArray.fromObject(mapBeans);
mapstate.addObject("data_json",jsonArray);
return mapstate;
}
}
二 、测试
热力图
springboot源码:https://github.com/LUK-qianliu/springboot_storm
github源码:https://github.com/LUK-qianliu/stormtrian