hbase java client_hbase client(java)

package hbase;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;

import java.util.List;

import java.util.ListIterator;

import java.util.Map;

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

import org.apache.hadoop.hbase.ZooKeeperConnectionException;

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

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

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

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

public class HbaseConnection {

private String port;

private String ip;

private Configuration conf = null;

private Connection conn = null;

public HbaseConnection(String port, String ip) {

this.port = port;

this.ip = ip;

conf = HBaseConfiguration.create();

conf.set("hbase.zookeeper.quorum", ip);

conf.set("hbase.zookeeper.property.clientPort", port);

try {

conn = ConnectionFactory.createConnection(conf);

} catch (IOException e) {

e.printStackTrace();

}

}

/*

* 删除指定的列

*

* @tableName 表名

*

* @rowKey rowKey

*

* @familyName 列族名

*

* @columnName 列名

*/

public void deleteColumn(String tableName, String rowKey,

String falilyName, String columnName) throws IOException {

Table table = conn.getTable(TableName.valueOf(tableName));

Delete deleteColumn = new Delete(Bytes.toBytes(rowKey));

deleteColumn.deleteColumns(Bytes.toBytes(falilyName),

Bytes.toBytes(columnName));

table.delete(deleteColumn);

System.out.println(falilyName + ":" + columnName + "is deleted!");

}

/*

* 删除指定的列

*

* @tableName 表名

*

* @rowKey rowKey

*/

public void deleteAllColumn(String tableName, String rowKey)

throws IOException {

Table table = conn.getTable(TableName.valueOf(tableName));

Delete deleteAll = new Delete(Bytes.toBytes(rowKey));

table.delete(deleteAll);

System.out.println("all columns are deleted!");

}

/*

* 更新表中的某一列

*

* @tableName 表名

*

* @rowKey rowKey

*

* @familyName 列族名

*

* @columnName 列名

*

* @value 更新后的值

*/

public void updateTable(String tableName, String rowKey,

String familyName, String columnName, String value)

throws IOException {

Table table = conn.getTable(TableName.valueOf(tableName));

Put put = new Put(Bytes.toBytes(rowKey));

put.add(Bytes.toBytes(familyName), Bytes.toBytes(columnName),

Bytes.toBytes(value));

table.put(put);

System.out.println("update table Success!");

}

/*

* 删除表

*

* @tableName 表名

*/

public void deleteTable(String tableName) throws IOException {

HBaseAdmin admin = new HBaseAdmin(conf);

admin.disableTable(tableName);

admin.deleteTable(tableName);

System.out.println(tableName + "is deleted!");

}

//查询某个表的数据

public void getResultScann(String tableName) throws IOException {

Scan scan = new Scan();

ResultScanner rs = null;

Table table = conn.getTable(TableName.valueOf(tableName));

try {

rs = table.getScanner(scan);

for (Result r : rs) {

for (KeyValue kv : r.list()) {

System.out.println("row:" + Bytes.toString(kv.getRow()));

System.out.println("family:"

+ Bytes.toString(kv.getFamily()));

System.out.println("qualifier:"

+ Bytes.toString(kv.getQualifier()));

System.out

.println("value:" + Bytes.toString(kv.getValue()));

System.out.println("timestamp:" + kv.getTimestamp());

System.out

.println("-------------------------------------------");

}

}

} finally {

rs.close();

}

}

//创建表

public void createTable(String tableName, List cols)

throws MasterNotRunningException, ZooKeeperConnectionException,

IOException {

HBaseAdmin admin = new HBaseAdmin(conf);

if (admin.tableExists(tableName)) {

System.out.print("该表已经存在");

}

HTableDescriptor desc = new HTableDescriptor(tableName);

for (String col : cols) {

desc.addFamily(new HColumnDescriptor(col));

}

admin.createTable(desc);

}

public static void main(String[] args) throws IOException {

HbaseConnection hbc = new HbaseConnection("2181", "192.168.73.101");

// 创建表

// List list = new ArrayList();

// list.add("infosqwe:");

// list.add("other:");

// try {

// hbc.createTable("student1", list);

// } catch (IOException e) {

// e.printStackTrace();

// }

// String[] array={"a","b"} ;

// 插入数据

// String[] array = { "nicholas", "lee" };

// hbc.insert("zjj", "student",array, array);

// 查询某一条数据

// hbc.getKey("student", "zjj");

// 查询整个表的记录

hbc.getResultScann("student");

}

// 取得其中一调数据

public Result getKey(String tableName, String key) {

try {

Table table = conn.getTable(TableName.valueOf(tableName));

Result result = table.get(new Get(Bytes.toBytes(key)));

for (KeyValue kv : result.list()) {

System.out.println("family:" + Bytes.toString(kv.getFamily()));

System.out.println("qualifier:"

+ Bytes.toString(kv.getQualifier()));

System.out.println("value:" + Bytes.toString(kv.getValue()));

System.out.println("Timestamp:" + kv.getTimestamp());

System.out

.println("-------------------------------------------");

}

return result;

// byte[] bytes= res.getValue(Bytes.toBytes("infos"),

// Bytes.toBytes("lee"));

// return bytes.toString();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

// 插入数据,到时候可用结合实际需求

public void insert(String rowKey, String tableName, String[] array,

String[] array1) throws IOException {

Put put = new Put(Bytes.toBytes(rowKey));

Table table = conn.getTable(TableName.valueOf(tableName));

// 没有迭代列主

for (int i = 0; i < array.length; i++) {

put.add(Bytes.toBytes("infos"), Bytes.toBytes(array[i]),

Bytes.toBytes(array1[i]));

}

table.put(put);

}

// public static void addData(String rowKey, String tableName,

// String[] column1, String[] value1, String[] column2, String[] value2)

// throws IOException {

// Put put = new Put(Bytes.toBytes(rowKey));// 设置rowkey

// Table table = conn.getTable(TableName.valueOf(tableName));

// HColumnDescriptor[] columnFamilies = table.getTableDescriptor() //

// 获取所有的列族

// .getColumnFamilies();

//

// for (int i = 0; i < columnFamilies.length; i++) {

// String familyName = columnFamilies[i].getNameAsString(); // 获取列族名

// if (familyName.equals("article")) { // article列族put数据

// for (int j = 0; j < column1.length; j++) {

// put.add(Bytes.toBytes(familyName),

// Bytes.toBytes(column1[j]), Bytes.toBytes(value1[j]));

// }

// }

// if (familyName.equals("author")) { // author列族put数据

// for (int j = 0; j < column2.length; j++) {

// put.add(Bytes.toBytes(familyName),

// Bytes.toBytes(column2[j]), Bytes.toBytes(value2[j]));

// }

// }

// }

// table.put(put);

// System.out.println("add data Success!");

// }

//用于批量

public void Save(String tableName, List puts) {

try {

Table table = conn.getTable(TableName.valueOf(tableName));

table.put(puts);

} catch (IOException e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值