zoukankan      html  css  js  c++  java
  • IDEA Zookeeper 配置 和HBase 配置 (附JavaAPI)

    j将配置填入idea 的配置文件当中 等待idea 直接下载安装即可

    zookeeper move 配置  :版本不同 里面的zookeeper的版本要根据自己的版本来更改

        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-slf4j-impl</artifactId>
                <version>2.12.0</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.5.8</version>
            </dependency>
        </dependencies>

    Hbase 配置 

     <dependencies>
    
            <!--        hbase依赖-->
    
            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-server</artifactId>
                <version>2.2.6</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-client</artifactId>
                <version>2.2.6</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-auth</artifactId>
                <version>3.1.3</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-common</artifactId>
                <version>2.2.6</version>
            </dependency>
    
    
    
        </dependencies>

    IDEA 对HBASE进行的增删改查操作:

    1.个人笔记所写:

    package com.hadoop.hbase;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.*;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
    import org.apache.hadoop.hbase.util.Bytes;
    import org.checkerframework.checker.units.qual.C;
    import org.omg.CORBA.StringHolder;
    
    
    import java.io.IOException;
    
    /**
     * DDL:
     *1.判断表是否存在
     * 2.创建命名空间
     * 3.创建命名空间
     * 4.删除表
     *
     * DML:
     * 5.插入数据
     * 6.查数据(get)
     * 7.查数据 (scan)
     * 8.删除数据
     */
    
    public class HbaseAPI {
    
        private static Connection connection =null;
        private  static  Admin admin=null;
        static
        {
            try {
                // 获取赋值信息
                Configuration configuration=HBaseConfiguration.create();
                configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
                //创建连接对象
                connection=ConnectionFactory.createConnection(configuration);
    
                //创建Admin 对象
                admin =connection.getAdmin();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
        }
    
        // 1 .判断表是否存在
        public static boolean isTableExist(String tablename) throws IOException {
    
            //获取配置信息
            ///HBaseConfiguration configuration =new HBaseConfiguration();
            Configuration configuration= HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
            configuration.set("hbase.zookeeper.property.clientPort", "2181");
            //获取管理员对象
            Connection connection = ConnectionFactory.createConnection(configuration);
            Admin admin = connection.getAdmin();
            //HBaseAdmin admin = new HBaseAdmin(configuration);
    
            //判断表是否存在
            boolean  exists = admin.tableExists(TableName.valueOf(tablename));
            //返回结果
            return  exists;
        }
    
        // 2. 创建表                                           可变形参
        public static void creatTable(String tablename,String... cfs) throws IOException {
    
            //判断是否存在列族信息
            if(cfs.length <=0){
                System.out.println("请设置列族信息!");
                return;
            }
            //表存在
            if (isTableExist(tablename)){
                System.out.println(tablename+"已经存在!");
                return;
            }
            //创建表描述器
            HTableDescriptor hTableDescriptor= new HTableDescriptor(TableName.valueOf(tablename));
            //循环添加列族信息
            for (String cf:cfs){
                //创建列族信息
                HColumnDescriptor hColumnDescriptor= new HColumnDescriptor(cf);
                //添加具体列族信息
                hTableDescriptor.addFamily(hColumnDescriptor);
            }
            //创建表
            admin.createTable(hTableDescriptor);
        }
    
    
    
        // 3. 删除表
        public static void dropTable(String tableName) throws IOException {
    
            //表是否存在
            if(!isTableExist(tableName)){
                System.out.println(tableName+"表不存在!");
                return;
            }
            //让表下线
            admin.disableTable(TableName.valueOf(tableName));
            //删除表
            admin.deleteTable(TableName.valueOf(tableName));
            System.out.println("删除表成功。");
        }
    
        // 4. 创建命名空间
        public static void createNameSpace(String ns){
    
            //创建命名空间描述器
            NamespaceDescriptor namespaceDescriptor =NamespaceDescriptor.create(ns).build();
    
            //创建命名空间
            try {
                admin.createNamespace(namespaceDescriptor);
                System.out.println("命名空间创建成功。");
            }catch (NamespaceNotFoundException e){
                System.out.println(ns+"命名空间已存在!");
    
            }
            catch (IOException e) {
                e.printStackTrace();
            }
           // System.out.println("虽然存在,仍旧到这里了");
        }
    
        //5. 向表插入数据
        public static void  putData(String tableName,String rowKey,String cf,String cn,String value) throws IOException {
    
             // 获取表对象
            Table table =  connection.getTable(TableName.valueOf(tableName));
            // 创建put 对象
            Put put = new Put(Bytes.toBytes(rowKey));
            //给put 对象赋值
            put.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn),Bytes.toBytes(value));
            //批量添加
           // put.addColumn(Bytes.toBytes(cf),Bytes.toBytes("sex"),Bytes.toBytes("male"));
            //插入数据
            table.put(put);
            //关闭连接
            table.close();
        }
    
        //6.获取数据 (get)
        public static void getData(String tableName, String rowKey, String cf,String cn) throws IOException {
    
            //获取表对象
            Table table= connection.getTable(TableName.valueOf(tableName));
    
            //创建Get对象
            Get get = new Get(Bytes.toBytes(rowKey));
    
            //2.1 获取指定列族
            //get.addFamily(Bytes.toBytes(cf));
    
            //2.2 获取指定列和族
           // get.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn));
    
            //2.3 获取数据的版本数
          //  get.getMaxVersions();
            //获取数据
            Result result = table.get(get);
    
            //解析result 并打印
           for (Cell cell : result.rawCells()){
               //打印数据
               System.out.println("CF:"+Bytes.toString(CellUtil.cloneFamily(cell))+
                       ",CN:"+ Bytes.toString(CellUtil.cloneQualifier(cell))+
                       ",Value:"+Bytes.toString(CellUtil.cloneValue(cell)));
           }
    
           //关闭表连接
            table.close();
    
        }
    
        //7 获取数据 scan
        public static  void  scanTable(String tableName) throws IOException {
    
            //获取表对象
            Table table= connection.getTable(TableName.valueOf(tableName));
            //构建Scan 对象
           // Scan scan = new Scan();
            // 左闭右开
            Scan scan = new Scan(Bytes.toBytes("1001"),Bytes.toBytes("1002"));
            //扫描表
            ResultScanner scanner = table.getScanner(scan);
    
            //解析resultScanner
            for (Result result :scanner){
    
                //解析 result 并打印
                for(Cell cell : result.rawCells()){
                    //打印数据
                    System.out.println("rowkey:"+Bytes.toString(CellUtil.cloneRow(cell))+",CF:"+Bytes.toString(CellUtil.cloneFamily(cell))+
                            ",CN:"+ Bytes.toString(CellUtil.cloneQualifier(cell))+
                            ",Value:"+Bytes.toString(CellUtil.cloneValue(cell)));
    
                }
    
            }
    
            //关闭表连接
            table.close();
    
        }
    
        //8.删除数据
        public static void deleteData(String tableName,String rowKey,String cf,String cn) throws IOException {
    
            //获取表对象
            Table table = connection.getTable(TableName.valueOf(tableName));
    
            //构建删除对象
            Delete delete = new Delete(Bytes.toBytes(rowKey));
    
            //2.1设置删除的列
           // delete.addColumn();
           // delete.addColumns();
    
            //删除操作
            table.delete(delete);
    
            //关闭连接
            table.close();
        }
    
    
    
    
    
    
    
    
    
    
    
        //关闭连接
        public static void  close()  {
            if (admin!=null){
                try {
                    admin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            if (connection!=null){
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        public static void main(String[] args) throws IOException {
    
    
            //测试表是否存在
         // System.out.println(isTableExist("XuQiuDemand"));
    
    
            //创建表测试
        // creatTable("XuQiuDemand","infos");
    
            //System.out.println(isTableExist("XuQiuDemand"));
    
            //删除表操作
        //    dropTable("stu5");
    
    
            //创建命名空间测试
         //   createNameSpace("cuixingyu");
    
            //创建数据测试
            String sjz="test";
           putData("stu","1500","info2","names",sjz);
    
            //获取单行数据
         //  getData("stu","1001","info2","phone");
    
       //     System.out.println(isTableExist("stu5"));
    
            //scanTable("stu");
    
            //测试删除
          //  deleteData("stu","1005","ss","ss");‘、+
            close();
        }
    
    }

    2.大佬同学所写

    package com.hadoop.hbase;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.Cell;
    import org.apache.hadoop.hbase.CellUtil;
    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 HbaseUtil {
        private static ThreadLocal<Connection> connHolder= new ThreadLocal<Connection>();
    
    
        /**
         * 创建connection
         * @throws IOException
         */
        public static void makeHbaseConnection() throws  IOException {
            Connection connection = connHolder.get();
            if (connection == null){
                Configuration conf = HBaseConfiguration.create();
                conf.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
                conf.set("hbase.zookeeper.property.clientPort", "2181");
                connection = ConnectionFactory.createConnection(conf);
                connHolder.set(connection);
            }
        }
    
        /**
         * 关闭连接
         * @throws IOException
         */
        public static void closeHbseConn() throws IOException {
            Connection connection = connHolder.get();
            if (connection != null){
                connection.close();
                connHolder.remove();
            }
        }
    
        /**
         * 添加一行数据
         * @param tableName 表名
         * @param rowKey 行号
         * @param family 列族
         * @param column 列名
         * @param value
         * @throws IOException
         */
        public static void insertData(String tableName,String rowKey,String family,String column,String value) throws IOException {
            //获取连接
            Connection connection = connHolder.get();
            //获取表对象
            Table table = connection.getTable(TableName.valueOf(tableName));
            //获取添加对象
            Put put = new Put(Bytes.toBytes(rowKey));
            //添加一列
            put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value));
            //添加
            table.put(put);
            //关闭
            table.close();
    
        }
    
        /**
         * 创建表
         * @param tableName
         * @param family
         * @throws IOException
         */
        public static void createTable(String tableName,String family) throws IOException {
            Connection connection = connHolder.get();
            //获取admin
            Admin admin = connection.getAdmin();
    
            //列族描述对象建造者
            ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(family));
            //设置最大版本号
            columnFamilyDescriptorBuilder.setMaxVersions(3);
            //列族描述对象
            ColumnFamilyDescriptor columnFamilyDescriptor = columnFamilyDescriptorBuilder.build();
    
            //表描述对象建造者
            TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
            //将列族对象添加进表描述
            tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
            //创建表描述对象
            TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
    
            //创建表
            admin.createTable(tableDescriptor);
            admin.close();
        }
    
    
        /**
         * 创建表(多列族)
         * @param tableName
         * @param familys
         * @throws IOException
         */
        public static void createTable(String tableName,String[] familys) throws IOException {
            Connection connection = connHolder.get();
            //获取admin
            Admin admin = connection.getAdmin();
            //表描述对象建造者
            TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
    
            for (String family : familys) {
                //列族描述对象建造者
                ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(family));
                //设置最大版本号
                columnFamilyDescriptorBuilder.setMaxVersions(3);
                //将列族对象添加进表描述
                tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
            }
    
            //创建表描述对象
            TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
    
            //创建表
            admin.createTable(tableDescriptor);
            admin.close();
        }
    
        /**
         * 根据行号查询数据
         * @param tableName
         * @param rowKey
         * @return
         * @throws IOException
         */
        public static Result selectDataByRowkey(String tableName,String rowKey) throws IOException {
            Connection connection = connHolder.get();
            //获取表
            Table table = connection.getTable(TableName.valueOf(tableName));
            //获取表描述对象
            Get get = new Get(Bytes.toBytes(rowKey));
            Result result = table.get(get);
            return result;
        }
    
        /**
         * 获取某一列族中某一列的某一行数据
         * @param tableName
         * @param rowKey
         * @param family
         * @param column
         * @return
         * @throws IOException
         */
        public static Result selectDataByCol(String tableName,String rowKey,String family,String column) throws IOException {
            Connection connection = connHolder.get();
            //获取表
            Table table = connection.getTable(TableName.valueOf(tableName));
            //获取表描述对象
            Get get = new Get(Bytes.toBytes(rowKey));
            get.addColumn(Bytes.toBytes(family),Bytes.toBytes(column));
            Result result = table.get(get);
            return result;
    
        }
    
    
    
    
    
        /**
         * 打印result
         * @param result
         */
        public static void showResult(Result result){
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                System.out.println("family:" + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println("row:" + Bytes.toString(CellUtil.cloneRow(cell)));
                System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
    
        /**
         * 删除表
         * @param tableName
         * @throws IOException
         */
        public static void deleteTable(String tableName) throws IOException {
            Connection connection = connHolder.get();
            Admin admin = connection.getAdmin();
            TableName name = TableName.valueOf(tableName);
            if (admin.tableExists(name)){
                admin.deleteTable(name);
            }
        }
    
        /**
         * 是否存在表
         * @param tableName
         * @return
         * @throws IOException
         */
        public static boolean tableExists(String tableName) throws IOException {
            Connection connection = connHolder.get();
            Admin admin = connection.getAdmin();
            return  admin.tableExists(TableName.valueOf(tableName));
        }
    
    
    }

    另一位大佬所写(比较全面):

    package com.hadoop.hbase;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Iterator;
    
    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.NamespaceDescriptor;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.TableNotFoundException;
    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.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.filter.CompareFilter;
    import org.apache.hadoop.hbase.filter.FilterList;
    import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
    import org.apache.hadoop.hbase.util.Bytes;
    
    
    public class DBUtil {
        public static Configuration configuration;
        public static Connection connection;
        public static Admin admin;
    
        //**********连接操作**********
        //建立链接
        public static void getConnection() {
            configuration = HBaseConfiguration.create();
            configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/hbase");
            try {
                connection = ConnectionFactory.createConnection(configuration);
                admin = connection.getAdmin();
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
        //关闭链接
        public static void close() {
            try {
                if(admin!=null) {
                    admin.close();
                }
                if(null!=connection) {
                    connection.close();
                }
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    
        //**********命名空间操作**********
        //创建namespace
        public static void createNamespace(String namespace) throws IOException {
            getConnection();
            NamespaceDescriptor nDescriptor = NamespaceDescriptor.create(namespace).build();
            admin.createNamespace(nDescriptor);
            close();
        }
        //查询所有的namespace
        public static ArrayList<String> listNamespace() throws IOException {
            getConnection();
            NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
            ArrayList<String> list = new ArrayList<>();
            for(NamespaceDescriptor nd:namespaceDescriptors) {
                System.out.println(nd.getName());
                list.add(nd.getName());
            }
            close();
            return list;
        }
        //获取指定namespace中所有的表名
        public static ArrayList<String> listTables(String namespace) throws IOException {
            getConnection();
            HTableDescriptor[] tables = admin.listTableDescriptorsByNamespace(namespace);
            ArrayList<String> list = new ArrayList<>();
            for(HTableDescriptor table:tables) {
    //                System.out.println(table.getNameAsString());
                list.add(table.getTableName().getNameAsString());
            }
            close();
            return list;
        }
        //删除namespace
        public static boolean dropNamespace(String namespace) {
            getConnection();
            try {
                admin.deleteNamespace(namespace);
                close();
                return true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                close();
                return false;
            }
        }
    
        //**********表操作**********
        //创建表
        public static void createTable(String tablename,String[] colFamily) throws IOException {
            getConnection();
            TableName tName = TableName.valueOf(tablename);
            if(admin.tableExists(tName)) {
                System.out.println("table exists");
            }else {
                HTableDescriptor hTableDescriptor = new HTableDescriptor(tName);
                for(String str:colFamily) {
                    HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
                    hTableDescriptor.addFamily(hColumnDescriptor);
                }
                admin.createTable(hTableDescriptor);
            }
            close();
        }
        //获取所有列族
        public static ArrayList<String> listColFamilies(String tablename) throws TableNotFoundException, IOException{
            getConnection();
            HTableDescriptor tableDescriptor = admin.getTableDescriptor(TableName.valueOf(tablename));
            HColumnDescriptor[] columnDescriptors = tableDescriptor.getColumnFamilies();
            ArrayList<String> list = new ArrayList<>();
            for(HColumnDescriptor hcd:columnDescriptors) {
                System.out.println(hcd.getNameAsString());
                list.add(hcd.getNameAsString());
            }
            return list;
        }
        //删除列族
        public static boolean deleteColFamily(String tablename,String colname) {
            getConnection();
            try {
                admin.deleteColumn(TableName.valueOf(tablename), Bytes.toBytes(colname));
                close();
                return true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                close();
                return false;
            }
        }
        //删除表
        @SuppressWarnings("finally")
        public static boolean dropTable(String tablename) {
            getConnection();
            try {
                HTableDescriptor descriptor = admin.getTableDescriptor(TableName.valueOf(tablename));
                if(admin.tableExists(TableName.valueOf(tablename))) {
                    if(admin.isTableEnabled(TableName.valueOf(tablename))) {
                        admin.disableTable(TableName.valueOf(tablename));
                    }
                    admin.deleteTable(TableName.valueOf(tablename));
                }
                close();
                return true;
            } catch (TableNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                close();
                return false;
            }
        }
    
        //添加数据(表名,行健,列族,列名,值)/如果存在则修改
        public static boolean insertData(String tablename,String rowkey,String colFamily,String col,String val){
            getConnection();
            Table table;
            try {
                table = connection.getTable(TableName.valueOf(tablename));
                Put put = new Put(Bytes.toBytes(rowkey));
                put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
                table.put(put);
                table.close();
                close();
                return true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                close();
                return false;
            }
        }
        //删除数据(表名,行健)
        public static boolean deleteData(String tablename,String rowkey){
            getConnection();
            Table table;
            try {
                table = connection.getTable(TableName.valueOf(tablename));
                Delete delete = new Delete(Bytes.toBytes(rowkey));
                table.delete(delete);
                table.close();
                close();
                return true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                close();
                return false;
            }
        }
        //浏览数据
        public static void getData(String tablename,String rowkey,String colFamily,String col) throws IOException {
            getConnection();
            Table table = connection.getTable(TableName.valueOf(tablename));
            Get get = new Get(Bytes.toBytes(rowkey));
            get.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col));
            Result result = table.get(get);
            System.out.println(new String(result.getValue(colFamily.getBytes(), col==null?null:col.getBytes())));
            table.close();
            close();
        }
        //扫描数据
        public static Iterator<Result> scan(String tablename) {
            getConnection();
            try {
                Table table = connection.getTable(TableName.valueOf(tablename));
                Scan scan = new Scan();
    //                scan.setStartRow(Bytes.toBytes(startRow));
    //                scan.setStopRow(Bytes.toBytes(endrow));
                ResultScanner scanner = table.getScanner(scan);
                Iterator<Result> iterator = scanner.iterator();
                return iterator;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        }
    
    
        //**********显示内容**********
        //显示scan
        public static Iterator<Result> showScan(Table table,Scan scan) {
            ResultScanner scanner;
            try {
                scanner = table.getScanner(scan);
                Iterator<Result> iterator = scanner.iterator();
                return iterator;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        }
        //显示result
        public static void showResult(Result result) {
            CellScanner cellScanner = result.cellScanner();
            try {
                while(cellScanner.advance()) {
                    Cell cell = cellScanner.current();
                    String rowkey = new String(CellUtil.cloneRow(cell));
                    String colFamily = new String(CellUtil.cloneFamily(cell));
                    String qualifier = new String(CellUtil.cloneQualifier(cell));
                    String value = new String(CellUtil.cloneValue(cell));
                    System.out.println(rowkey+":"+colFamily+"."+qualifier+"="+value);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //迭代器result
        public static void showIterResult(Iterator<Result> iterator) {
            while(iterator.hasNext()) {
                Result result = iterator.next();
                showResult(result);
            }
        }
    
    //**********过滤器**********
    
        //创建单列值过滤器
        public static SingleColumnValueFilter singleColumnValueFilter(String family,String qualifier,CompareFilter.CompareOp compareOp,String value,boolean isNull) {
            SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes(family), Bytes.toBytes(qualifier), compareOp, Bytes.toBytes(value));
            filter.setFilterIfMissing(isNull);
            return filter;
        }
        //过滤器链
        public static Iterator<Result> filterList(String tablename,String type,SingleColumnValueFilter[] lists) {
            FilterList filterList = null;
            if(type.equals("and")) {
                filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
            }else if(type.equals("or")){
                filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE);
            }
            for(SingleColumnValueFilter filter:lists) {
                filterList.addFilter(filter);
            }
            Scan scan = new Scan();
            scan.setFilter(filterList);
            getConnection();
            try {
                Table table = connection.getTable(TableName.valueOf(tablename));
                return showScan(table, scan);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        }
    
    
    
    
        public static void main(String[] args) throws IOException {
    //            String[] colFamily = new String[] {"score","age"};
    //            String tablename = "people";
    //            createTable(tablename,colFamily);
    //            insertData(tablename, "001", "score", "english", "60");
    //            insertData(tablename, "001", "score", "math", "90");
    //            insertData(tablename, "001", "age", "now", "20");
    //            insertData(tablename, "001", "age", "last", "19");
    //            getData(tablename, "001", "score", "english");
    //            insertData(tablename, "001", "score", "english", "90");
    //            getData(tablename, "001", "score", "english");
    //            deleteData(tablename, "001", "score", "english");
    //            createNamespace("zdm");
    //            dropNamespace("ns3");
    //            listNamespace();
    //            listTables("default");
    //            deleteColFamily("people", "age");
    //            listColFamilies("people");
    //            dropTable("people");
    //            listTables("default");
            SingleColumnValueFilter s1 = singleColumnValueFilter("score", "math",CompareFilter.CompareOp.GREATER , "20", true);
            SingleColumnValueFilter s2 = singleColumnValueFilter("age", "",CompareFilter.CompareOp.EQUAL , "70", true);
            SingleColumnValueFilter[] filters = new SingleColumnValueFilter[] {s1,s2};
            showIterResult(filterList("people", "and", filters));
        }
    }
    DBUtil
  • 相关阅读:
    root用户没有权限编辑其他用户处理
    php中 被遗忘的函数
    erlang file操作(IO编程)
    Linux下的MySQL自动备份脚本
    这就是传说中让理科生沉默,让文科生落泪的文理综合体(转)
    LINUX 忘记root密码
    php中 被遗忘的函数
    分页显示的常用操作方法
    php 接口类:interface
    php垃圾回收机制分析
  • 原文地址:https://www.cnblogs.com/cxy0210/p/13832737.html
Copyright © 2011-2022 走看看