Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

javaAPI连接hbase

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

javaAPI连接hbase

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

import java.io.

IOException;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import java.util.NavigableMap;

import java.util.Random;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.Cell;

import org.apache.hadoop.hbase.CellScanner;

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.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.Get;
import

org.apache.hadoop.hbase.client.HBaseAdmin;

import org.apache.hadoop.hbase.client.Put;

import org.apache.hadoop.hbase.client.Result;

import org.apache.hadoop.hbase.client.Table;

import org.apache.hadoop.hbase.util.Bytes;

/**

* hbase 操作 创建表

* 1.通过 HBaseConfiguration.create() :获取配置 conf

* 2.conf.set() :设置 zk 等参数(kerberos 认证等)

3.ConnectionFactory.createConnection(configuratio

n) :获取连接 conn

* 4. 通 过 conn.getAdmin() 来 获 取 Admin :表相关操作的类

(HBaseAdmin 已过期)

* 5. 创 建 TableName : 描 述 表 名 称 的 : TableName tname =

TableName.valueOf(tablename);

* 6. 创 建表 描述 信 息类 : HTableDescriptor tDescriptor = new

HTableDescriptor(tname);

* 7. 添 加 表 列 簇 描 述 信 息 类 : HColumnDescriptor famliy = new

HColumnDescriptor(cf);
* 8. 将 表 列 簇 描 述 信 息 类 添 加 到 表 描 述 信 息 类 :

tDescriptor.addFamily(famliy);

* 9.调用 admin 创建表:admin.createTable(tDescriptor);

* hbase 操作 添加数据

* 1.通过 HBaseConfiguration.create() :获取配置 conf

* 2.conf.set() :设置 zk 等参数(kerberos 认证等)

3.ConnectionFactory.createConnection(configuratio

n) :获取连接 conn

* 4. 创 建 TableName : 描 述 表 名 称 的 : TableName tname =

TableName.valueOf(tablename);

* 5. 通 过 conn 连 接 获 得 表 对 象 : Table table =

connection.getTable(tableName);

* 6.1.单挑插入 table.put(Put)

* 6.2. 批 量 插 入 数 据 , 先 用 list 封 装 put 对 象 : List<Put> batPut =

new ArrayList<Put>();

* Put put = new

Put(Bytes.toBytes("rowkey_"+i)); //插入的 rowkey

* put.addColumn(Bytes.toBytes("i"),

Bytes.toBytes("username"),

Bytes.toBytes("un_"+i)); //列簇,列,值
* batPut.add(put)

* table.put(batPut)

* hbase 操作 获取数据

* 1.通过 HBaseConfiguration.create() :获取配置 conf

* 2.conf.set() :设置 zk 等参数(kerberos 认证等)

3.ConnectionFactory.createConnection(configuratio

n) :获取连接 conn

* 4. 创 建 TableName : 描 述 表 名 称 的 : TableName tname =

TableName.valueOf(tablename);

* 5. 通 过 conn 连 接 获 得 表 对 象 : Table table =

connection.getTable(tableName);

* 6.List<Get> gets = new ArrayList<Get>(); //批量封装请

求信息

* Get get = new

Get(Bytes.toBytes("rowkey_"+i)); //查询的 rowkey

* gets.add(get);

* 7.Result[] results = table.get(gets); //通过 Result[]接

收数据

* 8. 使 用 CellScanner cellScanner =
result.cellScanner(); 获取 cell

* while(cellScanner.advance()){

Cell cell = cellScanner.current();

//从单元格 cell 中把数据获取并输出

//使用 CellUtil 工具类,从 cell 中把数据获取出来

String famliy =

Bytes.toString(CellUtil.cloneFamily(cell));

String qualify =

Bytes.toString(CellUtil.cloneQualifier(cell));

String rowkey =

Bytes.toString(CellUtil.cloneRow(cell));

String value =

Bytes.toString(CellUtil.cloneValue(cell));

System.out.println("rowkey:"+rowkey+",c

olumnfamily:"+famliy+",qualify:"+qualify+",value:"

+value);

* @author jiangtao

*/

public class HbaseTest {

public Connection connection;


// 用 hbaseconfiguration 初 始 化 配 置 信 息 时 会 自 动 加 载 当 前 应 用

classpath 下的 hbase-site.xml

public static Configuration configuration =

HBaseConfiguration.create();

public Table table;

public Admin admin;

public HBaseAdmin ad;

public HbaseTest() throws Exception{

//ad = new HBaseAdmin(configuration); //过期了,推

荐使用 Admin

configuration.set("hbase.zookeeper.quorum","

bd36,bd37,bd38,bd66,bd67");

configuration.set("hbase.zookeeper.property.cl

ientPort","2181");

configuration.set("zookeeper.znode.parent",

"/hbase-unsecure");

//对 connection 初始化

connection =

ConnectionFactory.createConnection(configuration);

admin = connection.getAdmin();

}
//创建表

public void createTable(String

tablename,String... cf1) throws Exception{

//获取 admin 对象

Admin admin = connection.getAdmin();

//创建 tablename 对象描述表的名称信息

TableName tname =

TableName.valueOf(tablename);//bd17:mytable

//创建 HTableDescriptor 对象,描述表信息

HTableDescriptor tDescriptor = new

HTableDescriptor(tname);

//判断是否表已存在

if(admin.tableExists(tname)){

System.out.println("表"+tablename+"已存在");

return;

//添加表列簇信息

for(String cf:cf1){

HColumnDescriptor famliy = new

HColumnDescriptor(cf);

tDescriptor.addFamily(famliy);

}
//调用 admin 的 createtable 方法创建表

admin.createTable(tDescriptor);

System.out.println("表"+tablename+"创建成功");

//删除表

public void deleteTable(String tablename) throws

Exception{

Admin admin = connection.getAdmin();

TableName tName =

TableName.valueOf(tablename);

if(admin.tableExists(tName)){

admin.disableTable(tName);

admin.deleteTable(tName);

System.out.println("删除表"+tablename+"成功!");

}else{

System.out.println("表"+tablename+"不存在。");

//新增数据到表里面 Put

public void putData(String table_name) throws

Exception{

TableName tableName =
TableName.valueOf(table_name);

Table table = connection.getTable(tableName);

Random random = new Random();

List<Put> batPut = new ArrayList<Put>();

for(int i=0;i<10;i++){

//构建 put 的参数是 rowkey rowkey_i (Bytes 工具类,各种

java 基础数据类型和字节数组之间的相互转换)

Put put = new

Put(Bytes.toBytes("rowkey_"+i));

put.addColumn(Bytes.toBytes("i"),

Bytes.toBytes("username"),

Bytes.toBytes("un_"+i));

put.addColumn(Bytes.toBytes("i"),

Bytes.toBytes("age"),

Bytes.toBytes(random.nextInt(50)+1));

put.addColumn(Bytes.toBytes("i"),

Bytes.toBytes("birthday"),

Bytes.toBytes("20170"+i+"01"));

put.addColumn(Bytes.toBytes("j"),

Bytes.toBytes("phone"), Bytes.toBytes("电话_"+i));

put.addColumn(Bytes.toBytes("j"),

Bytes.toBytes("email"), Bytes.toBytes("email_"+i));
//单记录 put

// table.put(put);

batPut.add(put);

table.put(batPut);

System.out.println("表插入数据成功!");

public void getData(String table_Name) throws

Exception{

TableName tableName =

TableName.valueOf(table_Name);

table = connection.getTable(tableName);

//构建 get 对象

List<Get> gets = new ArrayList<Get>();

for(int i=0;i<5;i++){

Get get = new

Get(Bytes.toBytes("rowkey_"+i));

gets.add(get);

Result[] results = table.get(gets);

for(Result result:results){

//一行一行读取数据
//

NavigableMap<byte[],NavigableMap<byte[],Naviga

bleMap<Long,byte[]>>> maps = result.getMap();

// for(byte[] cf:maps.keySet()){

//

NavigableMap<byte[],NavigableMap<Long,byte[]>

> valueWithColumnQualify = maps.get(cf);

// for(byte[]

columnQualify:valueWithColumnQualify.keySet()){

// NavigableMap<Long,byte[]>

valueWithTimeStamp =

valueWithColumnQualify.get(columnQualify);

// for(Long

ts:valueWithTimeStamp.keySet()){

// byte[] value =

valueWithTimeStamp.get(ts);

//

System.out.println("rowkey:"+Bytes.toString(result

.getRow())+",columnFamliy:"+

// Bytes.toString(cf)

+",comlumnQualify:"+Bytes.toString(columnQualify

)+",timestamp:"
// +new Date(ts)

+",value:"+Bytes.toString(value)

// );

// }

// }

// }

//使用字段名称和列簇名称来获取 value 值

//

System.out.println("rowkey:"+Bytes.toString(result

.getRow())

+",columnfamily:i,columnqualify:username,value:"+

//

Bytes.toString(result.getValue(Bytes.toBytes("i"),

Bytes.toBytes("username")))

// );

//

System.out.println("rowkey:"+Bytes.toString(result

.getRow())

+",columnfamily:i,columnqualify:age,value:"+

//

Bytes.toInt(result.getValue(Bytes.toBytes("i"),
Bytes.toBytes("age")))

// );

//使用 cell 获取 result 里面的数据

CellScanner cellScanner =

result.cellScanner();

while(cellScanner.advance()){

Cell cell = cellScanner.current();

//从单元格 cell 中把数据获取并输出

//使用 CellUtil 工具类,从 cell 中把数据获取出来

String famliy =

Bytes.toString(CellUtil.cloneFamily(cell));

String qualify =

Bytes.toString(CellUtil.cloneQualifier(cell));

String rowkey =

Bytes.toString(CellUtil.cloneRow(cell));

String value =

Bytes.toString(CellUtil.cloneValue(cell));

System.out.println("rowkey:"+rowkey+",c

olumnfamily:"+famliy+",qualify:"+qualify+",value:"

+value);

}
}

//关闭连接

public void cleanUp() throws Exception{

connection.close();

public static void main(String[] args) throws

Exception {

HbaseTest hbaseTest = new HbaseTest();

hbaseTest.createTable("jiangtao:test", "i","j");

hbaseTest.putData("jiangtao:test");

hbaseTest.getData("jiangtao:test");

hbaseTest.cleanUp();

You might also like