zoukankan      html  css  js  c++  java
  • 在终端打印出指定表的所有记录数据。

    import java.io.IOException;
    
    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.HTableDescriptor;
    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.ConnectionFactory;
    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;
    
    
    public class B_getAllData {
        public static Configuration configuration;
        public static Connection connection;
        public static Admin admin;
        
        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            getTableData("student");
        }
        //建立连接
        public static void init(){
            configuration  = HBaseConfiguration.create();
            configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
            try{
                connection = ConnectionFactory.createConnection(configuration);
                admin = connection.getAdmin();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        //关闭连接
        public static void close(){
            try{
                if(admin != null){
                    admin.close();
                }
                if(null != connection){
                    connection.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        public static void getTableData(String tablename) throws IOException {
            init();
            HTableDescriptor hTableDescriptors[] = admin.listTables();
            System.out.println(tablename);
            Table table = connection.getTable(TableName.valueOf(tablename));
            //创建一个空的Scan实例
            Scan scan1 = new Scan();
            //在行上获取遍历器
            ResultScanner scanner1 = table.getScanner(scan1);
            //打印行的值
            for (Result res : scanner1) {
                Cell[] cells = res.rawCells();
                for(Cell cell:cells){
                    System.out.println(new String(CellUtil.cloneRow(cell))+" "+new String(CellUtil.cloneFamily(cell))+" "+new String(CellUtil.cloneQualifier(cell))+" "+new String(CellUtil.cloneValue(cell))+" ");
                }
            }
            //关闭释放资源
            scanner1.close();
            table.close();
            close();
        }
    }
  • 相关阅读:
    rt_thread studio结合cubmx进行stm32驱动开发学习
    rt_thread之时钟管理
    rt_thread线程间通讯
    使用jQuery开发iOS风格的页面导航菜单
    使用jQuery开发一个带有密码强度检验的超酷注册页面
    使用Javascript来创建一个响应式的超酷360度全景图片查看幻灯效果
    [英] 推荐 15 个 jQuery 选择框插件
    JavaScript封装Ajax(类JQuery中$.ajax()方法)
    阿里前端电话面试
    基于HTML5的Web跨设备超声波通信方案
  • 原文地址:https://www.cnblogs.com/MiraculousB/p/13958116.html
Copyright © 2011-2022 走看看