zoukankan      html  css  js  c++  java
  • HBASE基础(5):语法(3) API (2) DML

    创建类HBase_DML

    1 插入数据

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.Cell;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;
    
    import java.io.IOException;
    
    public class HBase_DML {
    
        //TODO 插入数据
        public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {
    
            //1.获取配置信息并设置连接参数
            Configuration configuration = HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
    
            //2.获取连接
            Connection connection = ConnectionFactory.createConnection(configuration);
    
            //3.获取表的连接
            Table table = connection.getTable(TableName.valueOf(tableName));
    
            //4.创建Put对象
            Put put = new Put(Bytes.toBytes(rowKey));
    
            //5.放入数据
            put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
    
            //6.执行插入数据操作
            table.put(put);
    
            //7.关闭连接
            table.close();
            connection.close();
        }
    
    }

    2 单条数据查询

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.Cell;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;
    
    import java.io.IOException;
    
    public class HBase_DML {
    
        //TODO 单条数据查询(GET)
        public static void getDate(String tableName, String rowKey, String cf, String cn) throws IOException {
    
            //1.获取配置信息并设置连接参数
            Configuration configuration = HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
    
            //2.获取连接
            Connection connection = ConnectionFactory.createConnection(configuration);
    
            //3.获取表的连接
            Table table = connection.getTable(TableName.valueOf(tableName));
    
            //4.创建Get对象
            Get get = new Get(Bytes.toBytes(rowKey));
            // 指定列族查询
            // get.addFamily(Bytes.toBytes(cf));
            // 指定列族:列查询
            // get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
    
            //5.查询数据
            Result result = table.get(get);
    
            //6.解析result
            for (Cell cell : result.rawCells()) {
                System.out.println("ROW:" + Bytes.toString(CellUtil.cloneRow(cell)) +
                            " CF:" + Bytes.toString(CellUtil.cloneFamily(cell))+
                            " CL:" + Bytes.toString(CellUtil.cloneQualifier(cell))+
                            " VALUE:" + Bytes.toString(CellUtil.cloneValue(cell)));
            }
    
            //7.关闭连接
            table.close();
            connection.close();
    
        }
    
    }

    3 扫描数据

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.Cell;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;
    
    import java.io.IOException;
    
    public class HBase_DML {
    
        //TODO 扫描数据(Scan)
        public static void scanTable(String tableName) throws IOException {
    
            //1.获取配置信息并设置连接参数
            Configuration configuration = HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
    
            //2.获取连接
            Connection connection = ConnectionFactory.createConnection(configuration);
    
            //3.获取表的连接
            Table table = connection.getTable(TableName.valueOf(tableName));
    
            //4.创建Scan对象
            Scan scan = new Scan();
    
            //5.扫描数据
            ResultScanner results = table.getScanner(scan);
    
            //6.解析results
            for (Result result : results) {
                for (Cell cell : result.rawCells()) {
          System.out.println(
                            Bytes.toString(CellUtil.cloneRow(cell))+":"+
                                    Bytes.toString(CellUtil.cloneFamily(cell))+":" +
                                    Bytes.toString(CellUtil.cloneQualifier(cell)) +":" +
                                    Bytes.toString(CellUtil.cloneValue(cell))
                    );
                }
            }
    
            //7.关闭资源
            table.close();
            connection.close();
    
        }
    
    }

    4 删除数据

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.Cell;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;
    
    import java.io.IOException;
    
    public class HBase_DML {
    
        //TODO 删除数据
        public static void deletaData(String tableName, String rowKey, String cf, String cn) throws IOException {
    
            //1.获取配置信息并设置连接参数
            Configuration configuration = HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
    
            //2.获取连接
            Connection connection = ConnectionFactory.createConnection(configuration);
    
            //3.获取表的连接
            Table table = connection.getTable(TableName.valueOf(tableName));
    
            //4.创建Delete对象
            Delete delete = new Delete(Bytes.toBytes(rowKey));
    
            // 指定列族删除数据
            // delete.addFamily(Bytes.toBytes(cf));
            // 指定列族:列删除数据(所有版本)
            // delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
            // 指定列族:列删除数据(指定版本)
            // delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));
    
            //5.执行删除数据操作
            table.delete(delete);
    
            //6.关闭资源
            table.close();
            connection.close();
    
    }
    
    }

    本文来自博客园,作者:秋华,转载请注明原文链接:https://www.cnblogs.com/qiu-hua/p/15225444.html

  • 相关阅读:
    21世纪网络创业新生代中国海归的传承与开创圆桌论坛实录_网络营销_网赚猫 及时更新网络赚钱_网赚项目_兼职_网络营销等相关网赚资讯
    知方可补不足~利用LogParser将IIS日志插入到数据库
    WebApi系列~自主宿主HttpSelfHost的实现
    我心中的核心组件(可插拔的AOP)~第十五回 我的日志组件Logger.Core(策略,模版方法,工厂,单例等模式的使用)
    爱上MVC系列~前端验证与后端数据有效性验证
    第九回 Microsoft.Practices.Unity.Interception实现基于数据集的缓存(针对六,七,八讲的具体概念和配置的解说)
    struts2第一个程序的详解(配图)
    JavaScript中的对象(一)
    SqlServer操作远程数据库
    [leetcode]Binary Tree Inorder Traversal
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/15225444.html
Copyright © 2011-2022 走看看