0

我对 HDP 很陌生,我想创建一个包含多列的 hbase 表,并从 csv 文件加载数据,如下所示

.csv 文件

如您所见,我有每个示例列族“信息人员”,其中包含多个列,例如“nom”“prenom”等。

所以我的问题是: - 如何在 hdp 沙箱上使用 java api 创建表 hbase?- 如何从我的 csv 文件中加载数据?

ps:我尝试创建表,但我不知道如何在沙盒上运行它?把我的java类放在哪里?我需要配置一些东西吗?

这是我的代码

    import java.io.IOException;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.conf.Configuration;

public class CreateTable {

   public static void main(String[] args) throws IOException {

      // Instantiating configuration class
      Configuration con = HBaseConfiguration.create();
        con.set("hbase.zookeeper.property.clientPort", "2181");
        con.set("hbase.zookeeper.quorum", "hortonworks.hbase.vm");
        con.set("zookeeper.znode.parent", "/hbase-unsecure");
        System.out.println("HBase is running!");

      // Instantiating HbaseAdmin class
      HBaseAdmin admin = new HBaseAdmin(con);

      // Instantiating table descriptor class
      HTableDescriptor tableDescriptor = new
      TableDescriptor(TableName.valueOf("competence"));

      // Adding column families to table descriptor
      tableDescriptor.addFamily(new HColumnDescriptor("Infos_collaborateur"));
      tableDescriptor.addFamily(new HColumnDescriptor("Infos_Rh"));
      tableDescriptor.addFamily(new HColumnDescriptor("Savoir_faire"));
      tableDescriptor.addFamily(new HColumnDescriptor("Savoir_etre"));
      tableDescriptor.addFamily(new HColumnDescriptor("Langues"));
      tableDescriptor.addFamily(new HColumnDescriptor("Java:Developpement/Librairies/API/Frameworks/CMS"));
      tableDescriptor.addFamily(new HColumnDescriptor("PHP/Frameworks"));
      tableDescriptor.addFamily(new HColumnDescriptor("Techno_Web/Frameworks"));
      tableDescriptor.addFamily(new HColumnDescriptor("Autres"));
      tableDescriptor.addFamily(new HColumnDescriptor("ERP:Language/Outils"));
      tableDescriptor.addFamily(new HColumnDescriptor("Mobile:natif"));
      tableDescriptor.addFamily(new HColumnDescriptor("Mobile:Cross"));
      tableDescriptor.addFamily(new HColumnDescriptor("Infographie/creas"));
      tableDescriptor.addFamily(new HColumnDescriptor("Outils_de_developpement/Software"));
      tableDescriptor.addFamily(new HColumnDescriptor("Analytics"));
      tableDescriptor.addFamily(new HColumnDescriptor("Outils_Microsoft"));
      tableDescriptor.addFamily(new HColumnDescriptor("Developpements/Librairies"));
      tableDescriptor.addFamily(new HColumnDescriptor("BaseDeDonnees/FluxDeDonnees"));
      tableDescriptor.addFamily(new HColumnDescriptor("Windows:SystemeDexploitation/serveur"));
      tableDescriptor.addFamily(new HColumnDescriptor("AutresOS"));
      tableDescriptor.addFamily(new HColumnDescriptor("Plateforms"));
      tableDescriptor.addFamily(new HColumnDescriptor("Serveur_web_parametrage"));
      tableDescriptor.addFamily(new HColumnDescriptor("Serveur_Application_parametrage"));
      tableDescriptor.addFamily(new HColumnDescriptor("Integration/fonctionnel"));
      tableDescriptor.addFamily(new HColumnDescriptor("Outils_de_conception/de_gestion_projet"));
      tableDescriptor.addFamily(new HColumnDescriptor("AMOA"));
      tableDescriptor.addFamily(new HColumnDescriptor("Experience"));   
      tableDescriptor.addFamily(new HColumnDescriptor("Interventions"));            

      // Execute the table through admin
      admin.createTable(tableDescriptor);
      System.out.println(" Table created ");
   }
}

谢谢提前

4

1 回答 1

0

如果您尝试从本地计算机运行 java 程序以连接到沙箱 hbase 和 zookeeper,那么您需要在沙箱设置 > 网络 > 高级 > 端口转发中对 2181 端口进行端口转发。提供任何名称,例如 zk,协议:TCP,Hist IP:127.0.0.1,主机端口:2181,访客端口:2181。然后在您的程序中设置 conf 如下并运行程序:

con.set("hbase.zookeeper.property.clientPort", "2181");
con.set("hbase.zookeeper.quorum", "127.0.0.1");

在您的 java 程序中,您可以使用扫描仪 api 读取 csv 文件以供参考http://www.journaldev.com/2335/read-csv-file-java-scanner并使用 java hbase api 存储数据以存储数据以供参考https://autofei.wordpress.com/2012/04/02/java-example-code-using-hbase-data-model-operations/

其他选项是将您的文件和 java 程序 jar 发送到沙箱并在那里运行。要复制或 ssh 到沙箱,您需要像上面一样进行端口转发,提供主机端口:2222,访客端口:22

希望这可以帮助你...

于 2016-09-08T15:58:24.133 回答