美文网首页
springboot-使用phoeinx连接hbase

springboot-使用phoeinx连接hbase

作者: 刘东青_6f21 | 来源:发表于2021-01-14 19:52 被阅读0次

1.pom.xml 加上对应版本

        <dependency>
            <groupId>org.apache.phoenix</groupId>
            <artifactId>phoenix-core</artifactId>
            <version>4.14.0-HBase-1.2</version>
        </dependency>

2.resources 目录下 添加hbase配置文件 hbase-site.xml

3.使用示例

    @RequestMapping("/test1")
    @ResponseBody
    public Map<String, Object> test1() throws ClassNotFoundException, SQLException {
        String driver = "org.apache.phoenix.jdbc.PhoenixDriver";
        Class.forName(driver);
        String phoenix_url = "xxxxxxx";
        String use = "xxx";
        String password = "xx";
        Connection conn = DriverManager.getConnection(phoenix_url,use,password);
        String sql = "select 字段1,字段2 from 表名 where pk='xxx'";
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        List<Map<String, Object>> list = new ArrayList<>();
        list = new ArrayList<>();
        //移动光标,如果新的当前行有效,则返回 true;如果不存在下一行,则返回 false
        while (rs.next()) {
            ResultSetMetaData rsmd = rs.getMetaData();
            Map<String, Object> map = new HashMap<>();
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                map.put(toLowerCase(rsmd.getColumnName(i)), rs.getObject(i));
            }
            list.add(map);
        }
        rs.close();
        ps.close();
        Map<String, Object> map = new HashMap<>(100000);
        map.put("data", list);
        return map;
    }

相关文章

网友评论

      本文标题:springboot-使用phoeinx连接hbase

      本文链接:https://www.haomeiwen.com/subject/lifbaktx.html