博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HBase1.0.0 实现数据增删查
阅读量:4308 次
发布时间:2019-06-06

本文共 8454 字,大约阅读时间需要 28 分钟。

HBase1.0.0 即Hadoop 2.6 采用maven 的方式实现HBase数据简单操作

import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Random;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;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.Connection;import org.apache.hadoop.hbase.client.ConnectionFactory;import org.apache.hadoop.hbase.client.HBaseAdmin;import org.apache.hadoop.hbase.client.HConnection;import org.apache.hadoop.hbase.client.HTableInterface;import org.apache.hadoop.hbase.client.HTablePool;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.client.Table;import org.apache.hadoop.hbase.util.Bytes;/** * @author 作者 E-mail: * @version 创建时间:2015年12月28日 下午11:31:31 类说明 */public class HBaseUtils {    /*     * static { HBaseDaoPool.getInstance(); }     */    private static Configuration conf;    private static HBaseAdmin admin;    private  static HTable hTable = null;    static {        conf = new Configuration();        String filePath = "hbase-site.xml";        Path path = new Path(filePath);        conf.addResource(path);        conf = HBaseConfiguration.create(conf);    }    /**     * 创建表并判断表是否存在,如果存在则退出     * @param name     * @param cf     * @throws Exception     */    @SuppressWarnings("deprecation")    public static void create_table(String name, String cf) throws Exception {        admin = new HBaseAdmin(conf);        // 先检查表是否存在        if (admin.tableExists(name)) {            System.out.println("table is exit" + name);            System.exit(0);        }        HTableDescriptor tableDesc = new HTableDescriptor(name);        HColumnDescriptor hd = new HColumnDescriptor(cf);        /* hd.setMaxVersions(version); */        tableDesc.addFamily(hd);        admin.createTable(tableDesc);        admin.close();    }    /**     * 获得HBase里面所有Table     * @return     * @throws Exception     * @throws ZooKeeperConnectionException     * @throws MasterNotRunningException     */    @SuppressWarnings({ "unused", "deprecation" })    private static List
getAllTable() throws MasterNotRunningException, ZooKeeperConnectionException, Exception { List
table = null; admin = new HBaseAdmin(conf); try { HTableDescriptor[] listTables = admin.listTables(); if (listTables.length > 0) { table = new ArrayList
(); for (HTableDescriptor tableDes : listTables) { table.add(tableDes.getNameAsString()); System.out.println("table list:" + tableDes.getNameAsString()); } } } catch (Exception e) { e.printStackTrace(); } return table; } /** * 添加一条记录 * @param tableName * @param rowKey * @param cloumnFianly * @param column * @param values * @return */ @SuppressWarnings({ "unused", "deprecation", "resource" }) private static boolean addOneRows(String tableName, String rowKey, String cloumnFianly, String column, byte[] values) { HTablePool hTablePool = new HTablePool(conf, 1000); HTableInterface table = hTablePool.getTable(tableName); Put put = new Put(rowKey.getBytes()); put.add(cloumnFianly.getBytes(), column.getBytes(), values); try { table.put(put); System.out.println("add success:" + rowKey + "....end"); return true; } catch (IOException e) { e.printStackTrace(); System.out.println("add false :" + rowKey + " error ...end"); return false; } } /** * 根据表名插入一条数据,rowkey 做了简单处理,前面加了7位随机数 * @param hConnection * @param tableName */ @SuppressWarnings("deprecation") public static void insertData(String tableName) { try { TableName table = TableName.valueOf(tableName); System.out.println(table + "table"); Connection connection = ConnectionFactory.createConnection(conf); Table tb1 = connection.getTable(table); Random random = new Random(); int sum = random.nextInt(9999999); String.format("%07d", sum); String rowKkey = String.format("%07d", sum); Put put = new Put(Bytes.toBytes(rowKkey)); put.add(Bytes.toBytes("cf1"), Bytes.toBytes("address"), Bytes.toBytes("zz")); tb1.put(put); tb1.close(); System.out.println("insert end...."); } catch (IOException e) { e.printStackTrace(); } }
/**     * 根据rowKey 获得一条记录     * @param tableName     * @param rowKey     */    public void getOneRowsByKey(String tableName, String rowKey){      try {        hTable = new HTable(conf, tableName);        Get get = new Get(Bytes.toBytes(rowKey));        Result result = hTable.get( get );        KeyValue[] rows = result.raw();        for (int i = 0; i < rows.length; i++){            String rowname = new String(rows[i].getQualifier());            String rowValue = new String(rows[i].getValue());            System.out.println("rowname:" + rowname + "--value--" +rowValue);                    }    }    catch ( IOException e ) {              e.printStackTrace();    }    }
 

 

public static void main(String[] args) throws Exception {        // addOneRows("test", "row7", "cf", "g", "value7".getBytes());        // String table = "ps";        String table = "a1";        String cf = "cf";        // create_table(table, cf);        // getAllTable();        insertData(table);        System.out.println("success");    }}

maven 配置:

org.apache.hadoop
hadoop-common
2.6.0
org.apache.hbase
hbase-client
1.0.0
org.apache.hbase
hbase-server
1.0.0
hbase-site.xml 配置
hbase.rootdir
hdfs://HMaster/hbase
hbase.client.write.buffer
2097152
hbase.client.pause
100
hbase.client.retries.number
35
hbase.client.scanner.caching
100
hbase.client.keyvalue.maxsize
10485760
hbase.regionserver.thrift.http
false
hbase.thrift.support.proxyuser
false
hbase.rpc.timeout
60000
hbase.snapshot.enabled
true
hbase.snapshot.master.timeoutMillis
60000
hbase.snapshot.region.timeout
60000
hbase.snapshot.master.timeout.millis
60000
hbase.security.authentication
simple
zookeeper.session.timeout
60000
zookeeper.znode.parent
/hbase
zookeeper.znode.rootserver
root-region-server
hbase.zookeeper.quorum
node5,node2,node3,node4,node1
hbase.zookeeper.property.clientPort
2181

 

转载于:https://www.cnblogs.com/zhanggl/p/5091757.html

你可能感兴趣的文章
Nginx配置文件nginx.conf中文详解(总结)
查看>>
Mysql出现Table 'performance_schema.session_status' doesn't exist
查看>>
MySQL innert join、left join、right join等理解
查看>>
vivado模块封装ip/edf
查看>>
sdc时序约束
查看>>
Xilinx Jtag Access/svf文件/BSCANE2
查看>>
NoC片上网络
查看>>
开源SoC整理
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
已知子网掩码,确定ip地址范围
查看>>
判断时间或者数字是否连续
查看>>
docker-daemon.json各配置详解
查看>>
Docker(一)使用阿里云容器镜像服务
查看>>
Docker(三) 构建镜像
查看>>
FFmpeg 是如何实现多态的?
查看>>
FFmpeg 源码分析 - avcodec_send_packet 和 avcodec_receive_frame
查看>>
FFmpeg 新旧版本编码 API 的区别
查看>>
RecyclerView 源码深入解析——绘制流程、缓存机制、动画等
查看>>
Android 面试题整理总结(一)Java 基础
查看>>