使用Java连接到远程HBase

时间:2016-11-01 21:01:06

标签: java maven hadoop hbase

我试图在我的学校提供​​的集群上建立与远程HBase的连接,但它失败了。 我在没有HBase的情况下在我自己的机器上执行如下代码:

<%
String searchURI = request.getQueryString();
String requestTemp = request.getParameter("num");
requestTemp = stripXSS(encodeHTML(requestTemp));
String query = request.getParameter("q");
query = encodeHTML(query); 

String client = request.getParameter("client");
int ud = 1;

query = stripXSS(query);
// Get encoded query
int  queryPos = searchURI.indexOf("&q="); 
queryPos = queryPos + 3;
String subQuery = searchURI.substring(queryPos);
int j = subQuery.indexOf('&');
if ( j > -1)  // Found
    subQuery = subQuery.substring(0, j);
String site =  request.getParameter("site");
site = encodeHTML(site);
String keyword = "null:" + query;
String resultCount="zero";
int resultsPerPage = 5;
try{
    resultsPerPage = Integer.parseInt(request.getParameter("num"));
} catch(Exception e) {
}

int maxNumReturned = 1000;

int PageNo= 0;
int NextPage=0;
int PrevPage=0;

boolean sortByRelevance=true;
String previous = "";
String next = "";
String currentPage ="";
String resultsFrom="";
int total = 0;
int totalPages = 0;
int startResults = 0;
requestTemp = request.getParameter("start");
requestTemp = encodeHTML(requestTemp);

if (requestTemp == null) {
    startResults = 0;
} else {
    startResults = Integer.parseInt(requestTemp);
    if (startResults == 1){
        startResults = 0;
    }
}

int fromRequest = searchURI.indexOf("?status=RMI1");
if (fromRequest >=0){
    searchURI = searchURI.substring(0,fromRequest);
}

int fromRequestQuery = query.indexOf("?status=RMI1");
if (fromRequestQuery >=0) {
    query = query.substring(0,fromRequestQuery);
}

if (site == null) {
    site = "www.test.com";
}

PageNo = startResults;

String runSearch = request.getParameter("OPN");
%>

<form name="searchform" action="?" method="get" id="searchpg">
    <input type="hidden" name="output" value="xml_no_dtd">
    <input type="hidden" name="sort" value="date:D:L:d1">
    <input type="hidden" name="client" value="google-csbe"> <!--  default_frontend -->
    <input type="hidden" name="ud" value="1">
    <input type="hidden" name="oe" value="UTF-8">
    <input type="hidden" name="ie" value="UTF-8">
    <input type="hidden" name="OPN" value="RT1">
    <input type="hidden" name="num" value="<%=resultsPerPage%>">
    <input type="hidden" name="start" value="0">
    <input type="hidden" name="site" value="<%=site%>">
    <input type="hidden" name="cx" value="&cx=00100000009622659069:xxxxxx_xxxx">
    <span class="search_area">
        <input type="text" id="q" name="q" class="txtbox" />
        <input type="image" src="/siteImages//searchresults_submit.gif" alt="Submit" id="searchpg_submit" />
    </span>
</form>

<%!
    public String KWMatch(Node node) {
        Node childNode = node.getFirstChild();
        String docURI = "";
        String GD = null;
        String docTitle = null;
        String docDesc = "";

        while(childNode != null) {          
            //print link
            String nodeName = childNode.getNodeName();              
            if (nodeName.equals("GL")){
                docURI = childNode.getFirstChild().getNodeValue();
            }else if (nodeName.equals("GD")){
                GD = childNode.getFirstChild().getNodeValue();
                if (GD.indexOf("|") > -1) {
                    docTitle = GD.substring(0, GD.indexOf("|"));
                    docDesc = GD.substring(GD.indexOf("|")+1, GD.length());
                } else {
                    docTitle = GD;
                }
            }
            childNode = childNode.getNextSibling();
        }
        //Begin 'search_res' div to hold one search result listing
        return("<div class=\"search_res\"><a href=\"" + docURI + "\">" + docTitle + "</a><p>" + docDesc + "</p></div>");
    }
%>

<%
try { 

if ("RT1".equals(runSearch)){

out.println("spot 1");
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();       
        URL url = new URL("http://xx.xxx.xxx.xx:5454/test/search?" + searchURI);
        URLConnection conn = url.openConnection();
        conn.setConnectTimeout(10000);
        conn.setReadTimeout(10000);

        Document searchDoc = docBuilder.parse(conn.getInputStream());
out.println("spot 1.1");
        Element searchElements = searchDoc.getDocumentElement();
        NodeList resultsList = searchElements.getElementsByTagName("R");
        NodeList resultsCount = searchElements.getElementsByTagName("M");
        NodeList keymatchCount = searchElements.getElementsByTagName("GM");
out.println("spot 1.2");
        String preNext = "";
out.println("spot 2");
        if(resultsCount.getLength() <= 0 && keymatchCount.getLength() > 0){
            out.print("<h2 class=\"searchpage\">" + keymatchCount.getLength() + " results found for " + query + "</h2>");
            for(int i=0; i<keymatchCount.getLength(); i++) {
                Node node = keymatchCount.item(i);
                out.print(KWMatch(node));
            }
        } else if(resultsCount.getLength() > 0){
            Node totalNode = resultsCount.item(0);
            int expectedResultCount = Integer.parseInt(totalNode.getFirstChild().getNodeValue()) + (int) keymatchCount.getLength();

            if (expectedResultCount > maxNumReturned){
                expectedResultCount = maxNumReturned;
            }

            //  String encodedQuery = java.net.URLEncoder.encode(query);
            String encodedQuery = subQuery;

            String googleQuery = "q=" + encodedQuery + "&output=xml_no_dtd" + "&sort=" + "date:D:S:d1" + "&client=" + client + "&ud=" + ud + "&oe=UTF-8" + "&ie=UTF-8" 
            + "&site=" + site + "&start=" + (expectedResultCount - resultsPerPage) + "&num=" + resultsPerPage + "&cx=00100000009622659069:xxxxxx_xxxx";

            URL subUrl = new URL("http://xx.xxx.xxx.xx:5454/test/search?" + googleQuery);

            URLConnection subConn = subUrl.openConnection();
            subConn.setConnectTimeout(10000);
            subConn.setReadTimeout(10000);
            Document gSearchDoc = docBuilder.parse(subConn.getInputStream());
            Element gSearchElements = gSearchDoc.getDocumentElement();
            NodeList gResultsList = gSearchElements.getElementsByTagName("R");
out.println("spot 3");  
            Node lastResultNode = gResultsList.item(gResultsList.getLength() -1);
out.println("spot 3.1");

            String lastResultNum = lastResultNode.getAttributes().getNamedItem("N").getNodeValue();
            total = Integer.parseInt(lastResultNum) + keymatchCount.getLength();

                        keyword = query;
            resultCount=String.valueOf(total);

            Node firstNode =resultsList.item(0);
            Node lastNode = resultsList.item(resultsList.getLength()-1);
            String firstResultNum = firstNode.getAttributes().getNamedItem("N").getNodeValue();
            lastResultNum= lastNode.getAttributes().getNamedItem("N").getNodeValue();

            //Node totalNode = resultsCount.item(0);
            //total = Integer.parseInt(totalNode.getFirstChild().getNodeValue());
            totalPages = (total/resultsPerPage)+1;
            startResults = startResults/resultsPerPage;

out.println("spot 4");
out.println(site);


            out.print("<h2 class=\"searchpage\">" + total + " results found for \"" + query + "\"</h2>");

            if(keymatchCount.getLength() > 0 && startResults == 0){
                for(int i=0; i<keymatchCount.getLength(); i++) {
                    Node node = keymatchCount.item(i);
                    out.print(KWMatch(node));
                }
            }
            if(startResults == 0 || total <= resultsPerPage){
                NextPage=(PageNo/resultsPerPage) +1;
                PrevPage = 0;           
                if (total <= resultsPerPage) {
                    next= "";
                    resultsFrom = "";
                } else {        
                    next= "<a href='javascript:NextValues()'><img src='/siteImages/page_right.gif' width='5' height='10' /></a>";
                    //resultsFrom = (1+ PageNo) + " - " + (PageNo + resultsPerPage) + " of " + total;
                    resultsFrom = firstResultNum + " - " + lastResultNum + " of " + total;
                }
                previous = "";
            } else if(startResults==(totalPages -1)) {
                NextPage=(PageNo/resultsPerPage) +1;
                PrevPage =(PageNo/resultsPerPage)-1;
                //resultsFrom = "1 - " + (PageNo + resultsPerPage) + " of " + total;
                resultsFrom = firstResultNum + " - " + lastResultNum + " of " + total;
                next = "";
                previous= "<a href='javascript:PrevValues()'><img src='/siteImages/page_left.gif' width='5' height='10' /></a>";
            } else {
                NextPage=(PageNo/resultsPerPage) +1;
                PrevPage =(PageNo/resultsPerPage)-1;
                //resultsFrom = (1 + PageNo) + " - " + (PageNo + resultsPerPage) + " of " + total;
                resultsFrom = firstResultNum + " - " + lastResultNum + " of " + total;
                next= "<a href='javascript:NextValues()'><img src='/siteImages/page_right.gif' width='5' height='10' /></a>";
                previous= "<a href='javascript:PrevValues()'><img src='/siteImages/page_left.gif' width='5' height='10' /></a>";        
            }
        } else {
            out.print("<h2 class=\"searchpage\">No Results Found results found for \"" + query + "\"</h2>");
        }

        try{
            for(int i=0; i< resultsList.getLength(); i++) {
                Node node = resultsList.item(i);
                Node childNode = node.getFirstChild();
                String docURI = "";
                String docTitle = null;
                String docDesc = "";
                while(childNode != null) {          
                //print link
                String nodeName = childNode.getNodeName();              
                if (nodeName.equals("U")) {
                    docURI = childNode.getFirstChild().getNodeValue();
                } else if (nodeName.equals("T")) {
                    docTitle = childNode.getFirstChild().getNodeValue();
                } else if (nodeName.equals("S")) {
                    try{
                        docDesc = childNode.getFirstChild().getNodeValue();
                    }catch(Exception e){}
                } else { }
                childNode = childNode.getNextSibling();
            }
        %>
            <!-- Begin 'search_res' div to hold one search result listing -->
            <div class="search_res"> <a href="<%=docURI%>"><%=docTitle%></a>
                <p><%=docDesc%></p>
            </div>
        <%
        }
        }catch(Exception e){
        }
}

} catch (Exception e) {
    out.println("The page you are trying to view is unavailable. Please try again later");

    StringWriter sw = new StringWriter();
    e.printStackTrace(new PrintWriter(sw));

    String exceptionAsString = sw.toString();   
    out.println("" + sw.toString());

}
%>

这是我的错误&#34;:

public class HBaseApp {
    public static void main(String[] args) throws IOException, ServiceException {

        Configuration conf = HBaseConfiguration.create();
        HBaseAdmin admin = null;

        String ip = "149.202.86.142";
        String port = "2181";

        conf.set("hbase.zookeeper.quorum", ip);
        conf.set("hbase.zookeeper.property.clientPort", port);

        try {
            admin = new HBaseAdmin(conf);
            boolean bool = admin.tableExists("myTable");
            System.out.println("Table exists? " + bool);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

等等。

我使用Maven链接依赖项。

我必须确切地说我无法控制服务器的配置。 有谁知道我错过了什么?

此致

0 个答案:

没有答案