一、添加依赖:文章来源:https://www.toymoban.com/news/detail-821299.html
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.4.2</version>
</dependency>
二、使用Scanner读取数据示例:文章来源地址https://www.toymoban.com/news/detail-821299.html
package cn.edu.tju;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScanner;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
public class TestHBaseRead {
public static void main(String[] args) throws Exception {
Configuration config = HBaseConfiguration.create();// create configuration
//zookeeper 地址
config.set("hbase.zookeeper.quorum","xxx.xxx.xxx.xxx");//
//zookeeper端口
config.set("hbase.zookeeper.property.clientPort", "2181");//
//表名,必须提前在hbase中创建
String tableName ="c1";
//row key
String rowKey = "myKey2";
//family,必须是hbase中有的family
String familyName = "fm2";
//指定的某个column name
String specifiedColumnName = "by";
Connection connection = ConnectionFactory.createConnection(config);
Get g = new Get(rowKey.getBytes());
g.addFamily(familyName.getBytes());
try {
Table table = connection.getTable(TableName.valueOf(tableName));
Result result = table.get(g);
CellScanner cellScanner =result.cellScanner();
while (cellScanner.advance()){
Cell cell = cellScanner.current();
byte[] rowArray = cell.getRowArray(); //row key 字节数组
byte[] familyArray = cell.getFamilyArray(); //列族 字节数组
byte[] qualifierArray = cell.getQualifierArray();//列名 字节数据
byte[] valueArray = cell.getValueArray();// 值 字节数组
String columnName=new String(qualifierArray,cell.getQualifierOffset(),cell.getQualifierLength());
String columnValue=new String(valueArray,cell.getValueOffset(),cell.getValueLength());
if(specifiedColumnName.equals(columnName)){
System.out.println(columnValue);
}
}
}catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
}
到了这里,关于java: 从HBase中读取数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!