zoukankan      html  css  js  c++  java
  • shiyan

    Count.javapackage cn.edu.zucc.hbase;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.*;
    import java.io.IOException;
    public class CountTable {
        public static Configuration configuration;
        public static Connection connection;
        public static Admin admin;
        public static void countTable(String tableName) throws IOException {
            init(); Table table = connection.getTable(TableName.valueOf(tableName));
            Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan);
            int num = 0;
            for (Result result = scanner.next();
                    result != null; result = scanner .next()) {
                num++;
                }
            System.out.println("行数:" + num); scanner.close(); close();
            }
        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 main(String[] args) {
                    try { countTable("student");
                    }
                    catch (IOException e) {
                        e.printStackTrace();
                        }
                    }
            }


    Delete.java

    package cn.edu.zucc.hbase;

    import java.io.IOException;

    import org.apache.hadoop.conf.Configuration;
    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;
    public class DeleteRow {
        public static Configuration configuration;
        public static Connection connection;
        public static Admin admin;
        public static void deleteRow(String tableName, String rowKey, String colFamily, String col) throws IOException {
            init();
            Table table = connection.getTable(TableName.valueOf(tableName));
            Delete delete = new Delete(rowKey.getBytes());
            delete.addFamily(Bytes.toBytes(colFamily));
            delete.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col));
            table.delete(delete);
            table.close(); close();
            } 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 main(String[] args) {
                try { deleteRow("student", "95001", "score", "math");
                }
                catch (IOException e) {
                    e.printStackTrace();
                    }
                }
            }
     InsertRow.java

    package cn.edu.zucc.hbase;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.*;
    import java.io.IOException;
    public class InsertRow {
        public static Configuration configuration;
        public static Connection connection;
        public static Admin admin;
        public static void insertRow(String tableName, String rowKey, String colFamily, String col, String val) throws IOException {
            init();
            Table table = connection.getTable(TableName.valueOf(tableName));
            Put put = new Put(rowKey.getBytes());
            put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
            table.put(put);
            table.close();
            close();
            } 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 main(String[] args) {
                    try {
                        insertRow("student", "95001", "course", "math", "100");
                        }
                    catch (IOException e)
                    {
                            e.printStackTrace();
                        }
                    }
                }


    ListTableData

    package cn.edu.zucc.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 java.io.IOException;
    public class ListTableData {
        public static Configuration configuration;
        public static Connection connection;
        public static Admin admin;
        public static void getData(String tableName) throws IOException {
            init();
            Table table = connection.getTable(TableName.valueOf(tableName));
            Scan scan = new Scan();
            ResultScanner scanner = table.getScanner(scan);
            for (Result result : scanner) {
                printRecoder(result);
                }
            close();
        }
        public static void printRecoder(Result result) throws IOException {
            for (Cell cell : result.rawCells()) {
                System.out.print("行健: " + new String(CellUtil.cloneRow(cell)));
                System.out.print("列簇: " + new String(CellUtil.cloneFamily(cell)));
                System.out.print(" 列: " + new String(CellUtil.cloneQualifier(cell)));
                System.out.print(" 值: " + new String(CellUtil.cloneValue(cell)));
                System.out.println("时间戳: " + cell.getTimestamp());
                }
            }
        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 main(String[] args) {
                try { getData("student");
                } catch (IOException e) {
                    e.printStackTrace();
                    }
                }
            }


    ListTables

    package cn.edu.zucc.hbase;

    import java.io.IOException;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.HTableDescriptor;
    import org.apache.hadoop.hbase.client.Admin;
    import org.apache.hadoop.hbase.client.Connection;
    import org.apache.hadoop.hbase.client.ConnectionFactory;
    public class ListTables {
        public static Configuration configuration;
        public static Connection connection;
        public static Admin admin;
        public static void listTables() throws IOException {
            init();
            HTableDescriptor[] hTableDescriptors = admin.listTables();
            for (HTableDescriptor hTableDescriptor : hTableDescriptors) {
                System.out.println("表名:" + hTableDescriptor.getNameAsString());
                }
            close();
            }
        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 (connection != null) {
                    connection.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    }
            }
        public static void main(String[] args) {
            try { listTables();
            } catch (IOException e) {
                e.printStackTrace();
                }
            }
        }



    TruncateTable

    package cn.edu.zucc.hbase;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    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 java.io.IOException;
    public class TruncateTable {
        public static Configuration configuration;
        public static Connection connection;
        public static Admin admin;
        public static void clearRows(String tableName) throws IOException {
            init();
            TableName tablename = TableName.valueOf(tableName);
            admin.disableTable(tablename);
            admin.truncateTable(tablename, false); close();
            }
        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 main(String[] args) {
            try {
                clearRows("student");
                }
            catch (IOException e) {
                e.printStackTrace();
                }
            }
        }


    thirdone.java

    package cn.edu.zucc.hbase;

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.HColumnDescriptor;
    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 java.io.IOException;
    public class CreateTable {
        public static Configuration configuration;
        public static Connection connection;
        public static Admin admin;
        
        public static void createTable(String tableName,String[] fields)throws IOException{
            init();
            TableName tablename = TableName.valueOf(tableName);
            if(admin.tableExists(tablename)){
                System.out.println("table is exists");
                admin.disableTable(tablename);
                admin.deleteTable(tablename);
            }
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tablename);
            for(String str : fields){
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
                hTableDescriptor.addFamily(hColumnDescriptor);
            }
            admin.createTable(hTableDescriptor);
            close();
        }
        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 main(String[] args){
            String[] fileds = {"Score"};
            try{
                createTable("person",fileds);
            }catch(IOException e){
                e.printStackTrace();
            }
        }

    }

  • 相关阅读:
    There are multiple modules with names that only differ in casing. 问题的一种解决方法
    微软官方tfs源码转移至华为软开云操作过程 by vs2019
    【转载】docker hub下载速度太慢,更新国内源
    visual studio 2019不能在vue文件中直接识别less语法
    visual studio 2019 中初始化 vue.js 项目
    vs在微软官方tfs创建私有项目过程
    WinCC OA-C#-开发一个EricManager
    WinCC OA-JS-CTRL脚本与JS交互
    WinCC OA-JS-使用plotly.js开发个性化图表
    WinCC OA-JS-WebView解析及开发环境搭建
  • 原文地址:https://www.cnblogs.com/lijing925/p/9790458.html
Copyright © 2011-2022 走看看