import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Table; public class C_insertRows { public static Configuration configuration; public static Connection connection; public static Admin admin; /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub insertRow("student", "2015001", "info", "S_name", "Zhangsan"); insertRow("student", "2015001", "info", "S_sex", "male"); insertRow("student", "2015001", "info", "S_age", "23"); insertRow("student", "2015002", "info", "S_name", "Mary"); insertRow("student", "2015002", "info", "S_sex", "female"); insertRow("student", "2015002", "info", "S_age", "22"); insertRow("student", "2015003", "info", "S_name", "Lisi"); insertRow("student", "2015003", "info", "S_sex", "male"); insertRow("student", "2015003", "info", "S_age", "20"); insertRow("SC", "2015001_123001", "SC_score", "", "86"); insertRow("SC", "2015001_123003", "SC_score", "", "69"); insertRow("SC", "2015002_123002", "SC_score", "", "77"); insertRow("SC", "2015002_123003", "SC_score", "", "99"); insertRow("SC", "2015003_123001", "SC_score", "", "98"); insertRow("SC", "2015003_123002", "SC_score", "", "95"); insertRow("Course", "123001", "info", "C_Name", "Math"); insertRow("Course", "123001", "info", "C_Credit", "2.0"); insertRow("Course", "123002", "info", "C_Name", "Computer Science"); insertRow("Course", "123002", "info", "C_Credit", "5.0"); insertRow("Course", "123003", "info", "C_Name", "English"); insertRow("Course", "123003", "info", "C_Credit", "3.0"); B_getAllData show = new B_getAllData(); show.getTableData("student"); } //建立连接 public static void init(){ configuration = HBaseConfiguration.create(); configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase"); try{ connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); }catch (IOException e){ e.printStackTrace(); } } //关闭连接 public static void close(){ try{ if(admin != null){ admin.close(); } if(null != connection){ connection.close(); } }catch (IOException e){ e.printStackTrace(); } } //C_ public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException { init(); Table table = connection.getTable(TableName.valueOf(tableName)); Put put = new Put(rowKey.getBytes()); put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes()); table.put(put); table.close(); close(); } }