|
1 | 1 | # 项目介绍 |
2 | | -自定义的spring-boot的hbase starter,为hbase的query操作提供简易的api并集成spring-boot的auto configuration |
| 2 | +自定义的spring-boot的hbase starter,为hbase的query和更新等操作提供简易的api并集成spring-boot的auto configuration |
3 | 3 | # 使用方式 |
4 | 4 | ## 集成 |
| 5 | +在spring-boot项目的application.properties文件中加入spring.data.hbase.quorum配置项,并赋予正确的值 |
| 6 | +## 使用 |
| 7 | +### query |
| 8 | + |
| 9 | +```java |
| 10 | +public class PeopleDto { |
| 11 | + |
| 12 | + private String name; |
| 13 | + |
| 14 | + private int age; |
| 15 | + |
| 16 | + public String getName() { |
| 17 | + return name; |
| 18 | + } |
| 19 | + |
| 20 | + public PeopleDto setName(String name) { |
| 21 | + this.name = name; |
| 22 | + return this; |
| 23 | + } |
| 24 | + |
| 25 | + public int getAge() { |
| 26 | + return age; |
| 27 | + } |
| 28 | + |
| 29 | + public PeopleDto setAge(int age) { |
| 30 | + this.age = age; |
| 31 | + return this; |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | +```java |
| 36 | +import com.jthink.skyeye.data.hbase.api.RowMapper; |
| 37 | +import org.apache.hadoop.hbase.client.Result; |
| 38 | +import org.apache.hadoop.hbase.util.Bytes; |
| 39 | + |
| 40 | +public class PeopleRowMapper implements RowMapper<PeopleDto> { |
| 41 | + |
| 42 | + private static byte[] COLUMNFAMILY = "f".getBytes(); |
| 43 | + private static byte[] NAME = "name".getBytes(); |
| 44 | + private static byte[] AGE = "age".getBytes(); |
| 45 | + |
| 46 | + @Override |
| 47 | + public PeopleDto mapRow(Result result, int rowNum) throws Exception { |
| 48 | + PeopleDto dto = new PeopleDto(); |
| 49 | + // TODO: 设置相关的属性值 |
| 50 | + String name = Bytes.toString(result.getValue(COLUMNFAMILY, NAME)); |
| 51 | + int age = Bytes.toInt(result.getValue(COLUMNFAMILY, AGE)); |
| 52 | + |
| 53 | + return dto.setName(name).setAge(age); |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | +```java |
| 58 | +import com.jthink.skyeye.data.hbase.api.HbaseTemplate; |
| 59 | +import org.apache.hadoop.hbase.client.Scan; |
| 60 | +import org.apache.hadoop.hbase.util.Bytes; |
| 61 | +import org.springframework.beans.factory.annotation.Autowired; |
| 62 | +import org.springframework.stereotype.Service; |
| 63 | + |
| 64 | +import java.util.List; |
| 65 | + |
| 66 | +public class QueryService { |
| 67 | + |
| 68 | + @Autowired |
| 69 | + private HbaseTemplate hbaseTemplate; |
| 70 | + |
| 71 | + public List<PeopleDto> query(String startRow, String stopRow) { |
| 72 | + Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow)); |
| 73 | + scan.setCaching(5000); |
| 74 | + List<PeopleDto> dtos = this.hbaseTemplate.find("people_table", scan, new PeopleRowMapper()); |
| 75 | + return dtos; |
| 76 | + } |
| 77 | + |
| 78 | + public PeopleDto query(String row) { |
| 79 | + PeopleDto dto = this.hbaseTemplate.get("people_table", row, new PeopleRowMapper()); |
| 80 | + return dto; |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | +### update等 |
| 85 | +```java |
| 86 | +import com.jthink.skyeye.data.hbase.api.HbaseTemplate; |
| 87 | +import org.apache.hadoop.hbase.client.Delete; |
| 88 | +import org.apache.hadoop.hbase.client.Mutation; |
| 89 | +import org.apache.hadoop.hbase.client.Scan; |
| 90 | +import org.apache.hadoop.hbase.util.Bytes; |
| 91 | +import org.springframework.beans.factory.annotation.Autowired; |
| 92 | +import org.springframework.stereotype.Service; |
| 93 | + |
| 94 | +import java.util.ArrayList; |
| 95 | +import java.util.List; |
| 96 | + |
| 97 | +@Service |
| 98 | +public class QueryService { |
| 99 | + |
| 100 | + @Autowired |
| 101 | + private HbaseTemplate hbaseTemplate; |
| 102 | + |
| 103 | + public List<PeopleDto> query(String startRow, String stopRow) { |
| 104 | + Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow)); |
| 105 | + scan.setCaching(5000); |
| 106 | + List<PeopleDto> dtos = this.hbaseTemplate.find("people_table", scan, new PeopleRowMapper()); |
| 107 | + return dtos; |
| 108 | + } |
| 109 | + |
| 110 | + public PeopleDto query(String row) { |
| 111 | + PeopleDto dto = this.hbaseTemplate.get("people_table", row, new PeopleRowMapper()); |
| 112 | + return dto; |
| 113 | + } |
| 114 | + |
| 115 | + public void saveOrUpdates() { |
| 116 | + List<Mutation> puts = new ArrayList<>(); |
| 117 | + // 设值 |
| 118 | + this.hbaseTemplate.saveOrUpdates("people_table", puts); |
| 119 | + } |
| 120 | + |
| 121 | + public void saveOrUpdate() { |
| 122 | + Mutation delete = new Delete(Bytes.toBytes("")); |
| 123 | + this.hbaseTemplate.saveOrUpdate("people_table", delete); |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +### 其他 |
| 129 | +不可以满足需求的可以使用hbaseTemplate暴露出来的getConnection()方法 |
0 commit comments