HBase(2)JAVA Client

本文详细介绍了如何使用Java客户端进行HBase操作,包括创建表、删除表、插入记录、删除记录、查询单条记录、显示所有记录等功能。通过实际代码示例,展示了HBase在大数据场景下的应用。

HBase(2)JAVA Client

First of all, how to run jar based on maven plugin.
pom.xml
<build>  

 <plugins>   

  <plugin>   

   <groupId>org.apache.maven.plugins</groupId>

   <artifactId>maven-jar-plugin</artifactId>

   <configuration>

    <archive>

     <manifest>

      <mainClass>com.sillycat.easyhbase.ExecutorApp</mainClass>

     </manifest>

    </archive>

   </configuration>

  </plugin>

 </plugins>  

</build>

And the ExecutorApp is just a simplest Java Application.
package com.sillycat.easyhbase;

 

public class ExecutorApp {      

 public static void main(String[] args) {          

   System.out.println("Nice try.");      

 }

}

Fire and build the jar
>mvn clean install

Run the jar
>java -jar target/easyhbase-1.0.jar 
Nice try.


Error Message
08-05 17:15:46 [WARN] org.apache.hadoop.hbase.util.DynamicClassLoader.<init>(DynamicClassLoader.java:106) - Failed to identify the fs of dir hdfs://ubuntu-master:9000/hbase/lib, ignored java.io.IOException: No FileSystem for scheme: hdfs at org.apache.hadoop.fs.FileSystem.getFileSystemClass(FileSystem.java:2385) at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:2392) at org.apache.hadoop.fs.FileSystem.access$200(FileSystem.java:89) at org.apache.hadoop.fs.FileSystem$Cache.getInternal(FileSystem.java:2431) at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:2413) at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:368) at org.apache.hadoop.fs.Path.getFileSystem(Path.java:296) at org.apache.hadoop.hbase.util.DynamicClassLoader.<init>(DynamicClassLoader.java:104) at org.apache.hadoop.hbase.protobuf.ProtobufUtil.<clinit>(ProtobufUtil.java:202)

Solution:
After add this jar, the problem solved.
<dependency>     

 <groupId>org.apache.hadoop</groupId>

  <artifactId>hadoop-hdfs</artifactId>

  <version>${hadoop.version}</version>

</dependency>

Error Message
An internal error occurred during: "Updating Maven Project".
Unsupported IClasspathEntry kind=4

Solution:
Because I am using eclipse, so I use command mvn eclipse:eclipse to generate the jar dependencies. 
Right click on the project [Properties] —> [Java Build Path] —>[Library] 
Remove all the blue entries starting with “M2_REPO”, then I can use Maven—>Update Project again. Even I can download the source codes.

All the codes are in project easyhbase.

The dependencies are as follow:
<properties>  

 <hadoop.version>2.4.1</hadoop.version>  

 <hbase.version>0.98.4-hadoop2</hbase.version>

</properties> 

 

<dependencies>     

 <dependency>          

  <groupId>commons-logging</groupId>          

  <artifactId>commons-logging</artifactId>          

  <version>1.1.3</version>     

 </dependency>     

 

 <dependency>         

  <groupId>org.apache.hbase</groupId>         

  <artifactId>hbase-hadoop2-compat</artifactId>         

  <version>${hbase.version}</version>     

 </dependency>     

 

 <dependency>         

  <groupId>org.apache.hbase</groupId>

  <artifactId>hbase-common</artifactId>         

  <version>${hbase.version}</version>     

 </dependency>     

 

 <dependency>         

  <groupId>org.apache.hbase</groupId>         

  <artifactId>hbase-client</artifactId>         

  <version>${hbase.version}</version>     

 </dependency>     

 

 <dependency>           

  <groupId>org.apache.hadoop</groupId>           

  <artifactId>hadoop-common</artifactId>           

  <version>${hadoop.version}</version>     

 </dependency>     

 

 <dependency>         

  <groupId>org.apache.hadoop</groupId>         

  <artifactId>hadoop-hdfs</artifactId>         

  <version>${hadoop.version}</version>     

 </dependency>     

 

 <dependency>         

  <groupId>log4j</groupId>         

  <artifactId>log4j</artifactId>         

  <version>1.2.17</version>     

 </dependency>

</dependencies>

The JAVA Client Sample Codes
package com.sillycat.easyhbase;

 

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.CellUtil;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.HColumnDescriptor;

import org.apache.hadoop.hbase.HTableDescriptor;

import org.apache.hadoop.hbase.MasterNotRunningException;

import org.apache.hadoop.hbase.TableName;

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.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.util.Bytes;

 

public class HBaseMain {

  private static Configuration conf = null;

 // setting

 static {

  conf = HBaseConfiguration.create();

 }

 // create table

 

 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 {

   

  TableName tableNameObject = TableName.valueOf(tableName);

  HTableDescriptor tableDesc = new HTableDescriptor(tableNameObject);

  for (inti = 0; i < familys.length; i++) {

   tableDesc.addFamily(new HColumnDescriptor(familys[i]));

  }

  admin.createTable(tableDesc);

  System.out.println("create table " + tableName + " ok.");

 }

}

 

// delete table

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();

  }

}

 

// insert one line

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();

}

}

// delete one line

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.");

}

 

// query for one line

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 (Cell cell : rs.rawCells()) {

   System.out.print(new String(CellUtil.cloneRow(cell)) + " ");

   System.out.print(new String(CellUtil.cloneFamily(cell)) + ":");

   System.out.print(new String(CellUtil.cloneQualifier(cell)) + " ");

   System.out.print(cell.getTimestamp() + " ");

   System.out.println(new String(CellUtil.cloneValue(cell))); }

}

 

// list all data

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 (Cell cell : r.rawCells()) {

      System.out.print(new String(CellUtil.cloneRow(cell)) + " ");

      System.out.print(new String(CellUtil.cloneFamily(cell)) + ":");

      System.out.print(new String(CellUtil.cloneQualifier(cell)) + " ");

      System.out.print(cell.getTimestamp() + " ");

      System.out.println(new String(CellUtil.cloneValue(cell)));

     }

   }

  } catch (IOException e) {

   e.printStackTrace();

  }

 }

 

public static void getRangeRecord(String tableName,String startRowKey,String endRowKey){

  try {

    HTable table = new HTable(conf, tableName);

    Scan s = new Scan(startRowKey.getBytes(),endRowKey.getBytes());

    ResultScanner ss = table.getScanner(s);

    for (Result r : ss) {

      for (Cell cell : r.rawCells()) {

       System.out.print(new String(CellUtil.cloneRow(cell)) + " ");

       System.out.print(new String(CellUtil.cloneFamily(cell)) + ":");

       System.out.print(new String(CellUtil.cloneQualifier(cell)) + " ");

       System.out.print(cell.getTimestamp() + " ");

       System.out.println(new String(CellUtil.cloneValue(cell)));

      }

     }

    } catch (IOException e) {

      e.printStackTrace();

    }

   }

 

public static void main(String[] args) {

    try {

      String tablename = "scores"; String[] familys = { "grade", "course" };

      HBaseMain.creatTable(tablename, familys);

 

      // add record sillycat

      HBaseMain.addRecord(tablename, "sillycat-20140723", "grade", "", "5");

      HBaseMain.addRecord(tablename, "sillycat-20140723", "course", "math", "97");      

      HBaseMain.addRecord(tablename, "sillycat-20140723", "course", "art", "87");

      HBaseMain.addRecord(tablename, "sillycat-20130723", "grade", "", "5");

      HBaseMain.addRecord(tablename, "sillycat-20130723", "course", "math", "97");

      HBaseMain.addRecord(tablename, "sillycat-20130723", "course", "art", "87");

      HBaseMain.addRecord(tablename, "sillycat-20120723", "grade", "", "5");

      HBaseMain.addRecord(tablename, "sillycat-20120723", "course", "math", "97");

      HBaseMain.addRecord(tablename, "sillycat-20120723", "course", "art", "87");

 

      // add record kiko

      HBaseMain.addRecord(tablename, "kiko-20140723", "grade", "", "4");

      HBaseMain.addRecord(tablename, "kiko-20140723", "course", "math", "89");

 

      System.out.println("===========get one record========");

      HBaseMain.getOneRecord(tablename, "sillycat");

      System.out.println("===========show all record========");

      HBaseMain.getAllRecord(tablename);

      System.out.println("===========del one record========");

      HBaseMain.delRecord(tablename, "kiko");

      HBaseMain.getAllRecord(tablename);

      System.out.println("===========show all record========");

      HBaseMain.getAllRecord(tablename);

      System.out.print("=============show range record=======");

      HBaseMain.getRangeRecord(tablename, "sillycat-20130101", "sillycat-20141231");

    } catch (Exception e) {

      e.printStackTrace();

    }

   }

  }

References:
http://www.cnblogs.com/panfeng412/archive/2011/08/14/hbase-java-client-programming.html
http://lirenjuan.iteye.com/blog/1470645
http://hbase.apache.org/book/hbase_apis.html

http://blog.linezing.com/?tag=hbase
http://f.dataguru.cn/thread-226503-1-1.html
http://www.cnblogs.com/ggjucheng/p/3379459.html
http://blog.sina.com.cn/s/blog_ae33b83901016azb.html
http://blog.nosqlfan.com/html/3694.html
http://www.infoq.com/cn/news/2011/07/taobao-linhao-hbase
http://blog.youkuaiyun.com/xunianchong/article/details/8995019

client performance
http://blog.linezing.com/?p=1378
design row key
http://san-yun.iteye.com/blog/1995829

<think>我们正在使用Java编写HBase客户端示例代码。根据引用[2][3][4][5],我们可以构建一个简单的示例,包括连接HBase、创建表、插入数据、查询数据等步骤。 注意:引用[3]提到需要配置文件,引用[4]提到将配置文件复制到资源目录,引用[5]给出了RowKey设计的注意事项。 步骤: 1. 添加Maven依赖(如果使用Maven) 2. 配置HBase连接(通过hbase-site.xml和core-site.xml,或者代码中配置) 3. 获取连接(使用Connection) 4. 操作表(创建、插入、查询等) 由于引用中提到了RowKey设计,我们在插入数据时要注意RowKey的构造。 下面是一个简单的示例代码,包括: - 创建表 - 插入数据 - 查询数据 - 删除表(可选,一般不建议在代码中删除表) 注意:实际应用中,表结构和列族需要预先设计好。 引用[5]中提到RowKey设计为:call_[Long.MAX_VALUE - timestamp],以避免热点问题。我们在插入数据时按照这种方式生成RowKey。 由于示例需要,我们假设表名为"test_table",列族为"cf"。 但是,请注意:在真实的HBase环境中,表需要预先创建或通过代码创建,但通常表结构设计是预先规划好的。 我们将编写一个完整的示例,包括以下步骤: 1. 创建配置和连接 2. 创建表(如果不存在) 3. 插入数据(使用特定的RowKey生成方式) 4. 按照RowKey查询数据 5. 关闭连接 由于引用内容中没有提供具体的代码,我们根据HBase Java API的常规用法编写。 注意:以下代码仅为示例,实际运行需要HBase环境以及相应的配置文件(hbase-site.xml等)。 另外,引用[4]提到将Hadoop和HBase的配置文件复制到项目的src/main/resources目录下,这样在代码中调用HBaseConfiguration.create()时会自动加载这些配置。 开始编写代码示例: </think>以下是使用Java编写的HBase客户端操作示例代码,结合了HBase API的最佳实践和关键配置要点: ### 基本连接与配置 ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; public class HBaseClientExample { private static final String TABLE_NAME = "user_activity"; private static final String COLUMN_FAMILY = "cf_stats"; public static void main(String[] args) { try (Connection connection = getConnection()) { Table table = connection.getTable(TableName.valueOf(TABLE_NAME)); // 示例操作 putData(table); // 插入数据 getData(table); // 查询数据 scanData(table); // 扫描数据 } catch (Exception e) { e.printStackTrace(); } } // 创建HBase连接(自动加载hbase-site.xml配置) private static Connection getConnection() throws Exception { Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "zk1.example.com,zk2.example.com"); return ConnectionFactory.createConnection(config); } } ``` ### 关键操作实现 #### 1. 数据插入(Put操作) ```java private static void putData(Table table) throws Exception { // 生成RowKey:md5(业务键)_[Long.MAX_VALUE - 时间戳] String caller = "user123"; long timestamp = System.currentTimeMillis(); String rowKey = HashUtil.md5(caller) + "_" + (Long.MAX_VALUE - timestamp); Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn( Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes("click_count"), Bytes.toBytes(150L) ); put.addColumn( Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes("last_access"), Bytes.toBytes(timestamp) ); table.put(put); System.out.println("数据插入成功,RowKey: " + rowKey); } ``` #### 2. 单行查询(Get操作) ```java private static void getData(Table table) throws Exception { Get get = new Get(Bytes.toBytes("md5_user123_9223372036854775700")); Result result = table.get(get); if (!result.isEmpty()) { long clickCount = Bytes.toLong( result.getValue(Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes("click_count")) ); System.out.println("查询结果 - 点击次数: " + clickCount); } } ``` #### 3. 范围扫描(Scan操作) ```java private static void scanData(Table table) throws Exception { Scan scan = new Scan(); // 设置扫描范围(反向时间戳范围) scan.setStartRow(Bytes.toBytes("md5_user123_" + (Long.MAX_VALUE - 1700000000000L))); scan.setStopRow(Bytes.toBytes("md5_user123_" + (Long.MAX_VALUE - 1600000000000L))); try (ResultScanner scanner = table.getScanner(scan)) { for (Result result : scanner) { byte[] value = result.getValue( Bytes.toBytes(COLUMN_FAMILY), Bytes.toBytes("last_access") ); System.out.println("扫描到记录 - 最后访问: " + Bytes.toLong(value)); } } } ``` ### 关键配置说明 1. **配置文件加载** 项目`src/main/resources`目录需包含: - `hbase-site.xml`:HBase集群配置(ZooKeeper地址等) - `core-site.xml`:Hadoop核心配置(HDFS地址等)[^4] 2. **RowKey设计原则** - 使用`业务键_MD5`前缀避免热点问题 - 反向时间戳`(Long.MAX_VALUE - timestamp)`保证新数据排在前面 - 示例格式:`md5(caller)_[Long.MAX_VALUE - timestamp]`[^5] 3. **依赖配置(Maven)** ```xml <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.4.11</version> </dependency> ``` ### 注意事项 1. 连接复用:使用`ConnectionFactory`创建长连接,避免频繁创建销毁 2. 资源释放:所有`Table`和`ResultScanner`实例必须显式关闭 3. 批量操作:建议使用`BufferedMutator`进行批量写入 4. 异步操作:高并发场景可使用`AsyncConnection` > 完整示例需补充异常处理和工具类(如`HashUtil.md5()`),实际RowKey设计应根据业务需求优化[^5][^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值