HBase JDBC 연결

JDBC 연결 HBase
1. pom. xml 의 의존

	
		
			junit
			junit
			3.8.1
			test
		

		
			org.apache.hbase
			hbase-client
			1.1.3
			
				
					guava
					com.google.guava
				
			
		
		
			com.google.guava
			guava
			15.0
		

	
	

자바 코드
package cn.com.tengen.hbase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.Set;

import org.apache.commons.io.IOUtils;
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.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;

/**
 * JDBC    Hbase  
 * 
 * @author lucky
 *
 */
public class HBaseJDBCTest {

	/**
	 *   
	 */
	private static final String FAMILY_NAMES = "a,b,c,d,e,f";
	/**
	 *   
	 */
	private static final String TABLE_NAME = "bigTab";
	/**
	 *     
	 */
	private static Connection conn;
	/**
	 * Hbase    ,     “,”  
	 */
	private static final String HOSTS = "quickstart.cloudera";

	static {
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", HOSTS);
		try {
			conn = ConnectionFactory.createConnection(conf);
			createTable(TABLE_NAME, FAMILY_NAMES);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 *    
	 * 
	 * @param tableName
	 *              
	 * @param familyNames
	 *              ,     “,”  
	 * @throws IOException
	 */
	private static void createTable(String tableName, String familyNames) throws IOException {
		Admin admin = null;
		TableName tn = TableName.valueOf(tableName);

		admin = conn.getAdmin();
		if (!admin.tableExists(tn)) {
			HTableDescriptor descriptor = new HTableDescriptor(tn);
			for (String familyName : familyNames.split(",")) {

				descriptor.addFamily(new HColumnDescriptor(familyName));
			}
			admin.createTable(descriptor);
			System.out.println("   :" + tableName);
		}

	}

	/**
	 *    
	 * 
	 * @param tableName
	 *              
	 * @throws IOException
	 */
	private static void dropTable(String tableName) throws IOException {
		Admin admin = null;
		try {
			TableName tn = TableName.valueOf(tableName);
			admin = conn.getAdmin();
			if (admin.tableExists(tn)) {
				admin.disableTable(tn);
				admin.deleteTable(tn);
				System.out.println("   :" + tableName);
			}
		} finally {
			IOUtils.closeQuietly(admin);
		}

	}

	/**
	 *     
	 * 
	 * @param tableName
	 *              
	 * @param rowKey
	 *              
	 * @param datas
	 *              
	 * @throws IOException
	 */
	private static void add(String tableName, String rowKey, List datas) throws IOException {

		Table table = conn.getTable(TableName.valueOf(tableName));
		try {
			Put list = new Put(rowKey.getBytes());
			for (String[] data : datas) {
				list.addColumn(data[0].getBytes(), data[1].getBytes(), data[2].getBytes());
			}
			table.put(list);
		} finally {
			if (table != null) {
				IOUtils.closeQuietly(table);
			}
		}
	}

	/**
	 *       
	 * 
	 * @param tableName
	 * @throws IOException
	 */
	private static void scan(String tableName) throws IOException {

		Table table = conn.getTable(TableName.valueOf(tableName));
		try {
			ResultScanner rs = table.getScanner(new Scan());
			for (Result r : rs) {
				NavigableMap>> map = r.getMap();
				Set>>> set = map.entrySet();
				for (Entry>> entry : set) {

					Set>> entrySet = entry.getValue().entrySet();
					for (Entry> entry2 : entrySet) {
						System.out.print(new String(r.getRow()));
						System.out.print("\t");
						System.out.print(new String(entry.getKey()));
						System.out.print(":");
						System.out.print(new String(entry2.getKey()));
						System.out.print(" => ");
						System.out.println(new String(entry2.getValue().firstEntry().getValue()));
					}
				}
			}
		} finally {
			if (table != null) {
				IOUtils.closeQuietly(table);
			}
		}
	}

	/**
	 *   rowKey  
	 * 
	 * @param tableName
	 * @param rowKey
	 * @throws IOException
	 */
	private static void scanByrowKey(String tableName, String rowKey) throws IOException {

		Table table = conn.getTable(TableName.valueOf(tableName));
		try {
			Result r = table.get(new Get(rowKey.getBytes()));

			NavigableMap>> map = r.getMap();
			Set>>> set = map.entrySet();
			for (Entry>> entry : set) {

				Set>> entrySet = entry.getValue().entrySet();
				for (Entry> entry2 : entrySet) {
					System.out.print(new String(r.getRow()));
					System.out.print("\t");
					System.out.print(new String(entry.getKey()));
					System.out.print(":");
					System.out.print(new String(entry2.getKey()));
					System.out.print(" => ");
					System.out.println(new String(entry2.getValue().firstEntry().getValue()));
				}
			}

		} finally {
			if (table != null) {
				IOUtils.closeQuietly(table);
			}
		}
	}

	public static void main(String[] args) throws IOException {
		List list = new ArrayList<>();
		list.add("a, a1, www1".split(","));
		list.add("a, a2, www2".split(","));
		list.add("a, a3, www3".split(","));
		list.add("a, a4, www4".split(","));
		list.add("b, a1, www5".split(","));
		list.add("b, a2, www6".split(","));
		list.add("b, a3, www7".split(","));
		list.add("b, a4, www8".split(","));
		list.add("c, a5, www9".split(","));
		list.add("c, a6, www10".split(","));
		list.add("c, a1, www11".split(","));
		list.add("c, a2, www12".split(","));
		list.add("d, a3, www13".split(","));
		list.add("e, a4, www14".split(","));
		list.add("f, a5, www15".split(","));
		add(TABLE_NAME, "100001", list);
		list = new ArrayList<>();
		list.add("a, a1, vvv1".split(","));
		list.add("a, a2, vvv2".split(","));
		list.add("a, a3, vvv3".split(","));
		list.add("a, a4, vvv4".split(","));
		list.add("b, a1, vvv5".split(","));
		list.add("b, a2, vvv6".split(","));
		list.add("b, a3, vvv7".split(","));
		list.add("b, a4, vvv8".split(","));
		list.add("c, a5, vvv9".split(","));
		list.add("c, a6, vvv10".split(","));
		list.add("c, a1, vvv11".split(","));
		list.add("c, a2, vvv12".split(","));
		list.add("d, a3, vvv13".split(","));
		list.add("e, a4, vvv14".split(","));
		list.add("f, a5, vvv15".split(","));
		add(TABLE_NAME, "100002", list);

		scan(TABLE_NAME);
		System.out.println();
		scanByrowKey(TABLE_NAME, "100002");
		dropTable(TABLE_NAME);
	}
}

인공지능 에 관심 이 있 는 학생 은 다음 링크 를 클릭 할 수 있 습 니 다.
현재 인공지능 이 매우 뜨 거 워 서 많은 친구 들 이 배우 고 싶 어 하지만 일반적인 튜 토리 얼 은 박 석 생 을 위 한 것 이기 때문에 이해 하기 어렵다.최근 소 백 입문 에 적합 한 튜 토리 얼 을 발견 했다. 통속 적 이 고 알 기 쉬 울 뿐만 아니 라 유머 러 스 하 다.그래서 참 지 못 하고 나 눠 드 리 겠 습 니 다.여 기 를 누 르 면 튜 토리 얼 로 넘 어 갈 수 있 습 니 다.
https://www.cbedai.net/u014646662

좋은 웹페이지 즐겨찾기