05 ssm框架访问操作hbase

web端采用ssm框架进行对hbase操作。

配置文件:

05 ssm框架访问操作hbase_第1张图片

beans.xml:

 



    
    
        
            
        
    

    
    
        
    

    
    

    
    
        
        
        
        
        
        
        
        
    

    
    
        
        
        
        
    

    
    
        
    

mybatis-config.xml:

 




    

        
    
    
    

        
    

PersonMapper.xml:

 




    
    
    
        insert into persons(name , phone) values(#{name},#{phone})
    

dispatcher-servlet.xml:

 



    
    
    
    

    
    
    
    


    
    
        
        
    

web.xml:

 



    
    
        contextConfigLocation
        classpath*:beans.xml
    
    
    
        org.springframework.web.context.ContextLoaderListener
    

    
        characterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceEncoding
            true
        
    
    
        characterEncodingFilter
        /*
    


    
    
        dispatcher
        org.springframework.web.servlet.DispatcherServlet
    
    
        dispatcher
        /
    

 

创建PersonService:

 

 

 */
public interface PersonService extends BaseService {
    public String selectNameByPhone(String phone);
}

创建PersonServiceImpl:

 

@Service("personService")
public class PersonServiceImpl extends BaseServiceImpl implements PersonService{

    @Resource(name="userDao")
    public void setDao(BaseDao dao) {
        super.setDao(dao);
    }

    public String selectNameByPhone(String phone){
        return ((PersonDaoImpl)getDao()).selectNameByPhone(phone) ;
    }
}

创建PersonDaoImpl:

 

@Repository("personDao")
public class PersonDaoImpl extends SqlSessionDaoSupport implements BaseDao {
    public void insert(Person person) {
        getSqlSession().insert("persons.insert",person) ;
    }

    public void update(Person person) {

    }

    public void delete(Integer id) {

    }

    public Person selectOne(Integer id) {
        return null;
    }

    public List selectAll() {
        return getSqlSession().selectList("persons.selectAll");
    }

    public List selectPage(int offset, int len) {
        return null;
    }

    public int selectCount() {
        return 0;
    }

    //按照号码查询用户
    public String selectNameByPhone(String phone){
        return getSqlSession().selectOne("persons.selectNameByPhone",phone);
    }
}

创建CallLogService:

 

public interface CallLogService {
    public List findAll();

    /**
     * 按照范围查询通话记录
     */
    public List findCallogs(String call,List list);
}

创建CallLogServiceImpl:

 

**
 * 呼叫日志
 */
@Service("callLogService")
public class CallLogServiceImpl implements CallLogService{

    @Resource(name="personServiceCache")
    private PersonService ps ;

    private Table table ;
    public CallLogServiceImpl(){
        try {
            Configuration conf = HBaseConfiguration.create();
            Connection conn = ConnectionFactory.createConnection(conf);
            TableName name = TableName.valueOf("ns1:calllogs");
            table = conn.getTable(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *查询所有log
     */
    public List findAll() {
        List list = new ArrayList();
        try {
            Scan scan = new Scan();
            ResultScanner rs = table.getScanner(scan);
            Iterator it = rs.iterator();
            byte[] f = Bytes.toBytes("f1");

            byte[] caller = Bytes.toBytes("caller");
            byte[] callee = Bytes.toBytes("callee");
            byte[] callTime = Bytes.toBytes("callTime");
            byte[] callDuration = Bytes.toBytes("callDuration");
            CallLog log = null ;
            while(it.hasNext()){
                log = new CallLog();
                Result r = it.next();
                //TODO 设置用户名
                String callerStr = Bytes.toString(r.getValue(f, caller)) ;
                log.setCaller(callerStr);
                log.setCallerName(ps.selectNameByPhone(callerStr));
                System.out.println(ps.selectNameByPhone(callerStr));
                //TODO 设置用户名
                String calleeStr = Bytes.toString(r.getValue(f, callee)) ;
                log.setCallee(calleeStr);
                log.setCalleeName(ps.selectNameByPhone(calleeStr));
                System.out.println(ps.selectNameByPhone(calleeStr));
                log.setCallTime(Bytes.toString(r.getValue(f, callTime)));
                log.setCallDuration(Bytes.toString(r.getValue(f, callDuration)));
                list.add(log);
            }

            return list ;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 按照范围查询通话记录
     */
    public List findCallogs(String call , List ranges){
        List logs = new ArrayList();
        try {
            for(CallLogRange range : ranges){
                Scan scan = new Scan();
                //设置扫描起始行
                scan.setStartRow(Bytes.toBytes(CallLogUtil.getStartRowkey(call, range.getStartPoint(),100)));
                //设置扫描结束行
                scan.setStopRow(Bytes.toBytes(CallLogUtil.getStopRowkey(call, range.getStartPoint(), range.getEndPoint(),100)));

                ResultScanner rs = table.getScanner(scan);
                Iterator it = rs.iterator();
                byte[] f = Bytes.toBytes("f1");

                byte[] caller = Bytes.toBytes("caller");
                byte[] callee = Bytes.toBytes("callee");
                byte[] callTime = Bytes.toBytes("callTime");
                byte[] callDuration = Bytes.toBytes("callDuration");
                CallLog log = null;
                while (it.hasNext()) {
                    log = new CallLog();
                    Result r = it.next();
                    //rowkey
                    String rowkey = Bytes.toString(r.getRow());
                    String flag = rowkey.split(",")[3] ;
                    log.setFlag(flag.equals("0")?true:false);
                    //caller
                    log.setCaller(Bytes.toString(r.getValue(f, caller)));
                    //callee
                    log.setCallee(Bytes.toString(r.getValue(f, callee)));
                    //callTime
                    log.setCallTime(Bytes.toString(r.getValue(f, callTime)));
                    //callDuration
                    log.setCallDuration(Bytes.toString(r.getValue(f, callDuration)));
                    logs.add(log);
                }
            }
            return logs;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

 

 

 

 

创建CallLogController:

 

/**
 * Created by Administrator on 2017/4/11.
 */
@Controller
public class CallLogController {

    @Resource(name="personService")
    private PersonService ps ;

    //注入hiveservice
    @Resource(name="hiveCallLogService")
    private HiveCallLogService hcs ;

    @Resource(name="callLogService")
    private CallLogService cs ;

    @RequestMapping("/callLog/findAll")
    public String findAll(Model m){
        List list = cs.findAll();

        m.addAttribute("callLogs",list);
        return "callLog/callLogList" ;
    }

    @RequestMapping("/callLog/json/findAll")
    public String findAllJson(HttpServletResponse response) {
        List list = cs.findAll();
        String json = JSON.toJSONString(list);
        //内容类型
        response.setContentType("application/json");
        try {
            OutputStream out = response.getOutputStream();
            out.write(json.getBytes());
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  null;
    }

    /**
     * 进入查询通话记录的页面,form
     */
    @RequestMapping("/callLog/toFindCallLogPage")
    public String toFindCallLogPage(){
        return "callLog/findCallLog" ;
    }

    @RequestMapping(value = "/callLog/findCallLog",method = RequestMethod.POST)
    public String findCallLog(Model m , @RequestParam("caller") String caller, @RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime){
        List list = CallLogUtil.getCallLogRanges(startTime, endTime);
        List logs = cs.findCallogs(caller,list);
        m.addAttribute("callLogs", logs);
        return "callLog/callLogList" ;
    }

    /**
     * 查询最近通话记录
     */
    @RequestMapping(value = "/callLog/findLatestCallLog",method = RequestMethod.POST)
    public String findLatestCallLog(Model m , @RequestParam("caller") String caller){
        CallLog log = hcs.findLatestCallLog(caller);
        if(log != null){
            m.addAttribute("log", log);
        }
        return "callLog/latestCallLog" ;
    }

    /**
     * 查询最近通话记录
     */
    @RequestMapping(value = "/callLog/toFindLatestCallLog")
    public String toFindLatestCallLog(){
        return "callLog/findLatestCallLog" ;
    }
}

 

相关前端:

 

05 ssm框架访问操作hbase_第2张图片

进入后既可以打开浏览器访问,通过查询会进行与hbase交互

另外使用hive也一样过程,只不过只要hive查询会生成对应MR作业,另外需要开启hiveserver2.

可视化数据展示挺简单,以下是使用echarts中的条形图(使用之前要导入echarts.js):

 




    
    bar.html
    
    
    


这里的数据是写死的,查询出来的数据传入data中即可:

05 ssm框架访问操作hbase_第3张图片

你可能感兴趣的:(大数据电话通信项目)