日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HBase API 创建表

發(fā)布時(shí)間:2023/12/10 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。