Skip to content

Instantly share code, notes, and snippets.

@yangl
Last active May 4, 2018 03:43
Show Gist options
  • Save yangl/8902716f74d967b1ad6690d4ed7df1e3 to your computer and use it in GitHub Desktop.
Save yangl/8902716f74d967b1ad6690d4ed7df1e3 to your computer and use it in GitHub Desktop.
HBase例子 使用spring-data-hadoop操作hbase
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>hbase.zookeeper.quorum</name>
<value>cnsz22pl0122,cnsz22pl0123,cnsz22pl0124</value>
</property>
<property>
<name>hbase.zookeeper.property.clientPort</name>
<value>2181</value>
</property>
<property>
<name>zookeeper.znode.parent</name>
<value>/hbase</value>
</property>
<property>
<name>hbase.rpc.timeout</name>
<value>10000</value>
</property>
</configuration>
create 'uep_exception_msg',
{NAME => 'data', VERSIONS => '1', MIN_VERSIONS => '0', COMPRESSION => 'SNAPPY', TTL => '2678400', BLOOMFILTER => 'ROW'}
describe 'uep_exception_msg'
alter 'uep_exception_msg',
{NAME => 'data',TTL => '60'}
scan 'uep_exception_msg'
public static final String TABLE_NAME_EXCEPTION = "uep_exception_msg";
public static final String FAMILY_NAME_EXCEPTION = "data";
public static final String QUALIFIER_NAME_EXCEPTION = "value";
String rowkey = StringUtils.removeAll(UUID.randomUUID().toString(), "-");
List<RequestMessageToConsumer> listMessage = Lists.newArrayList();
RequestMessageToConsumer c1 = new RequestMessageToConsumer();
c1.setEventMsg("1临水临电福克斯的分离技术的分类xxx".getBytes());
c1.setSpanId(10000000000L);
RequestMessageToConsumer c2 = new RequestMessageToConsumer();
c2.setEventMsg("2临水临电福克斯的分离技术的分类yyy".getBytes());
c2.setSpanId(20000000000L);
listMessage.add(c1);
listMessage.add(c2);
String message = JSON.toJSONString(listMessage);
hbaseTemplate.put(HBaseConstant.TABLE_NAME_EXCEPTION, rowkey, HBaseConstant.FAMILY_NAME_EXCEPTION,
HBaseConstant.QUALIFIER_NAME_EXCEPTION, message.getBytes());
String message2 =
hbaseTemplate.get(HBaseConstant.TABLE_NAME_EXCEPTION, rowkey, HBaseConstant.FAMILY_NAME_EXCEPTION,
HBaseConstant.QUALIFIER_NAME_EXCEPTION, (result, rowNum) -> new String(result.value()));
Scan scan = new Scan();
scan.setFilter(new FirstKeyOnlyFilter());
long hbase = hbaseTemplate.find("uep_exception_msg", scan, results -> {
long rowCount = 0;
for (org.apache.hadoop.hbase.client.Result result : results) {
rowCount += result.size();
}
return rowCount;
});
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-hadoop</artifactId>
<version>2.5.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.2</version>
</dependency>
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop-2.3.xsd">
<!-- HDFS配置 -->
<!--<hdp:configuration id="hadoopConfiguration" resources="classpath:/hbase-site.xml"/>-->
<hdp:configuration id="hadoopConfiguration" resources="file:${UEP_CONF_DIR}/hbase-site.xml"/>
<!-- HBase连接配置 -->
<hdp:hbase-configuration id="hbaseConfiguration" configuration-ref="hadoopConfiguration"/>
<!-- HbaseTemplate Bean配置 -->
<bean id="hbaseTemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">
<property name="configuration" ref="hbaseConfiguration"></property>
<property name="encoding" value="UTF-8"></property>
</bean>
</beans>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment