zoukankan      html  css  js  c++  java
  • hbase安装并且简单的例子

    详细过程可以参考林子雨老师

    http://dblab.xmu.edu.cn/blog/2442-2/

    简单的例子

        import org.apache.hadoop.conf.Configuration;
        import org.apache.hadoop.hbase.*;
        import org.apache.hadoop.hbase.client.*;
        import org.apache.hadoop.hbase.util.Bytes;
         
        import java.io.IOException;
        public class ExampleForHBase {
            public static Configuration configuration;
            public static Connection connection;
            public static Admin admin;
            public static void main(String[] args)throws IOException{
                init();
                createTable("student",new String[]{"score"});
                insertData("student","zhangsan","score","English","69");
                insertData("student","zhangsan","score","Math","86");
                insertData("student","zhangsan","score","Computer","77");
                getData("student", "zhangsan", "score","English");
                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 createTable(String myTableName,String[] colFamily) throws IOException {
                TableName tableName = TableName.valueOf(myTableName);
                if(admin.tableExists(tableName)){
                    System.out.println("talbe is exists!");
                }else {
                    TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
                    for(String str:colFamily){
                        ColumnFamilyDescriptor family = 
        ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();
                        tableDescriptor.setColumnFamily(family);
                    }
                    admin.createTable(tableDescriptor.build());
                } 
            }
         
            public static void insertData(String tableName,String rowKey,String colFamily,String col,String val) throws IOException { 
                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(); 
            }
         
            public static void getData(String tableName,String rowKey,String colFamily, String col)throws  IOException{ 
                Table table = connection.getTable(TableName.valueOf(tableName));
                Get get = new Get(rowKey.getBytes());
                get.addColumn(colFamily.getBytes(),col.getBytes());
                Result result = table.get(get);
                System.out.println(new String(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes())));
                table.close(); 
            }
        }
    View Code
  • 相关阅读:
    大数加法、乘法实现的简单版本
    hdu 4027 Can you answer these queries?
    zoj 1610 Count the Colors
    2018 徐州赛区网赛 G. Trace
    1495 中国好区间 尺取法
    LA 3938 动态最大连续区间 线段树
    51nod 1275 连续子段的差异
    caioj 1172 poj 2823 单调队列过渡题
    数据结构和算法题
    一个通用分页类
  • 原文地址:https://www.cnblogs.com/ljpljm/p/14204024.html
Copyright © 2011-2022 走看看