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.Delete;
    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 D_clearTableData {
        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
            B_getAllData show = new B_getAllData();
            show.getTableData("student");
            clearTableData("student");
            show.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();
            }
        }
        /*
         * D_
         */
        public static void clearTableData(String tablename) throws IOException {
            init();
            HTableDescriptor hTableDescriptors[] = admin.listTables();
            Table table = connection.getTable(TableName.valueOf(tablename));
            //创建一个空的Scan实例
            Scan scan1 = new Scan();
            //在行上获取遍历器
            ResultScanner scanner1 = table.getScanner(scan1);
            //打印行的值
            for (Result res : scanner1) {
                System.out.println(res);
                Cell[] cells = res.rawCells();
                for(Cell cell:cells){
                    String rowKey = new String(CellUtil.cloneRow(cell));
                    String colFamily = new String(CellUtil.cloneFamily(cell));
                    Delete delete = new Delete(rowKey.getBytes());
                    delete.addFamily(colFamily.getBytes());
                    table.delete(delete);
                }
            }
            //关闭释放资源
            scanner1.close();
            table.close();
            close();
        }
    }
  • 相关阅读:
    Linux入门之系统启动
    2017.12.25 Android数据存储方案
    2017.12.18 Android开发之进程讲解
    2017.12.18 Android开发之消息队列(实现子线程修改UI组件)
    2017.12.4 Android开发之ListView组件
    2017.12.10 把KIE的jbpm_console嵌入到我们的web项目中
    2017.12.07 React组件嵌套以及for循环渲染子组件
    2017.12.07 React路由到不同组件界面
    2017.12.07 Ajax获取服务器数据并发送到前端
    B-树&B+树以及其在数据库中的应用
  • 原文地址:https://www.cnblogs.com/MiraculousB/p/13958121.html
Copyright © 2011-2022 走看看