代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
java 获取 hbase数据 springdatahadoop -- hbasetempl
作者:
/ 发布于
2017/5/8
/
675
配置文件 1、建立 spring-hbase.xml 获取连接池 第一种:直接在配置文件注明 hdfs的端口和zk的地址以及端口进行连接 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:hdp="http://www.springframework.org/schema/hadoop" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <context:property-placeholder location="hbase.properties" /> <!-- 配置HbaseTemplate --> <bean id="htemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate"> <property name="configuration" ref="hbaseConfiguration"> </property> </bean> <!-- 配置hadoop的基本信息 --> <hdp:configuration> fs.default.name="hdfs://192.168.0.173:8082" </hdp:configuration> <!-- 配置zookeeper地址和端口 --> <hdp:hbase-configuration zk-quorum="192.168.0.173" zk-port="2181" /> </beans> 第二种:只需要配置zk的地址和端口,但是需要在classpath另外新增 hbase-site.xml的配置 spring-hbase.xml 配置: [java] view plain copy print?在CODE上查看代码片派生到我的代码片 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:hdp="http://www.springframework.org/schema/hadoop" xmlns:beans="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd"> <!-- 配置zookeeper的信息,远程连接hbase时使用 --> <hdp:configuration resources="classpath:/hbase-site.xml" /> <hdp:hbase-configuration configuration-ref="hadoopConfiguration" /> <!-- 配置HbaseTemplate --> <bean id="htemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate"> <property name="configuration" ref="hbaseConfiguration"> </property> <property name="encoding" value="UTF-8"></property> </bean> </beans> hbase-site.xml 配置 放在classpath下(直接扔到src下即可) <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>hbase.zookeeper.quorum</name> <value>192.168.0.173</value> </property> <property> <name>hbase.zookeeper.property.clientPort</name> <value>2181</value> </property> </configuration> 配置完成 程序代码 官网的api地址已经在上面发过了,这里只做了一个简单的demo,列举了简单的 get find execue 方法 package cn.fulong.hbase; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.client.HTableInterface; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.data.hadoop.hbase.HbaseTemplate; import org.springframework.data.hadoop.hbase.RowMapper; import org.springframework.data.hadoop.hbase.TableCallback; import cn.fulong.view.HbaseModel; public class HbaseTest { //ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "application_hbase.xml" }); ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "spring_hbase.xml" }); BeanFactory factory = (BeanFactory) context; HbaseTemplate htemplate = (HbaseTemplate) factory.getBean("htemplate"); Map hMap = new HashMap<String, List<HbaseModel>>(); public String key; public String familyName ; public String qualifier; public String value; public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getFamilyName() { return familyName; } public void setFamilyName(String familyName) { this.familyName = familyName; } public String getQualifier() { return qualifier; } public void setQualifier(String qualifier) { this.qualifier = qualifier; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public static void main(String[] args) { //PrefixFilter HbaseTest h = new HbaseTest(); for(int i=0;i<=10000;i++){ h.setKey("linlin"+i); h.setFamilyName("info"); h.setQualifier("service"); h.setValue(i+"技术创新和质量服务"); h.execute("linlintest", null); } List<Map<String,Object>> mapList1 = h.find("linlintest",null,null); System.out.println("2"); } /** * 写数据 * @param tableName * @param action * @return */ public Boolean execute(String tableName, TableCallback<Boolean> action) { return htemplate.execute(tableName, new TableCallback<Boolean>() { public Boolean doInTable(HTableInterface table) throws Throwable { boolean flag = false; try{ byte[] rowkey = key.getBytes(); Put put = new Put(rowkey); put.add(Bytes.toBytes(familyName),Bytes.toBytes(qualifier), Bytes.toBytes(value)); table.put(put); flag = true; }catch(Exception e){ e.printStackTrace(); } return flag; } }); } /** * 通过表名和key获取一行数据 * @param tableName * @param rowName * @return */ public Map<String, Object> get(String tableName, String rowName) { return htemplate.get(tableName, rowName,new RowMapper<Map<String,Object>>(){ public Map<String,Object> mapRow(Result result, int rowNum) throws Exception { List<Cell> ceList = result.listCells(); Map<String,Object> map = new HashMap<String, Object>(); if(ceList!=null&&ceList.size()>0){ for(Cell cell:ceList){ map.put(Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength())+ "_"+Bytes.toString( cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()), Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); } } return map; } }); } /** * 通过表名 key 和 列族 和列 获取一个数据 * @param tableName * @param rowName * @param familyName * @param qualifier * @return */ public String get(String tableName ,String rowName, String familyName, String qualifier) { return htemplate.get(tableName, rowName,familyName,qualifier ,new RowMapper<String>(){ public String mapRow(Result result, int rowNum) throws Exception { List<Cell> ceList = result.listCells(); String res = ""; if(ceList!=null&&ceList.size()>0){ for(Cell cell:ceList){ res = Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()); } } return res; } }); } /** * 通过表名,开始行键和结束行键获取数据 * @param tableName * @param startRow * @param stopRow * @return */ public List<Map<String,Object>> find(String tableName , String startRow,String stopRow) { Scan scan = new Scan(); if(startRow==null){ startRow=""; } if(stopRow==null){ stopRow=""; } scan.setStartRow(Bytes.toBytes(startRow)); scan.setStopRow(Bytes.toBytes(stopRow)); /* PageFilter filter = new PageFilter(5); scan.setFilter(filter);*/ return htemplate.find(tableName, scan,new RowMapper<Map<String,Object>>(){ public Map<String,Object> mapRow(Result result, int rowNum) throws Exception { List<Cell> ceList = result.listCells(); Map<String,Object> map = new HashMap<String,Object>(); Map<String,Map<String,Object>> returnMap = new HashMap<String,Map<String,Object>>(); String row = ""; if(ceList!=null&&ceList.size()>0){ for(Cell cell:ceList){ row =Bytes.toString( cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()); String value =Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()); String family = Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength()); String quali = Bytes.toString( cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()); map.put(family+"_"+quali, value); } map.put("row",row ); } return map; } }); } /* public void scanValueByFilter(String tableName,String row) throws IOException{ HTable table = new HTable(conf, tableName); Scan scan = new Scan(); scan.setFilter(new PrefixFilter(row.getBytes())); ResultScanner resultScanner = table.getScanner(scan); for(Result rs:resultScanner){ } }*/ }
试试其它关键字
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3