大而话之BigData 2020-02-01
Hbase 创建表 插入数据
创建表
public static void main(String[] args) throws IOException { // Instantiating configuration class 初始化配置文件 Configuration con = HBaseConfiguration.create(); // Instantiating HbaseAdmin class 初始化HbaseAdmin HBaseAdmin admin = new HBaseAdmin(con); // Instantiating table descriptor class 设置表名 HTableDescriptor tableDescriptor = new TableDescriptor(TableName.valueOf("member")); // Adding column families to table descriptor 设置列族名(可设置多个) tableDescriptor.addFamily(new HColumnDescriptor("personal")); tableDescriptor.addFamily(new HColumnDescriptor("professional")); // Execute the table through admin admin.createTable(tableDescriptor); System.out.println(" Table created "); }
插入数据
public static void main(String[] args) throws IOException { //第一步,设置HBsae配置信息 Configuration configuration = HBaseConfiguration.create(); //注意。这里这行目前没有注释掉的,这行和问题3有关系 是要根据自己zookeeper.znode.parent的配置信息进行修改。 configuration.set("hbase.zookeeper.quorum", "192.168.222.128"); //hbase 服务地址 configuration.set("hbase.zookeeper.property.clientPort", "2181"); //端口号 //这里使用的是接口Admin 该接口有一个实现类HBaseAdmin 也可以直接使用这个实现类 Admin admin = ConnectionFactory.createConnection(configuration).getAdmin(); conn = ConnectionFactory.createConnection(configuration); if (admin != null) { try { //获取到数据库所有表信息 HTableDescriptor[] allTable = admin.listTables(); for (HTableDescriptor hTableDescriptor : allTable) { System.out.println(hTableDescriptor.getNameAsString()); } } catch (IOException e) { e.printStackTrace(); } } String id = "001828"; String name = "Paul_Smith"; //2、创建数据对象 String rowKey = "101" + "_" + id + "_" + name ; Put put = new Put(Bytes.toBytes(rowKey)); byte[] family = Bytes.toBytes("personal"); put.addColumn(family, Bytes.toBytes("key"), Bytes.toBytes(id)); put.addColumn(family, Bytes.toBytes("value"), Bytes.toBytes(name)); List<Put> puts = new ArrayList<Put>(); puts.add(put); Table table = conn.getTable(TableName.valueOf("member")); table.put(puts); table.close(); System.out.println("sucsssss"); }