设为首页 加入收藏

TOP

java对hbase的增删改查操作
2019-02-12 13:36:49 】 浏览:42
Tags:java hbase 删改 操作
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/k778899jx/article/details/79132035

直接上代码吧,conf里面的是配置,看个人而异,默认zookeeper.znode.parent=/hbase,这个不对的话会报空指针

package hadoop.vzan.util;

import java.io.IOException;
import java.util.Map;

import org.apache.commons.io.IOUtils;
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.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.C
		    

onnectionFactory; 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.util.Bytes; import org.apache.log4j.Logger; /** * 编辑hbase的CRUD操作,搞成工具类 * @author weihang * 2018年1月5日 */ public class HbaseUtil { private static final Logger logger = Logger.getLogger(HbaseUtil.class); // 声明静态配置 static Configuration conf = null; static Connection conn = null; static Admin admin = null; static Table table = null; /** * 创建连接 */ public static void init() { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.property.clientPort", "2181"); conf.set("hbase.zookeeper.quorum", "h1.vzan.cc,h3.vzan.cc,h2.vzan.cc"); conf.set("hbase.rootdir", "hdfs://h1.vzan.cc:8020/apps/hbase/data"); conf.set("zookeeper.znode.parent", "/hbase-unsecure"); try { conn = ConnectionFactory.createConnection(conf); } catch (IOException e) { e.printStackTrace(); } } /** * 创建表 * @param tableName 表名 * @param columns 列,可以多列以逗号分割 * @throws IOException */ public static void createTable(String tableName,String columns) throws IOException { // conn = ConnectionFactory.createConnection(conf); // Admin admin = null; admin = conn.getAdmin(); TableName table = TableName.valueOf(tableName); if (admin.tableExists(table)) { logger.info("表已存在,删除表"); admin.disableTable(table); admin.deleteTable(table); } HTableDescriptor hTableDescriptor = new HTableDescriptor(table); String[] column = columns.split(","); for (int i = 0; i < column.length; i++) { HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(column[i]); hTableDescriptor.addFamily(hColumnDescriptor); } admin.createTable(hTableDescriptor); IOUtils.closeQuietly(admin); } /** * 删除表 * @param tableName * @throws IOException */ public static void deleteTable(String tableName) throws IOException { admin = conn.getAdmin(); TableName table = TableName.valueOf(tableName); admin.disableTable(table); admin.deleteTable(table); IOUtils.closeQuietly(admin); } /** * 添加一列数据 * @param tableName 表名 * @param rowkey 主键 * @param column 列名 * @param columnFamily 簇 * @param value 值 * @throws IOException */ public static void add(String tableName,String rowkey,String column,String columnFamily,String value) throws IOException { try { table = conn.getTable(TableName.valueOf(tableName)); Put put = new Put(Bytes.toBytes(rowkey)); put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value)); table.put(put); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(table); // table.close(); } } /** * 批量新增,其实就是重复put.addColumn * @param tableName * @param rowkey * @param columns */ public static void bitchAdd(String tableName,String rowkey,Map<String,Object> columns) { try { table = conn.getTable(TableName.valueOf(tableName)); Put put = new Put(Bytes.toBytes(rowkey)); // put.addc } catch (IOException e) { e.printStackTrace(); } } /** * 删除columnFamily * @param tableName * @param columnFamily */ public static void deleteColumnFamily(String tableName,String columnFamily) { try { admin = conn.getAdmin(); TableName table = TableName.valueOf(tableName); admin.deleteColumn(table, Bytes.toBytes(columnFamily)); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(admin); } } /** * 删除row 换成string[] 即可删除多行 * @param tableName * @param rowkey * @throws IOException */ public static void deleteRow(String tableName,String rowkey) throws IOException { admin = conn.getAdmin(); TableName tablename = TableName.valueOf(tableName); table = conn.getTable(TableName.valueOf(tableName)); if (admin.tableExists(tablename)) { try { Delete delete = new Delete(rowkey.getBytes()); table.delete(delete);; } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(table); IOUtils.closeQuietly(admin); } } } /** * 删除qualifierName columnFamily:qualifierName 列的属性解释:column=columnFamily:qualifierName 例如:column=course:math * @param tableName * @param rowkey * @param columnFamily * @param qualifierName * @throws IOException */ public static void deletequalifierName(String tableName,String rowkey,String columnFamily,String qualifierName) throws IOException { admin = conn.getAdmin(); table = conn.getTable(TableName.valueOf(tableName)); if (admin.tableExists(TableName.valueOf(tableName))) { try { Delete delete = new Delete(rowkey.getBytes()); delete.addColumns(columnFamily.getBytes(),qualifierName.getBytes()); table.delete(delete); } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(table); IOUtils.closeQuietly(admin); } } } /** * 查询所有数据 * @param tableName * @throws Exception */ public static void scan(String tableName) throws Exception { table = conn.getTable(TableName.valueOf(tableName)); Scan s = new Scan(); ResultScanner rs = table.getScanner(s); for (Result r : rs) { System.out.println("Scan: " + r); } IOUtils.closeQuietly(table); } /** * 查询某个rowkey * @param tableName * @param rowkey */ public static void getData(String tableName,String rowkey) { try { table = conn.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowkey)); Result result = table.get(get); System.out.println("Get: " + result); KeyValue[] ky = result.raw(); for(KeyValue kv : ky){ System.out.println(" column family : " + Bytes.toString(kv.getFamily())); System.out.println(" column : " + Bytes.toString(kv.getQualifier())); System.out.println(" value : " + Bytes.toString(kv.getValue())); System.out.println(" timestamp : " + String.valueOf(kv.getTimestamp())); System.out.println("--------------------"); } } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(table); } } }

好像没有修改列这个api,,没找到


编程开发网
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Hbase数据库操作工具类 下一篇HBase的核心模块介绍

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(217) }