/* * (non-Javadoc) * * @see com.hazelcast.core.MapStore#store(java.lang.Object, * java.lang.Object) */ @Override public void store(String key, String value) { HTableInterface table = null; try { table = pool.getTable(tableName); try { byte[] rowId = prefixDate ? IdUtil.bucketizeId(key) : Bytes.toBytes(key); Put p = new Put(rowId); if (outputFormatType == StoreFormatType.SMILE) { p.add(family, qualifier, jsonSmileConverter.convertToSmile(value)); } else { p.add(family, qualifier, Bytes.toBytes(value)); } table.put(p); } catch (NumberFormatException nfe) { LOG.error("Encountered bad key: " + key, nfe); } } catch (IOException e) { LOG.error("Error during put", e); } finally { if (table != null) { pool.putTable(table); } } }
public static void writeTest(String tableStr) { try { Configuration conf = HBaseConfiguration.create(); byte[] tableName = Bytes.toBytes(tableStr); HConnection hConnection = HConnectionManager.createConnection(conf); HTableInterface table = hConnection.getTable(tableName); byte[] family = f0; List<Put> puts = new ArrayList<Put>(); for (int k = 0; k < 10; k++) // 写10行数据 { byte[] rowkey = Bytes.toBytes("rowKey_" + k); Put p = new Put(rowkey); byte[] value_id = Bytes.toBytes("123456"); byte[] value_user = Bytes.toBytes("mengqinghao" + k); p.add(family, qualifier_id, value_id); p.add(family, qualifier_user, value_user); puts.add(p); } table.put(puts); System.out.println("Puts done: " + puts.size()); table.close(); // very important } catch (IOException e) { e.printStackTrace(); } }
/** * Code for each 'client' to run. * * @param id * @param c * @param sharedConnection * @throws IOException */ static void cycle(int id, final Configuration c, final HConnection sharedConnection) throws IOException { HTableInterface table = sharedConnection.getTable(BIG_USER_TABLE); table.setAutoFlushTo(false); long namespaceSpan = c.getLong("hbase.test.namespace.span", 1000000); long startTime = System.currentTimeMillis(); final int printInterval = 100000; Random rd = new Random(id); boolean get = c.getBoolean("hbase.test.do.gets", false); try { Stopwatch stopWatch = new Stopwatch(); stopWatch.start(); for (int i = 0; i < namespaceSpan; i++) { byte[] b = format(rd.nextLong()); if (get) { Get g = new Get(b); table.get(g); } else { Put p = new Put(b); p.add(HConstants.CATALOG_FAMILY, b, b); table.put(p); } if (i % printInterval == 0) { LOG.info("Put " + printInterval + "/" + stopWatch.elapsedMillis()); stopWatch.reset(); stopWatch.start(); } } LOG.info( "Finished a cycle putting " + namespaceSpan + " in " + (System.currentTimeMillis() - startTime) + "ms"); } finally { table.close(); } }
@Override public void put(List<Put> puts) throws IOException { table.put(puts); }
@Override public void put(Put put) throws IOException { table.put(put); }