Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
);
057
????????????
}
058
????????????
HTableDescriptor tableDescriptor =
new
HTableDescriptor(tableName);
059
????????????
tableDescriptor.addFamily(
new
HColumnDescriptor(
"column1"
));
060
????????????
tableDescriptor.addFamily(
new
HColumnDescriptor(
"column2"
));
061
????????????
tableDescriptor.addFamily(
new
HColumnDescriptor(
"column3"
));
062
????????????
hBaseAdmin.createTable(tableDescriptor);
063
????????
}
catch
(MasterNotRunningException e) {
064
????????????
e.printStackTrace();
065
????????
}
catch
(ZooKeeperConnectionException e) {
066
????????????
e.printStackTrace();
067
????????
}
catch
(IOException e) {
068
????????????
e.printStackTrace();
069
????????
}
070
????????
System.out.println(
"end create table ......"
);
071
????
}
072
?
?073
?????
?074
????
public
static
void
insertData(String tableName) {
075
????????
System.out.println(
"start insert data ......"
);
076
????????
HTablePool pool =
new
HTablePool(configuration,
1000
);
077
????????
HTable table = (HTable) pool.getTable(tableName);
078
????????
Put put =
new
Put(
"112233bbbcccc"
.getBytes());
// 一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值
079
????????
put.add(
"column1"
.getBytes(),
null
,
"aaa"
.getBytes());
// 本行数据的第一列
080
????????
put.add(
"column2"
.getBytes(),
null
,
"bbb"
.getBytes());
// 本行数据的第三列
081
????????
put.add(
"column3"
.getBytes(),
null
,
"ccc"
.getBytes());
// 本行数据的第三列
082
????????
try
{
083
????????????
table.put(put);
084
????????
}
catch
(IOException e) {
085
????????????
e.printStackTrace();
086
????????
}
087
????????
System.out.println(
"end insert data ......"
);
088
????
}
089
?
?090
?????
?091
????
public
static
void
dropTable(String tableName) {
092
????????
try
{
093
????????????
HBaseAdmin admin =
new
HBaseAdmin(configuration);
094
????????????
admin.disableTable(tableName);
095
????????????
admin.deleteTable(tableName);
096
????????
}
catch
(MasterNotRunningException e) {
097
????????????
e.printStackTrace();
098
????????
}
catch
(ZooKeeperConnectionException e) {
099
????????????
e.printStackTrace();
100
????????
}
catch
(IOException e) {
101
????????????
e.printStackTrace();
102
????????
}
103
?
?104
????
}
105
?????
?106
?????
public
static
void
deleteRow(String tablename, String rowkey)? {
107
????????
try
{
108
????????????
HTable table =
new
HTable(configuration, tablename);
109
????????????
List list =
new
ArrayList();
110
????????????
Delete d1 =
new
Delete(rowkey.getBytes());
111
????????????
list.add(d1);
112
?????????????
?113
????????????
table.delete(list);
114
????????????
System.out.println(
"删除行成功!"
);
115
?????????????
?116
????????
}
catch
(IOException e) {
117
????????????
e.printStackTrace();
118
????????
}
119
?????????
?120
?
?121
????
}
122
?
?123
??????
?124
?????
public
static
void
deleteByCondition(String tablename, String rowkey)? {
125
????????????
//目前还没有发现有效的API能够实现根据非rowkey的条件删除这个功能能,还有清空表全部数据的API操作
126
?
?127
????
}
128
?
?129
?
?130
?????
?131
????
public
static
void
QueryAll(String tableName) {
132
????????
HTablePool pool =
new
HTablePool(configuration,
1000
);
133
????????
HTable table = (HTable) pool.getTable(tableName);
134
????????
try
{
135
????????????
ResultScanner rs = table.getScanner(
new
Scan());
136
????????????
for
(Result r : rs) {
137
????????????????
System.out.println(
"获得到rowkey:"
+
new
String(r.getRow()));
138
????????????????
for
(KeyValue keyValue : r.raw()) {
139
????????????????????
System.out.println(
"列:"
+
new
String(keyValue.getFamily())
140
????????????????????????????
+
"====值:"
+
new
String(keyValue.getValue()));
141
????????????????
}
142
????????????
}
143
????????
}
catch
(IOException e) {
144
????????????
e.printStackTrace();
145
????????
}
146
????
}
147
?
?148
?????
?149
????
public
static
void
QueryByCondition1(String tableName) {
150
?
?151
????????
HTablePool pool =
new
HTablePool(configuration,
1000
);
152
????????
HTable table = (HTable) pool.getTable(tableName);
153
????????
try
{
154
????????????
Get scan =
new
Get(
"abcdef"
.getBytes());
// 根据rowkey查询
155
????????????
Result r = table.get(scan);
156
????????????
System.out.println(
"获得到rowkey:"
+
new
String(r.getRow()));
157
????????????
for
(KeyValue keyValue : r.raw()) {
158
????????????????
System.out.println(
"列:"
+
new
String(keyValue.getFamily())
159
????????????????????????
+
"====值:"
+
new
String(keyValue.getValue()));
160
????????????
}
161
????????
}
catch
(IOException e) {
162
????????????
e.printStackTrace();
163
????????
}
164
????
}
165
?
?166
?????
?167
????
public
static
void
QueryByCondition2(String tableName) {
168
?
?169
????????
try
{
170
????????????
HTablePool pool =
new
HTablePool(configuration,
1000
);
171
????????????
HTable table = (HTable) pool.getTable(tableName);
172
????????????
Filter filter =
new
SingleColumnValueFilter(Bytes
173
????????????????????
.toBytes(
"column1"
),
null
, CompareOp.EQUAL, Bytes
174
????????????????????
.toBytes(
"aaa"
));
// 当列column1的值为aaa时进行查询
175
????????????
Scan s =
new
Scan();
176
????????????
s.setFilter(filter);
177
????????????
ResultScanner rs = table.getScanner(s);
178
????????????
for
(Result r : rs) {
179
????????????????
System.out.println(
"获得到rowkey:"
+
new
String(r.getRow()));
180
????????????????
for
(KeyValue keyValue : r.raw()) {
181
????????????????????
System.out.println(
"列:"
+
new
String(keyValue.getFamily())
182
????????????????????????????
+
"====值:"
+
new
String(keyValue.getValue()));
183
????????????????
}
184
????????????
}
185
????????
}
catch
(Exception e) {
186
????????????
e.printStackTrace();
187
????????
}
188
?
?189
????
}
190
?
?191
?????
?192
????
public
static
void
QueryByCondition3(String tableName) {
193
?
?194
????????
try
{
195
????????????
HTablePool pool =
new
HTablePool(configuration,
1000
);
196
????????????
HTable table = (HTable) pool.getTable(tableName);
197
?
?198
????????????
List<Filter> filters =
new
ArrayList<Filter>();
199
?
?200
????????????
Filter filter1 =
new
SingleColumnValueFilter(Bytes
201
????????????????????
.toBytes(
"column1"
),
null
, CompareOp.EQUAL, Bytes
202
????????????????????
.toBytes(
"aaa"
));
203
????????????
filters.add(filter1);
204
?
?205
????????????
Filter filter2 =
new
SingleColumnValueFilter(Bytes
206
????????????????????
.toBytes(
"column2"
),
null
, CompareOp.EQUAL, Bytes
207
????????????????????
.toBytes(
"bbb"
));
208
????????????
filters.add(filter2);
209
?
?210
????????????
Filter filter3 =
new
SingleColumnValueFilter(Bytes
211
????????????????????
.toBytes(
"column3"
),
null
, CompareOp.EQUAL, Bytes
212
????????????????????
.toBytes(
"ccc"
));
213
????????????
filters.add(filter3);
214
?
?215
????????????
FilterList filterList1 =
new
FilterList(filters);
216
?
?217
????????????
Scan scan =
new
Scan();
218
????????????
scan.setFilter(filterList1);
219
????????????
ResultScanner rs = table.getScanner(scan);
220
????????????
for
(Result r : rs) {
221
????????????????
System.out.println(
"获得到rowkey:"
+
new
String(r.getRow()));
222
????????????????
for
(KeyValue keyValue : r.raw()) {
223
????????????????????
System.out.println(
"列:"
+
new
String(keyValue.getFamily())
224
????????????????????????????
+
"====值:"
+
new
String(keyValue.getValue()));
225
????????????????
}
226
????????????
}
227
????????????
rs.close();
228
?
?229
????????
}
catch
(Exception e) {
230
????????????
e.printStackTrace();
231
????????
}
232
?
?233
????
}
234
?
?235
}