本篇文章给大家谈谈java连接hbase等待,以及java连接hbase集群对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、hbase java2、如何使用Java API操作Hbase3、求助帖,hbase新手,windows中的java怎么连接linux中的hbase
hbase java
hbase java是什么,让我们一起了解一下?
HBase是一个分布式的、面向列的开源数据库,具有高可靠性、高性能、面向列、可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群。
如何使用JAVA语言操作Hbase、整合Hbase?
可分为五步骤:
步骤1:新创建一个Java Project 。
步骤2:导入JAR包,在工程根目录下新建一个“lib”文件夹,将官方文档中的lib目录下的jar全部导入。
步骤3:修改开发机的hosts文件,在文件莫为增加一行虚拟机IP的映射信息。
步骤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 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 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 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 putList = new ArrayList (); Put put; // 生成数据集合 for(int i = 0; i
如何使用Java API操作Hbase
HBase提供了对HBase进行一系列的管理涉及到对表的管理、数据的操作java api。
常用的API操作有:
1、 对表的创建、删除、显示以及修改等,可以用HBaseAdmin,一旦创建了表,那么可以通过HTable的实例来访问表,每次可以往表里增加数据。
2、 插入数据
创建一个Put对象,在这个Put对象里可以指定要给哪个列增加数据,以及当前的时间戳等值,然后通过调用HTable.put(Put)来提交操作,子猴在这里提请注意的是:在创建Put对象的时候,你必须指定一个行(Row)值,在构造Put对象的时候作为参数传入。
3、 获取数据
要获取数据,使用Get对象,Get对象同Put对象一样有好几个构造函数,通常在构造的时候传入行值,表示取第几行的数据,通过HTable.get(Get)来调用。
4、 浏览每一行
通过Scan可以对表中的行进行浏览,得到每一行的信息,比如列名,时间戳等,Scan相当于一个游标,通过next()来浏览下一个,通过调用HTable.getScanner(Scan)来返回一个ResultScanner对象。HTable.get(Get)和HTable.getScanner(Scan)都是返回一个Result。Result是一个
KeyValue的链表。
5、 删除
使用Delete来删除记录,通过调用HTable.delete(Delete)来执行删除操作。(注:删除这里有些特别,也就是删除并不是马上将数据从表中删除。)
6、 锁
新增、获取、删除在操作过程中会对所操作的行加一个锁,而浏览却不会。
7、 簇的访问
客户端代码通过ZooKeeper来访问找到簇,也就是说ZooKeeper quorum将被使用,那么相关的类(包)应该在客户端的类(classes)目录下,即客户端一定要找到文件hbase-site.xml。
下面是一个例子程序:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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.Put;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseTest {
private static Configuration conf = null;
/**
* 初始化配置
*/
static {
Configuration HBASE_CONFIG = new Configuration();
//与hbase/conf/hbase-site.xml中hbase.zookeeper.quorum配置的值相同
HBASE_CONFIG.set(“hbase.zookeeper.quorum”, “10.1.1.1”);
//与hbase/conf/hbase-site.xml中hbase.zookeeper.property.clientPort配置的值相同
HBASE_CONFIG.set(“hbase.zookeeper.property.clientPort”, “2181”);
conf = HBaseConfiguration.create(HBASE_CONFIG);
}
/**
* 创建一张表
*/
public static void creatTable(String tableName, String[] familys) throws Exception {
HBaseAdmin admin = new HBaseAdmin(conf);
if (admin.tableExists(tableName)) {
System.out.println(“table already exists!”);
} else {
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
for(int i=0; ifamilys.length; i++){
tableDesc.addFamily(new HColumnDescriptor(familys[i]));
}
admin.createTable(tableDesc);
System.out.println(“create table ” + tableName + ” ok.”);
}
}
/**
* 删除表
*/
public static void deleteTable(String tableName) throws Exception {
try {
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println(“delete table ” + tableName + ” ok.”);
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
}
}
/**
* 插入一行记录
*/
public static void addRecord (String tableName, String rowKey, String family, String qualifier, String value)
throws Exception{
try {
HTable table = new HTable(conf, tableName);
Put put = new Put(Bytes.toBytes(rowKey));
put.add(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value));
table.put(put);
System.out.println(“insert recored ” + rowKey + ” to table ” + tableName +” ok.”);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除一行记录
*/
public static void delRecord (String tableName, String rowKey) throws IOException{
HTable table = new HTable(conf, tableName);
List list = new ArrayList();
Delete del = new Delete(rowKey.getBytes());
list.add(del);
table.delete(list);
System.out.println(“del recored ” + rowKey + ” ok.”);
}
/**
* 查找一行记录
*/
public static void getOneRecord (String tableName, String rowKey) throws IOException{
HTable table = new HTable(conf, tableName);
Get get = new Get(rowKey.getBytes());
Result rs = table.get(get);
for(KeyValue kv : rs.raw()){
System.out.print(new String(kv.getRow()) + ” ” );
System.out.print(new String(kv.getFamily()) + “:” );
System.out.print(new String(kv.getQualifier()) + ” ” );
System.out.print(kv.getTimestamp() + ” ” );
System.out.println(new String(kv.getValue()));
}
}
/**
* 显示所有数据
*/
public static void getAllRecord (String tableName) {
try{
HTable table = new HTable(conf, tableName);
Scan s = new Scan();
ResultScanner ss = table.getScanner(s);
for(Result r:ss){
for(KeyValue kv : r.raw()){
System.out.print(new String(kv.getRow()) + ” “);
System.out.print(new String(kv.getFamily()) + “:”);
System.out.print(new String(kv.getQualifier()) + ” “);
System.out.print(kv.getTimestamp() + ” “);
System.out.println(new String(kv.getValue()));
}
}
} catch (IOException e){
e.printStackTrace();
}
}
public static void main (String [] agrs) {
try {
String tablename = “scores”;
String[] familys = {“grade”, “course”};
HBaseTest.creatTable(tablename, familys);
//add record zkb
HBaseTest.addRecord(tablename,”zkb”,”grade”,””,”5″);
HBaseTest.addRecord(tablename,”zkb”,”course”,””,”90″);
HBaseTest.addRecord(tablename,”zkb”,”course”,”math”,”97″);
HBaseTest.addRecord(tablename,”zkb”,”course”,”art”,”87″);
//add record baoniu
HBaseTest.addRecord(tablename,”baoniu”,”grade”,””,”4″);
HBaseTest.addRecord(tablename,”baoniu”,”course”,”math”,”89″);
System.out.println(“===========get one record========”);
HBaseTest.getOneRecord(tablename, “zkb”);
System.out.println(“===========show all record========”);
HBaseTest.getAllRecord(tablename);
System.out.println(“===========del one record========”);
HBaseTest.delRecord(tablename, “baoniu”);
HBaseTest.getAllRecord(tablename);
System.out.println(“===========show all record========”);
HBaseTest.getAllRecord(tablename);
} catch (Exception e) {
e.printStackTrace();
}
}
}
求助帖,hbase新手,windows中的java怎么连接linux中的hbase
一、新建本地java工程
file-new-java project
二、添加jar包和配置文件
1、添加JAR包
右击Propertie在弹出的快捷菜单中选择Java Build Path对话框,在该对话框中单击Libraries选项卡,在该选项卡下单击
Add External JARs按钮,定位到$HBASE/lib目录下,并选取如下JAR包。
hadoop-core-1.0.0.jar
commons-loggings-version.jar
commons-cli-version.jar
commons-lang-version.jar
commons-configuration-version.jar
hbase-0.94.1.jar
zookeeper-3.4.3.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
log4j-1.2.16.jar
protobuf-java-2.4.1.jar
2、添加hbase-site.xml配置文件
在工程根目录下创建conf文件夹,将$HBASE_HOME/conf/目录中的hbase-site.xml文件复制到该文件夹中。通过右键
选择Propertie-Java Build Path-Libraries-Add Class Folder。
3、windows下开发HBase应用程序,HBase部署在linux环境中,在运行调试时可能会出现无法找到主机,类似异常信息如下:java.net.UnknownHostException: unknown host: master
解决办法如下:在C:\WINDOWS\system32\drivers\etc\hosts文件中添加如下信息
192.168.2.34 master
关于java连接hbase等待和java连接hbase集群的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。