Hbase(五): JavaApi操作Hbase
依赖
代码语言:javascript复制<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>0.98.17-hadoop2</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>0.98.17-hadoop2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
依赖的版本应该和Hbase版本一致
Hbase版本可以通过连接Hbase client使用version命令查看
定义静态配置变量
代码语言:javascript复制public static Configuration conf;
static{
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","hadoop1:2181");
}
由于配置变量都是一样的,可以定义成静态变量方便使用
需要注意的是需要在本地做主机ip映射,mac的hostname文件在etc目录下
创建表
代码语言:javascript复制//创建表
@Test
public void createTable() throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("users"));
HColumnDescriptor c1 = new HColumnDescriptor("c1");
c1.setMaxVersions(3);
//HColumnDescriptor c2 = new HColumnDescriptor("c2");
htd.addFamily(c1);
//htd.addFamily(c2);
admin.createTable(htd);
admin.close();
}
插入数据
代码语言:javascript复制/**
* 插入数据
*/
@Test
public void insertData() throws IOException {
HTable table = new HTable(conf,"table");
Put put = new Put(Bytes.toBytes("key_1"));
put.add(Bytes.toBytes("c1"),Bytes.toBytes("name"),Bytes.toBytes("zhangsan"));
table.put(put);
table.close();
}
单例模式插入大量数据
代码语言:javascript复制/**
* 单例模式插入大量数据
* @throws IOException
*/
@Test
public void insertDatas() throws IOException {
Long state = System.currentTimeMillis();
HTable table = new HTable(conf, "table2");
List<Put> puts = new ArrayList<Put>();
for (int i = 1;i <= 1000000; i ){
Put put = new Put(Bytes.toBytes("key_" i));
put.add(Bytes.toBytes("c1"),Bytes.toBytes("name" i),Bytes.toBytes("zhangsan" i));
puts.add(put);
if ( i % 10000 == 0){
table.put(puts);
puts = new ArrayList<Put>();
}
}
table.put(puts);
table.close();
Long over = System.currentTimeMillis();
System.out.println("用时" (over-state)/1000 "秒");
}
获取数据
代码语言:javascript复制/**
* 获取数据
*/
@Test
public void selectData() throws IOException {
HTable table = new HTable(conf, "table");
Get get = new Get(Bytes.toBytes("key_1"));
Result result = table.get(get);
byte[] bytes = result.getValue(Bytes.toBytes("c1"),Bytes.toBytes("name"));
String str = Bytes.toString(bytes);
System.out.println(str);
table.close();
}
获取数据集
代码语言:javascript复制/**
* 获取数据集
*/
@Test
public void selectDatas() throws IOException {
int i=1;
HTable table = new HTable(conf, "table");
Scan scan = new Scan(Bytes.toBytes("key_1"));
ResultScanner scanner = table.getScanner(scan);
Iterator it = scanner.iterator();
while (it.hasNext()){
Result result = (Result) it.next();
byte[] value = result.getValue(Bytes.toBytes("c1"), Bytes.toBytes("name"));
String str = Bytes.toString(value);
System.out.println(str);
i ;
}
table.close();
}
删除数据
代码语言:javascript复制/**
* 删除表数据
* @throws IOException
*/
@Test
public void removeData() throws IOException {
HTable table = new HTable(conf, "table");
Delete key_1 = new Delete(Bytes.toBytes("key_1"));
table.delete(key_1);
table.close();
}
删除表
代码语言:javascript复制/**
* 删除表
* @throws IOException
*/
@Test
public void deleteTable() throws IOException{
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(Bytes.toBytes("table1"));
admin.deleteTable(Bytes.toBytes("table1"));
admin.close();
}