1. 前言
HBase Shell可以基于后台访问HBase,但是不如java访问写起来方便一些,所以下面实现如何基于Java API实现对HBase的远程操作。
——————————————————————————————————————————在开发之前将hbase的lib里面的jar包导入到我们的项目依赖中:
2. 创建表
实例代码:
package hbase;
import net.spy.memcached.ConnectionFactoryBuilder;
import org.apache.hadoop.conf.Configuration;
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 java.io.IOException;
public class createTable {
public static void main(String[] args) {
System.setProperty("HADOOP_USER_NAME", "hadoop");
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "slave001,slave002,slave003");
try {
//建立连接
Connection connection = ConnectionFactory.createConnection(configuration);
//获取Admin
Admin admin = connection.getAdmin();
//添加表的描述
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("test"));
//添加列族
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("column1");
tableDescriptor.addFamily(hColumnDescriptor);
admin.createTable(tableDescriptor);
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. 插入数据
实例代码:
package hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import java.io.IOException;
public class addData {
public static void main(String[] args) {
System.setProperty("HADOOP_USER_NAME", "hadoop");
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "slave001,slave002,slave003");
try {
Connection connection = ConnectionFactory.createConnection(configuration);
//确定连接的表
Table table = connection.getTable(TableName.valueOf("test"));
//设置Row Key
Put put = new Put("lhd".getBytes());
//写入列名及数据
put.addColumn("column1".getBytes(),"age".getBytes(),"22".getBytes());
table.put(put);
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. 查询数据
查询数据分为两种方式,一种是通过get方法获取指定cell的数据值,另一种是通过Scan()方法遍历查询表中的全部数据,下面分别介绍:
- 查询指定cell的值,指定要查询的表名、rowkey、列族、属性。
实例代码:
package hbase;
import javafx.scene.control.Tab;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.jruby.RubyGlobal;
import java.io.IOException;
public class getData {
public static void main(String[] args) {
System.setProperty("HADOOP_USER_NAME", "hadoop");
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "slave001,slave002,slave003");
try{
Connection connection = ConnectionFactory.createConnection(configuration);
Table table = connection.getTable(TableName.valueOf("test"));
//通过Row Key指定行
Get get = new Get("lhd".getBytes());
Result result = table.get(get);
//指定列名,确定cell
Cell cell = result.getColumnLatestCell("column1".getBytes(),"age".getBytes());
//输出cell的Row Key的值
System.out.println(new String(CellUtil.cloneRow(cell), "utf-8"));
System.out.println(new String(CellUtil.cloneValue(cell), "utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
效果截图:
- 遍历查询指定表的全部数据,得到结果集后并输出。
实例代码:
package hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
import java.util.Iterator;
public class getDataByScan {
public static void main(String[] args) {
System.setProperty("HADOOP_USER_NAME", "hadoop");
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "slave001,slave002,slave003");
try {
Connection connection = ConnectionFactory.createConnection(configuration);
Table table = connection.getTable(TableName.valueOf("test"));
//调用Scan方法
Scan scan = new Scan();
//取得遍历查询的结果
ResultScanner resultScanner = table.getScanner(scan);
//对结果集进行迭代
Iterator iterator = resultScanner.iterator();
while(iterator.hasNext()){
Result result = (Result) iterator.next();
Cell cell = result.getColumnLatestCell("column1".getBytes(),"age".getBytes());
System.out.println(new String(CellUtil.cloneRow(cell), "utf-8"));
System.out.println(new String(CellUtil.cloneValue(cell),"utf-8"));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}