HBase API 创建表
生活随笔
收集整理的這篇文章主要介紹了
HBase API 创建表
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.配置文件和連接信息
// 兩個(gè)變量的聲明:private static Connection connection = null;private static Admin admin = null;static{try { // 1.獲取配置文件信息Configuration configuration = HBaseConfiguration.create();configuration.set("hbase.zookeeper.quorum","vincen,vincen1,vincen2");// 2.創(chuàng)建連接對象connection = ConnectionFactory.createConnection(configuration);// 3.創(chuàng)建admin對象admin = connection.getAdmin();} catch (IOException e) {e.printStackTrace();}}2.判斷表是否存在
public static boolean isTableExist(String tableName) throws IOException {// 5.判斷表是否存在boolean exists = admin.tableExists(TableName.valueOf(tableName));// 6.返回結(jié)果return exists;}3.創(chuàng)建表
public static void creaTable(String tableName,String... cfs) throws IOException { // 10.1 是否存在列族信息if(cfs.length <= 0){System.out.println("請?jiān)O(shè)置列族信息!");return;}// 10.2 判斷表是否存在if(isTableExist(tableName)){System.out.println(tableName + "表已存在!");return;}// 10.3 創(chuàng)建表描述器HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));// 10.4 循環(huán)添加列族信息for (String cf : cfs) { // 10.5 創(chuàng)建列族描述器HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf); // 10.6 添加具體的列族信息hTableDescriptor.addFamily(hColumnDescriptor);}// 10.7 創(chuàng)建表admin.createTable(hTableDescriptor);}4.單獨(dú)創(chuàng)建關(guān)閉資源
public static void close(){if(admin != null){try {admin.close();} catch (IOException e) {e.printStackTrace();}}if(connection!=null){try {connection.close();} catch (IOException e) {e.printStackTrace();}}}6.測試和關(guān)閉
public static void main(String[] args) throws IOException {// 8.測試表是否存在System.out.println(isTableExist("stu1"));// 11.創(chuàng)建表測試creaTable("stu1","info1","info2"); // 12.創(chuàng)建完測試表是否存在System.out.println(isTableExist("stu1"));// 9.關(guān)閉資源的調(diào)用close();}整體詳情:
package test;import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; 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.HTable;import java.io.IOException;public class CreateTable {// 兩個(gè)變量的聲明:private static Connection connection = null;private static Admin admin = null;static{try { // 1.獲取配置文件信息Configuration configuration = HBaseConfiguration.create();configuration.set("hbase.zookeeper.quorum","vincen,vincen1,vincen2");// 2.創(chuàng)建連接對象connection = ConnectionFactory.createConnection(configuration);// 3.創(chuàng)建admin對象admin = connection.getAdmin();} catch (IOException e) {e.printStackTrace();}}// 4.判斷表是否存在public static boolean isTableExist(String tableName) throws IOException {// 5.判斷表是否存在boolean exists = admin.tableExists(TableName.valueOf(tableName));// 6.返回結(jié)果return exists;}// 10.創(chuàng)建表public static void creaTable(String tableName,String... cfs) throws IOException { // 10.1 是否存在列族信息if(cfs.length <= 0){System.out.println("請?jiān)O(shè)置列族信息!");return;}// 10.2 判斷表是否存在if(isTableExist(tableName)){System.out.println(tableName + "表已存在!");return;}// 10.3 創(chuàng)建表描述器HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));// 10.4 循環(huán)添加列族信息for (String cf : cfs) { // 10.5 創(chuàng)建列族描述器HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf); // 10.6 添加具體的列族信息hTableDescriptor.addFamily(hColumnDescriptor);}// 10.7 創(chuàng)建表admin.createTable(hTableDescriptor);}// 7.關(guān)閉資源public static void close(){if(admin != null){try {admin.close();} catch (IOException e) {e.printStackTrace();}}if(connection!=null){try {connection.close();} catch (IOException e) {e.printStackTrace();}}}public static void main(String[] args) throws IOException {// 8.測試表是否存在System.out.println(isTableExist("stu1"));// 11.創(chuàng)建表測試creaTable("stu1","info1","info2"); // 12.創(chuàng)建完測試表是否存在System.out.println(isTableExist("stu1"));// 9.關(guān)閉資源的調(diào)用close();} }總結(jié)
以上是生活随笔為你收集整理的HBase API 创建表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: thinkphp无法登陆,session
- 下一篇: 基于FPGA VHDL 的 ASK调制与