HBase Java API
1.HBaseConfigUtil
a.设置一个Hadoop的配置类
b.使用set设置zookeeper所在的ip,zookeeper所在端口,以及HBase HMaster节点所在的ip和端口
c.使用addSource配置Hadoop配置文件所在的目录和HBase所在文件的目录
2.CreateTable
a.设置一个config对象,一个connection对象,一个admin对象,其中config对象用于调用HBaseConfigUtil,connection对象用于放置config,admin对象用于客户端连接
b.设置表名,创建HTableDesciptor对象,用于设置表名和设置列族。
3.InsertTable
a.设置一个config对象,一个connection对象,一个Table对象,其中config对象用于调用HBaseConfigUtil,connection对象用于放置config,Table对象通过connection获取表名。
b.针对一个二维数组,批量插入数据,使用for循环
4.ReadTable(根据rowkey去查询表中内容)
a.设置一个config对象,一个connection对象,一个Table对象,其中config对象用于调用HBaseConfigUtil,connection对象用于放置config,Table对象通过connection获取表名。
b.设置一个Get对象,用于获取rowkey,设置一个Result对象,用于存储Get到的结果,由于Get到的是字节数组,所以需要使用Bytes.toString()将数据转为字符串进行显示
5.ListTable(列举所有表)
a.设置一个config对象,一个connection对象,一个admin对象,其中config对象用于调用HBaseConfigUtil,connection对象用于放置config,admin对象用于客户端连接
b.设置表名,创建HTableDesciptor对象,使用admin对象的listTable方法获取到
TableDesciptor数组,遍历数组,调用该数组的getNameAsString方法将表名转为字符串。
6.ScanTable(扫描表中内容)
a.设置一个config对象,一个connection对象,一个Table对象,其中config对象用于调用HBaseConfigUtil,connection对象用于放置config,Table对象通过connection获取表名.
b.设置一个ResultScanner对象,设置一个Scan对象,首先设置Scan对象中需要扫描的列族+列修饰符,将Scan对象塞入ResultScanner对象。
c.ResultScanner下有一个next方法,使用for循环遍历得到每列具体的内容,是一个字节数组,转为String后打印,方法是Bytes.toString()
package com.kgc.demo.util;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
public class HBaseConfigUtil {
public static Configuration getHBaseConfiguration(){
Configuration configuration = new Configuration();
// configuration.set("hbase.zookeeper.quorum", "localhost");
// configuration.set("hbase.zookeeper.property.clientPort", "2181");
// configuration.set("hbase.master","localhost:16000");
configuration.addResource(new Path("/etc/hbase/conf/hbase-site.xml"));
configuration.addResource(new Path("/etc/hadoop/conf/core-site.xml"));
return configuration;
}
}
package com.kgc.demo.crud;
import com.kgc.demo.util.HBaseConfigUtil;
import org.apache.hadoop.conf.Configuration;
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;
public class CreateTable {
public static void main(String[] args) throws Exception{
Configuration config = HBaseConfigUtil.getHBaseConfiguration();
Connection connection = null;
Admin admin = null;
connection =ConnectionFactory.createConnection(config);
admin = connection.getAdmin();
String tableName = "student";
if (!admin.isTableAvailable(TableName.valueOf(tableName))){
HTableDescriptor hbaseTable = new HTableDescriptor(TableName.valueOf(tableName));
hbaseTable.addFamily(new HColumnDescriptor("name"));
hbaseTable.addFamily(new HColumnDescriptor("stuinfo"));
admin.createTable(hbaseTable);
}
if (admin!=null){
admin.close();
}
if (connection!=null&&connection.isClosed()){
connection.close();
}
}
}
package com.kgc.demo.crud;
import com.kgc.demo.util.HBaseConfigUtil;
import org.apache.hadoop.conf.Configuration;
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 org.apache.hadoop.hbase.util.Bytes;
public class InsertTable {
public void insert()throws Exception{
Configuration config = HBaseConfigUtil.getHBaseConfiguration();
Connection connection = null;
Table table = null;
connection = ConnectionFactory.createConnection(config);
table = connection.getTable(TableName.valueOf("student"));
//批量插入数据
String [][]students = {
{ "1", "Marcel", "Haddad", "marcel@xyz.com", "M", "26" },
{ "2", "Franklin", "Holtz", "franklin@xyz.com", "M", "24" },
{ "3", "Dwayne", "McKee", "dwayne@xyz.com", "M", "27" },
{ "4", "Rae", "Schroeder", "rae@xyz.com", "F", "31" },
{ "5", "Rosalie", "burton", "rosalie@xyz.com", "F", "25" },
{ "6", "Gabriela", "Ingram", "gabriela@xyz.com", "F", "24" } };
for (int i = 0; i <students.length ; i++) {
Put studentPut = new Put(Bytes.toBytes(students[i][0]));
studentPut.addColumn(Bytes.toBytes("name"),Bytes.toBytes("firstname"),Bytes.toBytes(students[i][1]));
studentPut.addColumn(Bytes.toBytes("name"),Bytes.toBytes("lastname"),Bytes.toBytes(students[i][2]));
studentPut.addColumn(Bytes.toBytes("stuinfo"),Bytes.toBytes("email"),Bytes.toBytes(students[i][3]));
studentPut.addColumn(Bytes.toBytes("stuinfo"),Bytes.toBytes("gender"),Bytes.toBytes(students[i][4]));
studentPut.addColumn(Bytes.toBytes("stuinfo"),Bytes.toBytes("age"),Bytes.toBytes(students[i][5]));
table.put(studentPut);
}
}
public static void main(String[] args)throws Exception {
InsertTable object = new InsertTable();
object.insert();
}
}
package com.kgc.demo.crud;
import com.kgc.demo.util.HBaseConfigUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
public class ListTable {
public static void main(String[] args) throws Exception{
ListTable object = new ListTable();
object.list();
}
public void list()throws Exception{
Configuration config = HBaseConfigUtil.getHBaseConfiguration();
Connection connection = null;
Admin admin = null;
connection = ConnectionFactory.createConnection(config);
admin = connection.getAdmin();
HTableDescriptor tableDescriptor[] = admin.listTables();
for (int i = 0; i <tableDescriptor.length ; i++) {
System.out.println(tableDescriptor[i].getNameAsString());
}
}
}
package com.kgc.demo.crud;
import com.kgc.demo.util.HBaseConfigUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
public class ReadTable {
public void read(String rowKey)throws Exception{
Configuration config = HBaseConfigUtil.getHBaseConfiguration();
Connection connection = null;
Table table = null;
connection = ConnectionFactory.createConnection(config);
table = connection.getTable(TableName.valueOf("student"));
Get get = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get);
byte[] firstNameValue = result.getValue(Bytes.toBytes("name"), Bytes.toBytes("firstname"));
byte[] lastNameValue = result.getValue(Bytes.toBytes("name"), Bytes.toBytes("lastname"));
byte[] emailValue = result.getValue(Bytes.toBytes("stuinfo"), Bytes.toBytes("email"));
byte[] genderValue = result.getValue(Bytes.toBytes("stuinfo"), Bytes.toBytes("gender"));
byte[] ageValue = result.getValue(Bytes.toBytes("stuinfo"), Bytes.toBytes("age"));
String firstName = Bytes.toString(firstNameValue);
String lastName = Bytes.toString(lastNameValue);
String email = Bytes.toString(emailValue);
String gender = Bytes.toString(genderValue);
String age = Bytes.toString(ageValue);
System.out.println("First Name : " + firstName + " --- Last Name : " + lastName + " --- Email : " + email + " --- Gender : " + gender + " --- Age : " + age);
}
public static void main(String[] args)throws Exception {
ReadTable object = new ReadTable();
System.out.println("please input the tablename");
object.read(args[0]);
}
}
package com.kgc.demo.crud;
import com.kgc.demo.util.HBaseConfigUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
public class ScanTable {
public static void main(String[] args) throws Exception{
Configuration config = HBaseConfigUtil.getHBaseConfiguration();
Connection connection =null;
Table table = null;
ResultScanner resultScanner = null;
connection = ConnectionFactory.createConnection(config);
table = connection.getTable(TableName.valueOf("student"));
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes("name"), Bytes.toBytes("firstname"));
scan.addColumn(Bytes.toBytes("name"), Bytes.toBytes("lastname"));
scan.addColumn(Bytes.toBytes("stuinfo"), Bytes.toBytes("email"));
scan.addColumn(Bytes.toBytes("stuinfo"), Bytes.toBytes("gender"));
scan.addColumn(Bytes.toBytes("stuinfo"), Bytes.toBytes("age"));
resultScanner = table.getScanner(scan);
for (Result result = resultScanner.next();result!=null;result=resultScanner.next()){
byte[] firstNameValue = result.getValue(Bytes.toBytes("name"), Bytes.toBytes("firstname"));
byte[] lastNameValue = result.getValue(Bytes.toBytes("name"), Bytes.toBytes("lastname"));
byte[] emailValue = result.getValue(Bytes.toBytes("stuinfo"), Bytes.toBytes("email"));
byte[] genderValue = result.getValue(Bytes.toBytes("stuinfo"), Bytes.toBytes("gender"));
byte[] ageValue = result.getValue(Bytes.toBytes("stuinfo"), Bytes.toBytes("age"));
String firstName = Bytes.toString(firstNameValue);
String lastName = Bytes.toString(lastNameValue);
String email = Bytes.toString(emailValue);
String gender = Bytes.toString(genderValue);
String age = Bytes.toString(ageValue);
System.out.println("First Name : " + firstName + " --- Last Name : " + lastName + " --- Email : " + email + " --- Gender : " + gender + " --- Age : " + age);
}
}
}
**