iT邦幫忙

0

HBase JAVA API 連線

  • 分享至 

  • xImage

我嘗試利用網路上教學的方式做一個HBase java 的資料傳遞,但是在執行時一直出現錯誤,以下是我的程式碼

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
public class Input_HBase {
    public static void main(String avgs[]) throws IOException{
        FileReader fr = new FileReader("D:\\DataIDSeq\\download-20190618T065704Z-001\\part-r-00000_Virus7538_VS_HomoSapiens20160721_26s_Len100_HumanAndVirus_NonPolyTandem.csv");
        BufferedReader bfr = new BufferedReader(fr);
        String str;
        String data[];


        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum","10.96.3.69:2181");
        config.set("hbase.zookeeper.property.clientPort","60000");
        HBaseAdmin admin = new HBaseAdmin(config);
        String tablename = "Virus_Search";
        HTable hTable = new HTable(config,tablename);

        while((str = bfr.readLine())!=null){
            data = new String(str).trim().split(",");
            for(int i = 0;i<data.length;i++){
                
                Put p = new Put(Bytes.toBytes(data[0]));
               
                byte[] familyName1 = Bytes.toBytes("Seq");
                byte[] familyName2 = Bytes.toBytes("Len");
                byte[] familyName3 = Bytes.toBytes("VirusID");
                String [] Column1_1 = new String[]{"seq","len","virusid"};

          p.add(familyName1,Bytes.toBytes(Column1_1[0]),Bytes.toBytes(data[0]));
                p.add(familyName2,Bytes.toBytes(Column1_1[1]),Bytes.toBytes(data[1]));
                p.add(familyName3,Bytes.toBytes(Column1_1[2]),Bytes.toBytes(data[2]));

                hTable.put(p);
                System.out.println("data inserted");

            }
        }
        hTable.close();
    }
}

以下為錯誤訊息

"C:\Program Files\Java\jdk1.8.0_231\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.2.3\lib\idea_rt.jar=55024:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.2.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_231\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\mysql-connector-java-5.1.25-bin.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\rt.jar;D:\Code\InputData_HBase\out\production\InputData_HBase;E:\Hbase_Java_Connect_JARRRRRRRRRR\hadoop-common-2.7.3.2.6.3.0-235.jar;E:\Hbase_Java_Connect_JARRRRRRRRRR\hbase-client-1.1.2.2.6.3.0-235.jar;E:\Hbase_Java_Connect_JARRRRRRRRRR\hbase-common-1.1.2.2.6.3.0-235.jar" Input_HBase
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
	at org.apache.hadoop.conf.Configuration.<clinit>(Configuration.java:186)
	at Input_HBase.main(Input_HBase.java:25)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	... 2 more

Process finished with exit code 1

我有試著去尋找錯誤訊息的意思,網路上是說缺少commons-logging.jar這個jar檔,有去下載也匯入了,但還是一樣的問題,請問還有什麼解決方法。

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
Darwin Watterson
iT邦好手 1 級 ‧ 2019-11-09 17:06:01

參考這篇導入maven管理libaray吧!
應該是jar檔不完整的錯誤!
Maven 構建Java專案 通過JavaApi 操作 Hbase

感謝您的回答,我測試看看

我要發表回答

立即登入回答