首页 > 技术文章 > shiyan

lijing925 2018-10-15 13:22 原文

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();
        }
    }

}

 

推荐阅读