已经创建了一个学生宿舍管理系统
数据表表名:student_info
列族1:students
列族2:dormitorys
列族3:staff_members
添加方法:
package myhbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
public class PutData {
public static void main(String[] args) throws MasterNotRunningException,
ZooKeeperConnectionException, IOException {
String tableName = "student_info";
String columnFamily1 = "students";
String columnFamily2 = "dormitorys";
String columnFamily3 = "staff_members";
put(tableName, "001", columnFamily1, "student_id", "164804098");
put(tableName, "001", columnFamily1, "student_name", "tingtingrugai");
put(tableName, "001", columnFamily1, "gender", "male");
put(tableName, "001", columnFamily1, "tel", "123456789");
put(tableName, "001", columnFamily2, "hostel_No.", "115");
put(tableName, "001", columnFamily2, "bed", "3");
put(tableName, "001", columnFamily3, "staff_nameA", "zhangsan");
put(tableName, "001", columnFamily3, "staff_ageA", "40");
put(tableName, "001", columnFamily3, "staff_genderA", "male");
}
public static Configuration getConfiguration() {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
conf.set("hbase.zookeeper.quorum", "localhost");
return conf;
}
public static void put(String tableName, String row, String columnFamily,
String column, String data) throws IOException {
HTable table = new HTable(getConfiguration(), tableName);
Put put = new Put(Bytes.toBytes(row));
put.add(Bytes.toBytes(columnFamily),
Bytes.toBytes(column),
Bytes.toBytes(data));
table.put(put);
System.err.println("SUCCESS");
}
}