igorjan94

Untitled

Mar 5th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.89 KB | None | 0 0
  1. connected to unix/:/var/run/tarantool/tarantool.sock
  2. unix/:/var/run/tarantool/tarantool.sock> _TARANTOOL
  3. ---
  4. - 1.7.7-227-g33980fc59
  5. ...
  6.  
  7. unix/:/var/run/tarantool/tarantool.sock> box.cfg
  8. ---
  9. - log_nonblock: true
  10.   vinyl_run_count_per_level: 2
  11.   rows_per_wal: 500000
  12.   vinyl_range_size: 1073741824
  13.   readahead: 16320
  14.   log_level: 5
  15.   checkpoint_interval: 3600
  16.   vinyl_page_size: 8192
  17.   coredump: false
  18.   vinyl_run_size_ratio: 3.5
  19.   replication_timeout: 1
  20.   wal_max_size: 268435456
  21.   slab_alloc_factor: 1.05
  22.   background: false
  23.   vinyl_dir: /var/lib/tarantool
  24.   vinyl_cache: 134217728
  25.   vinyl_read_threads: 1
  26.   vinyl_timeout: 60
  27.   memtx_min_tuple_size: 16
  28.   listen: '3301'
  29.   too_long_threshold: 0.5
  30.   vinyl_max_tuple_size: 1048576
  31.   vinyl_write_threads: 2
  32.   vinyl_bloom_fpr: 0.05
  33.   log_format: plain
  34.   memtx_max_tuple_size: 1048576
  35.   memtx_dir: /var/lib/tarantool
  36.   hot_standby: false
  37.   vinyl_memory: 134217728
  38.   wal_mode: none
  39.   worker_pool_threads: 4
  40.   wal_dir: /var/lib/tarantool
  41.   read_only: false
  42.   pid_file: /var/run/tarantool/tarantool.pid
  43.   memtx_memory: 268435456
  44.   force_recovery: false
  45.   replication_sync_lag: 10
  46.   checkpoint_count: 2
  47.   wal_dir_rescan_delay: 2
  48. ...
  49.  
  50. unix/:/var/run/tarantool/tarantool.sock> box.space._space.index.name:select{'MySpace'}
  51. ---
  52. - - [528, 1, 'MySpace', 'memtx', 0, {}, [{
  53.     'index': 1,      'type': 'unsigned',   'isIndexable': true, 'name': 'id',      }, {
  54.     'index': 2,      'type': 'integer',    'is_nullable': true, 'name': 'deletedAt'}, {
  55.     'index': 3,      'type': 'integer',    'is_nullable': true, 'name': 'createdAt'}, {
  56.     'index': 4,      'type': 'unsigned',   'isIndexable': true, 'name': 'field1', 'defaultValue': 0}, {
  57.     'index': 5,      'type': 'unsigned',   'isIndexable': true, 'name': 'field2', 'defaultValue': 0}, {
  58.     'index': 6,      'type': 'integer',    'is_nullable': true, 'name': 'updatedAt'}, {
  59.     'index': 7,      'type': 'number',     'is_nullable': true, 'name': 'field3', 'defaultValue': 0}, {
  60.     'index': 8,      'type': 'unsigned',   'isIndexable': true, 'name': 'field4', 'defaultValue': 0}, {
  61.     'name': 'field5', 'is_nullable': true}, {
  62.     'name': 'field6', 'is_nullable': true}, {
  63.     'name': 'field7', 'is_nullable': true}]
  64. ]
  65.  
  66. unix/:/var/run/tarantool/tarantool.sock> box.space.MySpace.index
  67. ---
  68. - 0: &0
  69.     unique: true
  70.     parts:
  71.     - type: unsigned
  72.       is_nullable: false
  73.       fieldno: 1
  74.     sequence_id: 16
  75.     id: 0
  76.     space_id: 528
  77.     name: id
  78.     type: TREE
  79.   1: &1
  80.     unique: true
  81.     parts:
  82.     - type: unsigned
  83.       is_nullable: false
  84.       fieldno: 5
  85.     - type: unsigned
  86.       is_nullable: false
  87.       fieldno: 8
  88.     id: 1
  89.     space_id: 528
  90.     name: secondaryIndex
  91.     type: TREE
  92.   2: &2
  93.     unique: false
  94.     parts:
  95.     - type: unsigned
  96.       is_nullable: false
  97.       fieldno: 8
  98.     id: 2
  99.     space_id: 528
  100.     name: field4index
  101.     type: TREE
  102.   3: &3
  103.     unique: false
  104.     parts:
  105.     - type: unsigned
  106.       is_nullable: false
  107.       fieldno: 4
  108.     id: 3
  109.     space_id: 528
  110.     name: field1index
  111.     type: TREE
  112.   secondaryIndex: *1
  113.   id: *0
  114.   field1index: *3
  115.   field4index: *2
  116. ...
  117.  
  118. --LUA FUNCTION
  119. function createOrUpdate(space_name, value)
  120.     local one = box.space.MySpace.index.secondaryIndex:get{value.field2, value.field4}
  121.     if one then
  122.         box.space.MySpace:update(one[1], {{'=', 7, value.field3}})
  123.     else
  124.         box.space.MySpace:auto_increment{0, 0, value.field1, value.field2, 0, value.field3, value.field4} --This code was executed only once long long ago in a galaxy far far away
  125.     end
  126.     return value
  127. end
  128.  
  129.  
  130. //JAVA CODE
  131.  
  132. //Connector.java
  133.     private static TarantoolClient client;
  134.    
  135.     public Connector(String host, int port, String username, String password) throws IOException {
  136.         //Initializing config
  137.         client = new TarantoolClientImpl(socketChannelProvider, config);
  138.     }
  139.  
  140.     public<T> void createOrUpdate(T t, Class<T> tClass) {
  141.         try {
  142.             client.asyncOps().call("createOrUpdate", getSpaceName(tClass.getSimpleName()), objectMapper.convertValue(t, Map.class)).get();
  143.         } catch (...) { ... }
  144.     }
  145.  
  146. //MySpace.java
  147. public class MySpace {
  148.     public int field1;
  149.     public int field2;
  150.     public double field3;
  151.     public int field4;
  152.    
  153.     //Constructors, toString and so on
  154. }
  155.  
  156. //In psvm
  157.     List<MySpace> objects = get();
  158.     System.out.println(objects.size());    //1_194_285
  159.     objects.parallelStream().forEach(object -> connector.createOrUpdate(object, MySpace.class));
  160.  
  161.  
  162. $ docker ps
  163. CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                PORTS                      NAMES
  164. 645ebd1bc754        progaudi/tarantool:1.7.7    "docker-entrypoint..."   3 weeks ago         Up 2 days (healthy)   0.0.0.0:3303->3301/tcp     tarantool
  165.  
  166. //crontab
  167. 0 * * * * java -jar java.jar localhost 3303 username password "<params>" >> /root/log.txt
Add Comment
Please, Sign In to add comment