zoukankan      html  css  js  c++  java
  • 本地IDEA向ubantu的hbase传数据

    今天用本地idea向hbase中传数据

    package one;
    
    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;
    import org.jruby.RubyProcess;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    
    public class test1 {
        public static Configuration conf;
    
        static{
            conf = HBaseConfiguration.create();
        }
        //4.添加数据put 'user','rowkey','info:name','tony'
        public static void addRow(String tableName,String rowkey,String cf,String column,String value){
            try {
                //对表操作需要使用HbaseAdmin
                Connection connection = ConnectionFactory.createConnection(conf);
                Table t = connection.getTable(TableName.valueOf(tableName));
                //1.表如果存在,请输入其他表名
                if (!isExist(tableName)) {
                    System.out.println("表不存在");
                } else {
                    //2.用put方式加入数据
                    Put p = new Put(Bytes.toBytes(rowkey));
                    //3.加入数据
                    p.addColumn(Bytes.toBytes(cf),Bytes.toBytes(column),Bytes.toBytes(value));
                    t.put(p);
    
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //1.判断一张表是否存在
        public static boolean isExist(String tableName){
            //对表操作需要使用HbaseAdmin
            try {
                Connection connection = ConnectionFactory.createConnection(conf);
                //管理表
                HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
                return admin.tableExists(TableName.valueOf(tableName));
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(-1);
            return false;
        }
        public static void main(String[] args) {
                boolean flag=isExist("wjm");
                System.out.println(flag);
                //test1.addRow("wjm","01","info","name","miaokezhuo");
                /*//Student
    
                int i=0;
                List<Bean> list=cin.cin_txt();
                for (Bean item : list) {
                    System.out.println(i);
                    i++;
                    test1.addRow("wjm",""+i,"cf","Ip",item.getIp());
                    test1.addRow("wjm",""+i,"cf","Date",item.getDate());
                    test1.addRow("wjm",""+i,"cf","Day",item.getDay());
                    test1.addRow("wjm",""+i,"cf","Traffic",item.getTraffic());
                    test1.addRow("wjm",""+i,"cf","Type",item.getType());
                    test1.addRow("wjm",""+i,"cf","Id",item.getId());
                }*/
    
    
    
    
    
    
    
    
        }
    }

  • 相关阅读:
    spring通过注解依赖注入和获取xml配置混合的方式
    Spring 3.0就这么简单读书笔记
    spring源码测试
    spring文档翻译
    敏感词过滤算法
    mongodb的地理空间索引如何在solr中体现
    mongodb的地理空间索引常见的问题
    mysql的distinct理解
    zookeeper管理solr的配置文件
    驾校理论考试
  • 原文地址:https://www.cnblogs.com/yizhixiaozhu/p/14211729.html
Copyright © 2011-2022 走看看