Commit 1f9b3cfb by qiuweili123

增加hbasedemo

parent a9f95a29
package com.secoo.mall;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@Slf4j
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
log.info("matrix-bigdata-demo SpringBoot Start Success");
}
}
package com.secoo.mall.hbase;
import com.secoo.mall.hbase.spring.boot.autoconfigure.HbaseTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 简单 HBase 操作
*
* @author zhanghao
* @date 2020-03-1719:20
*/
@Component
@Slf4j
public class SimpleHBase implements InitializingBean {
@Resource
private HbaseTemplate hbaseTemplate;
private static final String TABLE_NAME = "mytable";
private static final String CF_DEFAULT = "cf";
public static final byte[] QUALIFIER = "col1".getBytes();
private static final byte[] ROWKEY = "rowkey1".getBytes();
/**
* 建表
*
* @throws IOException
*/
public void createTable() throws IOException {
List<ColumnFamilyDescriptor> columnFamilyDescriptors = new ArrayList<ColumnFamilyDescriptor>();
columnFamilyDescriptors.add(ColumnFamilyDescriptorBuilder.of(CF_DEFAULT));
TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(TableName.valueOf(TABLE_NAME))
.setColumnFamilies(columnFamilyDescriptors).build();
log.info("Creating table. ");
Admin admin = hbaseTemplate.getConnection().getAdmin();
if(!admin.tableExists(TableName.valueOf(TABLE_NAME))){
admin.createTable(tableDescriptor);
}
log.info(" Done.");
}
/**
* 保存更新
*/
public void put() {
Put put = new Put(ROWKEY);
put.addColumn(CF_DEFAULT.getBytes(), QUALIFIER, "this is value".getBytes());
hbaseTemplate.saveOrUpdate(TABLE_NAME, put);
}
/**
* 查询
*/
public void get() {
hbaseTemplate.get(TABLE_NAME, "rowkey1", CF_DEFAULT, (result, rowNum) -> {
Map<String, String> map = new HashMap();
for (Cell cell : result.rawCells()) {
log.info("行健: " + new String(CellUtil.cloneRow(cell)));
log.info("列簇: " + new String(CellUtil.cloneFamily(cell)));
log.info("列: " + new String(CellUtil.cloneQualifier(cell)));
log.info("值: " + new String(CellUtil.cloneValue(cell)));
log.info("时间戳: " + cell.getTimestamp());
map.put(new String(CellUtil.cloneQualifier(cell)), new String(CellUtil.cloneValue(cell)));
}
return map;
});
}
@Override
public void afterPropertiesSet() throws Exception {
// createTable();
// put();
get();
}
}
spring:
application:
name: matrix-bigdata-demo
server:
port: 6080
hbase:
zookeeper:
quorum: 10.4.3.236:2181,10.4.3.237:2181,10.4.3.235:2181
......@@ -42,6 +42,6 @@ public class RoketMqSampleApplication implements ApplicationRunner {
template.syncSend(destination2, user, 10000);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment