|
1 |
| -# 项目介绍 |
| 1 | +项目分享 |
| 2 | + |
| 3 | +分享一个项目:https://github.com/JThink/SkyEye,对java、scala等运行于jvm的程序进行实时日志采集、索引和可视化,对系统进行进程级别的监控,对系统内部的操作进行策略性的报警、对分布式的rpc调用进行trace跟踪以便于进行性能分析。欢迎对分布式跟踪感兴趣的交流~~,交流群:624054633 |
| 4 | + |
| 5 | +项目介绍 |
| 6 | + |
2 | 7 | 自定义的spring-boot的hbase starter,为hbase的query和更新等操作提供简易的api并集成spring-boot的auto configuration
|
3 |
| -# 打包 |
| 8 | + |
| 9 | +打包 |
| 10 | + |
4 | 11 | 修改相关的maven私服地址
|
5 |
| -```shell |
6 |
| -gradle clean install uploadArchives |
7 |
| -``` |
8 |
| -# 使用方式 |
9 |
| -## 依赖 |
10 |
| -```shell |
11 |
| -compile "com.spring4all:spring-boot-starter-hbase:1.0.0.RELEASE" |
12 |
| -``` |
13 |
| -## 集成 |
14 |
| -在spring-boot项目的application.properties文件中加入spring.data.hbase.quorum,spring.data.hbase.rootDir,spring.data.hbase.nodeParent配置项,并赋予正确的值 |
15 |
| -## 使用 |
16 |
| -### query |
17 |
| -1. 将上述配置项赋予正确的值 |
18 |
| -2. dto定义 |
19 |
| -```java |
20 |
| -public class PeopleDto { |
21 | 12 |
|
22 |
| - private String name; |
| 13 | + gradle clean install uploadArchives |
23 | 14 |
|
24 |
| - private int age; |
| 15 | +使用方式 |
25 | 16 |
|
26 |
| - public String getName() { |
27 |
| - return name; |
28 |
| - } |
| 17 | +依赖 |
29 | 18 |
|
30 |
| - public PeopleDto setName(String name) { |
31 |
| - this.name = name; |
32 |
| - return this; |
33 |
| - } |
| 19 | + compile "com.spring4all:spring-boot-starter-hbase:1.0.0.RELEASE" |
34 | 20 |
|
35 |
| - public int getAge() { |
36 |
| - return age; |
37 |
| - } |
| 21 | +集成 |
38 | 22 |
|
39 |
| - public PeopleDto setAge(int age) { |
40 |
| - this.age = age; |
41 |
| - return this; |
42 |
| - } |
43 |
| -} |
44 |
| -``` |
45 |
| -3. RowMapper定义 |
46 |
| -```java |
47 |
| -import com.jthink.skyeye.data.hbase.api.RowMapper; |
48 |
| -import org.apache.hadoop.hbase.client.Result; |
49 |
| -import org.apache.hadoop.hbase.util.Bytes; |
50 |
| - |
51 |
| -public class PeopleRowMapper implements RowMapper<PeopleDto> { |
52 |
| - |
53 |
| - private static byte[] COLUMNFAMILY = "f".getBytes(); |
54 |
| - private static byte[] NAME = "name".getBytes(); |
55 |
| - private static byte[] AGE = "age".getBytes(); |
56 |
| - |
57 |
| - @Override |
58 |
| - public PeopleDto mapRow(Result result, int rowNum) throws Exception { |
59 |
| - PeopleDto dto = new PeopleDto(); |
60 |
| - // TODO: 设置相关的属性值 |
61 |
| - String name = Bytes.toString(result.getValue(COLUMNFAMILY, NAME)); |
62 |
| - int age = Bytes.toInt(result.getValue(COLUMNFAMILY, AGE)); |
63 |
| - |
64 |
| - return dto.setName(name).setAge(age); |
65 |
| - } |
66 |
| -} |
67 |
| -``` |
68 |
| -4. query操作 |
69 |
| -```java |
70 |
| -import com.jthink.skyeye.data.hbase.api.HbaseTemplate; |
71 |
| -import org.apache.hadoop.hbase.client.Scan; |
72 |
| -import org.apache.hadoop.hbase.util.Bytes; |
73 |
| -import org.springframework.beans.factory.annotation.Autowired; |
74 |
| -import org.springframework.stereotype.Service; |
75 |
| - |
76 |
| -import java.util.List; |
77 |
| - |
78 |
| -public class QueryService { |
79 |
| - |
80 |
| - @Autowired |
81 |
| - private HbaseTemplate hbaseTemplate; |
82 |
| - |
83 |
| - public List<PeopleDto> query(String startRow, String stopRow) { |
84 |
| - Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow)); |
85 |
| - scan.setCaching(5000); |
86 |
| - List<PeopleDto> dtos = this.hbaseTemplate.find("people_table", scan, new PeopleRowMapper()); |
87 |
| - return dtos; |
88 |
| - } |
| 23 | +在spring-boot项目的application.properties文件中加入spring.data.hbase.quorum,spring.data.hbase.rootDir,spring.data.hbase.nodeParent配置项,并赋予正确的值 |
| 24 | + |
| 25 | +使用 |
| 26 | + |
| 27 | +query |
89 | 28 |
|
90 |
| - public PeopleDto query(String row) { |
91 |
| - PeopleDto dto = this.hbaseTemplate.get("people_table", row, new PeopleRowMapper()); |
92 |
| - return dto; |
93 |
| - } |
94 |
| -} |
95 |
| -``` |
96 |
| -### update等 |
97 | 29 | 1. 将上述配置项赋予正确的值
|
98 |
| -2. update、delete、put操作 |
99 |
| -```java |
100 |
| -import com.jthink.skyeye.data.hbase.api.HbaseTemplate; |
101 |
| -import org.apache.hadoop.hbase.client.Delete; |
102 |
| -import org.apache.hadoop.hbase.client.Mutation; |
103 |
| -import org.apache.hadoop.hbase.client.Scan; |
104 |
| -import org.apache.hadoop.hbase.util.Bytes; |
105 |
| -import org.springframework.beans.factory.annotation.Autowired; |
106 |
| -import org.springframework.stereotype.Service; |
107 |
| - |
108 |
| -import java.util.ArrayList; |
109 |
| -import java.util.List; |
110 |
| - |
111 |
| -@Service |
112 |
| -public class QueryService { |
113 |
| - |
114 |
| - @Autowired |
115 |
| - private HbaseTemplate hbaseTemplate; |
116 |
| - |
117 |
| - public List<PeopleDto> query(String startRow, String stopRow) { |
118 |
| - Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow)); |
119 |
| - scan.setCaching(5000); |
120 |
| - List<PeopleDto> dtos = this.hbaseTemplate.find("people_table", scan, new PeopleRowMapper()); |
121 |
| - return dtos; |
122 |
| - } |
| 30 | +2. dto定义 |
123 | 31 |
|
124 |
| - public PeopleDto query(String row) { |
125 |
| - PeopleDto dto = this.hbaseTemplate.get("people_table", row, new PeopleRowMapper()); |
126 |
| - return dto; |
| 32 | + public class PeopleDto { |
| 33 | + |
| 34 | + private String name; |
| 35 | + |
| 36 | + private int age; |
| 37 | + |
| 38 | + public String getName() { |
| 39 | + return name; |
| 40 | + } |
| 41 | + |
| 42 | + public PeopleDto setName(String name) { |
| 43 | + this.name = name; |
| 44 | + return this; |
| 45 | + } |
| 46 | + |
| 47 | + public int getAge() { |
| 48 | + return age; |
| 49 | + } |
| 50 | + |
| 51 | + public PeopleDto setAge(int age) { |
| 52 | + this.age = age; |
| 53 | + return this; |
| 54 | + } |
127 | 55 | }
|
128 | 56 |
|
129 |
| - public void saveOrUpdates() { |
130 |
| - List<Mutation> puts = new ArrayList<>(); |
131 |
| - // 设值 |
132 |
| - this.hbaseTemplate.saveOrUpdates("people_table", puts); |
133 |
| - } |
| 57 | +1. RowMapper定义 |
134 | 58 |
|
135 |
| - public void saveOrUpdate() { |
136 |
| - Mutation delete = new Delete(Bytes.toBytes("")); |
137 |
| - this.hbaseTemplate.saveOrUpdate("people_table", delete); |
| 59 | + import com.jthink.skyeye.data.hbase.api.RowMapper; |
| 60 | + import org.apache.hadoop.hbase.client.Result; |
| 61 | + import org.apache.hadoop.hbase.util.Bytes; |
| 62 | + |
| 63 | + public class PeopleRowMapper implements RowMapper<PeopleDto> { |
| 64 | + |
| 65 | + private static byte[] COLUMNFAMILY = "f".getBytes(); |
| 66 | + private static byte[] NAME = "name".getBytes(); |
| 67 | + private static byte[] AGE = "age".getBytes(); |
| 68 | + |
| 69 | + @Override |
| 70 | + public PeopleDto mapRow(Result result, int rowNum) throws Exception { |
| 71 | + PeopleDto dto = new PeopleDto(); |
| 72 | + // TODO: 设置相关的属性值 |
| 73 | + String name = Bytes.toString(result.getValue(COLUMNFAMILY, NAME)); |
| 74 | + int age = Bytes.toInt(result.getValue(COLUMNFAMILY, AGE)); |
| 75 | + |
| 76 | + return dto.setName(name).setAge(age); |
| 77 | + } |
138 | 78 | }
|
| 79 | + |
| 80 | +1. query操作 |
| 81 | + |
| 82 | + import com.jthink.skyeye.data.hbase.api.HbaseTemplate; |
| 83 | + import org.apache.hadoop.hbase.client.Scan; |
| 84 | + import org.apache.hadoop.hbase.util.Bytes; |
| 85 | + import org.springframework.beans.factory.annotation.Autowired; |
| 86 | + import org.springframework.stereotype.Service; |
| 87 | + |
| 88 | + import java.util.List; |
| 89 | + |
| 90 | + public class QueryService { |
139 | 91 |
|
140 |
| - public void saveOrUpdate() { |
141 |
| - List<Mutation> saveOrUpdates = new ArrayList<>(); |
142 |
| - Put put = new Put(Bytes.toBytes("135xxxxxx")); |
143 |
| - put.addColumn(Bytes.toBytes("people"), Bytes.toBytes("name"), Bytes.toBytes("JThink")); |
144 |
| - saveOrUpdates.add(put); |
| 92 | + @Autowired |
| 93 | + private HbaseTemplate hbaseTemplate; |
| 94 | + |
| 95 | + public List<PeopleDto> query(String startRow, String stopRow) { |
| 96 | + Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow)); |
| 97 | + scan.setCaching(5000); |
| 98 | + List<PeopleDto> dtos = this.hbaseTemplate.find("people_table", scan, new PeopleRowMapper()); |
| 99 | + return dtos; |
| 100 | + } |
| 101 | + |
| 102 | + public PeopleDto query(String row) { |
| 103 | + PeopleDto dto = this.hbaseTemplate.get("people_table", row, new PeopleRowMapper()); |
| 104 | + return dto; |
| 105 | + } |
| 106 | + } |
145 | 107 |
|
146 |
| - Delete delete = new Delete(Bytes.toBytes("136xxxxxx")); |
147 |
| - saveOrUpdates.add(delete); |
| 108 | +update等 |
148 | 109 |
|
149 |
| - // 继续add |
| 110 | +1. 将上述配置项赋予正确的值 |
| 111 | +2. update、delete、put操作 |
150 | 112 |
|
151 |
| - this.hbaseTemplate.saveOrUpdates("people_table", saveOrUpdates); |
| 113 | + import com.jthink.skyeye.data.hbase.api.HbaseTemplate; |
| 114 | + import org.apache.hadoop.hbase.client.Delete; |
| 115 | + import org.apache.hadoop.hbase.client.Mutation; |
| 116 | + import org.apache.hadoop.hbase.client.Scan; |
| 117 | + import org.apache.hadoop.hbase.util.Bytes; |
| 118 | + import org.springframework.beans.factory.annotation.Autowired; |
| 119 | + import org.springframework.stereotype.Service; |
| 120 | + |
| 121 | + import java.util.ArrayList; |
| 122 | + import java.util.List; |
| 123 | + |
| 124 | + @Service |
| 125 | + public class QueryService { |
| 126 | + |
| 127 | + @Autowired |
| 128 | + private HbaseTemplate hbaseTemplate; |
| 129 | + |
| 130 | + public List<PeopleDto> query(String startRow, String stopRow) { |
| 131 | + Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow)); |
| 132 | + scan.setCaching(5000); |
| 133 | + List<PeopleDto> dtos = this.hbaseTemplate.find("people_table", scan, new PeopleRowMapper()); |
| 134 | + return dtos; |
| 135 | + } |
| 136 | + |
| 137 | + public PeopleDto query(String row) { |
| 138 | + PeopleDto dto = this.hbaseTemplate.get("people_table", row, new PeopleRowMapper()); |
| 139 | + return dto; |
| 140 | + } |
| 141 | + |
| 142 | + public void saveOrUpdates() { |
| 143 | + List<Mutation> puts = new ArrayList<>(); |
| 144 | + // 设值 |
| 145 | + this.hbaseTemplate.saveOrUpdates("people_table", puts); |
| 146 | + } |
| 147 | + |
| 148 | + public void saveOrUpdate() { |
| 149 | + Mutation delete = new Delete(Bytes.toBytes("")); |
| 150 | + this.hbaseTemplate.saveOrUpdate("people_table", delete); |
| 151 | + } |
| 152 | + |
| 153 | + public void saveOrUpdate() { |
| 154 | + List<Mutation> saveOrUpdates = new ArrayList<>(); |
| 155 | + Put put = new Put(Bytes.toBytes("135xxxxxx")); |
| 156 | + put.addColumn(Bytes.toBytes("people"), Bytes.toBytes("name"), Bytes.toBytes("JThink")); |
| 157 | + saveOrUpdates.add(put); |
| 158 | + |
| 159 | + Delete delete = new Delete(Bytes.toBytes("136xxxxxx")); |
| 160 | + saveOrUpdates.add(delete); |
| 161 | + |
| 162 | + // 继续add |
| 163 | + |
| 164 | + this.hbaseTemplate.saveOrUpdates("people_table", saveOrUpdates); |
| 165 | + } |
152 | 166 | }
|
153 |
| -} |
154 |
| -``` |
155 | 167 |
|
156 |
| -### 其他 |
| 168 | +其他 |
| 169 | + |
157 | 170 | 不可以满足需求的可以使用hbaseTemplate暴露出来的getConnection()方法
|
0 commit comments