linux cpu占用率如何看
268
2022-09-24
Scala封装Phoenix工具类操作Hbase
《02.Scala封装Phoenix工具类》
Maven依赖
查询
import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet, ResultSetMetaData}import com.alibaba.fastjson.JSONObjectimport scala.collection.mutable.ListBufferobject PhoenixUtil { def main(args: Array[String]): Unit = { //查询语句 select * from "ns1"."us_population" val list: List[JSONObject] = queryList("select * from \"ns1\".\"us_population\"") println(list) } private val phoenixUrl = "jdbc:phoenix:zjj101,zjj102,zjj103:2181" def queryList(sql: String): List[JSONObject] = { val rsList: ListBuffer[JSONObject] = new ListBuffer[JSONObject] //注册驱动 Class.forName("org.apache.phoenix.jdbc.PhoenixDriver") //建立连接 val conn: Connection = DriverManager.getConnection(phoenixUrl) //创建数据库操作对象 val ps: PreparedStatement = conn.prepareStatement(sql) //执行SQL语句 val rs: ResultSet = ps.executeQuery() val rsMetaData: ResultSetMetaData = rs.getMetaData //处理结果集 while (rs.next()) { val usPopulation = new JSONObject() for (i <- 1 to rsMetaData.getColumnCount) { usPopulation.put(rsMetaData.getColumnName(i), rs.getObject(i)) } rsList.append(usPopulation) } //释放资源 rs.close() ps.close() conn.close() rsList.toList }}
执行输出结果:
List({“POPULATION”:3844829,“CITY”:“Los Angeles”,“STATE”:“CA”}, {“POPULATION”:2842518,“CITY”:“Chicago”,“STATE”:“IL”}, {“POPULATION”:8143197,“CITY”:“NewYork”,“STATE”:“NY”})
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~