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