• 本地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());
                }*/
    
    
    
    
    
    
    
    
        }
    }

  • 相关阅读:
    星空雅梦
    星空雅梦
    Navicat permium快捷键
    Mysql建表+创建索引
    mysql 常见ALTER TABLE操作
    mysql常用的索引种类
    xShell终端中文乱码-解决方法
    git删除本地分支和远程分支
    git版本回退
    log4j2配置文件log4j2.xml详解
  • 原文地址:https://www.cnblogs.com/yizhixiaozhu/p/14211729.html
Copyright © 2020-2023  润新知