步骤1:新创建一个Java Project
步骤2:导入JAR包
全部来源于官方文档(hbase-1.1.2-bin.tar.gz)解压后,xx\hbase-1.1.2\lib*.jar
步骤3:修改开发机的hosts文件
步骤4:修改虚拟机的配置文件
步骤5:实现查询、新建、删除等
package hbase;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.exceptions.DeserializationException;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;
public class HBaseDemo {
// 与HBase数据库的连接对象
Connection connection;
// 数据库元数据操作对象
Admin admin;
@Before
public void setUp() throws Exception {
// 取得一个数据库连接的配置参数对象
Configuration conf = HBaseConfiguration.create();
// 设置连接参数:HBase数据库所在的主机IP
conf.set("hbase.zookeeper.quorum", "192.168.137.13");
// 设置连接参数:HBase数据库使用的端口
conf.set("hbase.zookeeper.property.clientPort", "2181");
// 取得一个数据库连接对象
connection = ConnectionFactory.createConnection(conf);
// 取得一个数据库元数据操作对象
admin = connection.getAdmin();
}
/**
* 创建表
*/
public void createTable() throws IOException{
System.out.println("---------------创建表 START-----------------");
// 数据表表名
String tableNameString = "t_book";
// 新建一个数据表表名对象
TableName tableName = TableName.valueOf(tableNameString);
// 如果需要新建的表已经存在
if(admin.tableExists(tableName)){
System.out.println("表已经存在!");
}
// 如果需要新建的表不存在
else{
// 数据表描述对象
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
// 列族描述对象
HColumnDescriptor family= new HColumnDescriptor("base");;
// 在数据表中新建一个列族
hTableDescriptor.addFamily(family);
// 新建数据表
admin.createTable(hTableDescriptor);
}
System.out.println("---------------创建表 END-----------------");
}
/**
* 查询整表数据
*/
@Test
public void queryTable() throws IOException{
System.out.println("---------------查询整表数据 START-----------------");
// 取得数据表对象
Table table = connection.getTable(TableName.valueOf("t_book"));
// 取得表中所有数据
ResultScanner scanner = table.getScanner(new Scan());
// 循环输出表中的数据
for (Result result : scanner) {
byte[] row = result.getRow();
System.out.println("row key is:" + new String(row));
List<Cell> listCells = result.listCells();
for (Cell cell : listCells) {
byte[] familyArray = cell.getFamilyArray();
byte[] qualifierArray = cell.getQualifierArray();
byte[] valueArray = cell.getValueArray();
System.out.println("row value is:" + new String(familyArray) + new String(qualifierArray)
+ new String(valueArray));
}
}
System.out.println("---------------查询整表数据 END-----------------");
}
/**
* 按行键查询表数据
*/
@Test
public void queryTableByRowKey() throws IOException{
System.out.println("---------------按行键查询表数据 START-----------------");
// 取得数据表对象
Table table = connection.getTable(TableName.valueOf("t_book"));
// 新建一个查询对象作为查询条件
Get get = new Get("row8".getBytes());
// 按行键查询数据
Result result = table.get(get);
byte[] row = result.getRow();
System.out.println("row key is:" + new String(row));
List<Cell> listCells = result.listCells();
for (Cell cell : listCells) {
byte[] familyArray = cell.getFamilyArray();
byte[] qualifierArray = cell.getQualifierArray();
byte[] valueArray = cell.getValueArray();
System.out.println("row value is:" + new String(familyArray) + new String(qualifierArray)
+ new String(valueArray));
}
System.out.println("---------------按行键查询表数据 END-----------------");
}
/**
* 按条件查询表数据
*/
@Test
public void queryTableByCondition() throws IOException{
System.out.println("---------------按条件查询表数据 START-----------------");
// 取得数据表对象
Table table = connection.getTable(TableName.valueOf("t_book"));
// 创建一个查询过滤器
Filter filter = new SingleColumnValueFilter(Bytes.toBytes("base"), Bytes.toBytes("name"),
CompareOp.EQUAL, Bytes.toBytes("bookName6"));
// 创建一个数据表扫描器
Scan scan = new Scan();
// 将查询过滤器加入到数据表扫描器对象
scan.setFilter(filter);
// 执行查询操作,并取得查询结果
ResultScanner scanner = table.getScanner(scan);
// 循环输出查询结果
for (Result result : scanner) {
byte[] row = result.getRow();
System.out.println("row key is:" + new String(row));
List<Cell> listCells = result.listCells();
for (Cell cell : listCells) {
byte[] familyArray = cell.getFamilyArray();
byte[] qualifierArray = cell.getQualifierArray();
byte[] valueArray = cell.getValueArray();
System.out.println("row value is:" + new String(familyArray) + new String(qualifierArray)
+ new String(valueArray));
}
}
System.out.println("---------------按条件查询表数据 END-----------------");
}
/**
* 清空表
*/
@Test
public void truncateTable() throws IOException{
System.out.println("---------------清空表 START-----------------");
// 取得目标数据表的表名对象
TableName tableName = TableName.valueOf("t_book");
// 设置表状态为无效
admin.disableTable(tableName);
// 清空指定表的数据
admin.truncateTable(tableName, true);
System.out.println("---------------清空表 End-----------------");
}
/**
* 删除表
*/
@Test
public void deleteTable() throws IOException{
System.out.println("---------------删除表 START-----------------");
// 设置表状态为无效
admin.disableTable(TableName.valueOf("t_book"));
// 删除指定的数据表
admin.deleteTable(TableName.valueOf("t_book"));
System.out.println("---------------删除表 End-----------------");
}
/**
* 删除行
*/
@Test
public void deleteByRowKey() throws IOException{
System.out.println("---------------删除行 START-----------------");
// 取得待操作的数据表对象
Table table = connection.getTable(TableName.valueOf("t_book"));
// 创建删除条件对象
Delete delete = new Delete(Bytes.toBytes("row2"));
// 执行删除操作
table.delete(delete);
System.out.println("---------------删除行 End-----------------");
}
/**
* 删除行(按条件)
*/
@Test
public void deleteByCondition() throws IOException, DeserializationException{
System.out.println("---------------删除行(按条件) START-----------------");
// 步骤1:调用queryTableByCondition()方法取得需要删除的数据列表
// 步骤2:循环步骤1的查询结果,对每个结果调用deleteByRowKey()方法
System.out.println("---------------删除行(按条件) End-----------------");
}
/**
* 新建列族
*/
@Test
public void addColumnFamily() throws IOException{
System.out.println("---------------新建列族 START-----------------");
// 取得目标数据表的表名对象
TableName tableName = TableName.valueOf("t_book");
// 创建列族对象
HColumnDescriptor columnDescriptor = new HColumnDescriptor("more");
// 将新创建的列族添加到指定的数据表
admin.addColumn(tableName, columnDescriptor);
System.out.println("---------------新建列族 END-----------------");
}
/**
* 删除列族
*/
@Test
public void deleteColumnFamily() throws IOException{
System.out.println("---------------删除列族 START-----------------");
// 取得目标数据表的表名对象
TableName tableName = TableName.valueOf("t_book");
// 删除指定数据表中的指定列族
admin.deleteColumn(tableName, "more".getBytes());
System.out.println("---------------删除列族 END-----------------");
}
/**
* 插入数据
*/
@Test
public void insert() throws IOException{
System.out.println("---------------插入数据 START-----------------");
// 取得一个数据表对象
Table table = connection.getTable(TableName.valueOf("t_book"));
// 需要插入数据库的数据集合
List<Put> putList = new ArrayList<Put>();
Put put;
// 生成数据集合
for(int i = 0; i < 10; i++){
put = new Put(Bytes.toBytes("row" + i));
put.addColumn(Bytes.toBytes("base"), Bytes.toBytes("name"), Bytes.toBytes("bookName" + i));
putList.add(put);
}
// 将数据集合插入到数据库
table.put(putList);
System.out.println("---------------插入数据 END-----------------");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
遇到的问题
详细描述:
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
原因分析: 缺少jar:hamcrest-core-1.3.jar
解决方案:
导入jar:hamcrest-core-1.3.jar
jar文件来源:\hadoop-2.6.0\share\hadoop\common\lib\